Namespaces
Variants

std::chrono:: operator+, std::chrono:: operator- (std::chrono::year_month)

From cppreference.net
(Anmerkung: Der bereitgestellte HTML-Code enthält keinen übersetzbaren Text, da alle Tags leer sind. Die Struktur wurde gemäß den Anforderungen unverändert beibehalten.)
constexpr std:: chrono :: year_month Operator + ( const std:: chrono :: year_month & ym,
const std:: chrono :: years & dy ) noexcept ;
(1) (seit C++20)
constexpr std:: chrono :: year_month operator + ( const std:: chrono :: years & dy,
const std:: chrono :: year_month & ym ) noexcept ;
(2) (seit C++20)
constexpr std:: chrono :: year_month operator + ( const std:: chrono :: year_month & ym,
const std:: chrono :: months & dm ) noexcept ;
(3) (seit C++20)
constexpr std:: chrono :: year_month operator + ( const std:: chrono :: months & dm,
const std:: chrono :: year_month & ym ) noexcept ;
(4) (seit C++20)
constexpr std:: chrono :: year_month operator - ( const std:: chrono :: year_month & ym,
const std:: chrono :: years & dy ) noexcept ;
(5) (seit C++20)
constexpr std:: chrono :: year_month operator - ( const std:: chrono :: year_month & ym,
const std:: chrono :: months & dm ) noexcept ;
(6) (seit C++20)
constexpr std:: chrono :: months operator - ( const std:: chrono :: year_month & ym1,
const std:: chrono :: year_month & ym2 ) noexcept ;
(7) (seit C++20)
1,2) Fügt dy. count ( ) Jahre zu ym hinzu.
3,4) Fügt dm. count ( ) Monate zu ym hinzu.
5) Zieht dy. count ( ) Jahre von ym ab.
6) Subtrahiert dm. count ( ) Monate von ym .
7) Gibt die Differenz in Monaten zwischen den beiden Zeitpunkten zurück, die durch ym1 und ym2 dargestellt werden.

Für Zeitdauern, die sowohl in std::chrono::years als auch in std::chrono::months konvertierbar sind, werden die years Überladungen (1,2,5) bevorzugt, wenn der Aufruf andernfalls mehrdeutig wäre.

Inhaltsverzeichnis

Rückgabewert

1,2) std:: chrono :: year_month ( ym. year ( ) + dy, ym. month ( ) )
3,4) Ein year_month -Wert z , sodass z - ym == dm und z. ok ( ) == true .
5) ym + - dy
6) ym + - dm
7)
ym1. year ( ) - ym2. year ( ) + std:: chrono :: months ( int ( unsigned ( ym1. month ( ) ) ) -
int ( unsigned ( ym2. month ( ) ) ) )

Hinweise

Das Ergebnis der Subtraktion zweier year_month -Werte ist eine Dauer vom Typ std::chrono::months . Diese Dauer-Einheit repräsentiert die Länge des durchschnittlichen gregorianischen Monats (30,436875 Tage), und die resultierende Dauer steht in keiner Beziehung zur tatsächlichen Anzahl der Tage im betreffenden Zeitraum. Zum Beispiel ist das Ergebnis von 2017y / 3 - 2017y / 2 gleich std:: chrono :: months ( 1 ) , obwohl der Februar 2017 nur 28 Tage enthält.

Beispiel

#include <cassert>
#include <chrono>
int main()
{
    auto ym{std::chrono::year(2021)/std::chrono::July};
    ym = ym + std::chrono::months(14);
    assert(ym.month() == std::chrono::September);
    assert(ym.year() == std::chrono::year(2022));
    ym = ym - std::chrono::years(3);
    assert(ym.month() == std::chrono::month(9));
    assert(ym.year() == std::chrono::year(2019));
    ym = ym + (std::chrono::September - std::chrono::month(2));
    assert(ym.month() == std::chrono::April);
    assert(ym.year() == std::chrono::year(2020));
}

Siehe auch

modifiziert das year_month um eine Anzahl von Monaten oder Jahren
(öffentliche Elementfunktion)