Condividi tramite


Classe year_month

Rappresenta un mese e un anno. Il giorno non è specificato.

Sintassi

class year_month; // C++20

Membri

Nome Descrizione
Costruttori Costruire un oggetto year_month
year Restituisce l'anno.
month Restituisce il mese.
ok Verificare che i year valori e month siano inclusi nell'intervallo valido.
operator+= Aggiungere il numero specificato di mesi o anni.
operator-= Sottrarre il numero specificato di mesi o anni.

Membri non membri

Nome Descrizione
from_stream Analizzare un oggetto year_month da un flusso usando il formato specificato
operator+ Aggiungere mesi e/o anni.
operator- Sottrae mesi e/o anni.
operator== Determinare se due year_month valori sono uguali.
operator<=> Confrontare due year_month valori. Gli >, >=, <=, <, != operatori vengono sintetizzati dal compilatore.
operator<< Restituire un oggetto year_month a un flusso.

Requisiti

Intestazione: <chrono> (da C++20)

Spazio dei nomi: std::chrono

Opzione del compilatore: /std:c++latest

Costruttori

Creare un oggetto year_month.

1) year_month() = default;
2) constexpr year_month(const year& y, const month& m) noexcept;

Parametri

y
Valore year.

m
Valore month.

Osservazioni:

1) Il costruttore predefinito non inizializza il year valore o month .
2) Costruire un year_month oggetto con i valori specificati.

Per informazioni sulla sintassi C++20 per specificare le date, vedere operator/

Esempio: Creare un oggetto year_month

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

    std::cout << ym;
    return 0;
}
2021/Jun

month

Ottenere il mese.

constexpr month month() const noexcept;

Valore restituito

Valore month.

year

Prendi l'anno.

constexpr year year() const noexcept;

Valore restituito

Il year.

ok

Controllare se il valore anno e mese archiviato in questo year_month intervallo sono entrambi inclusi nell'intervallo valido.

constexpr bool ok() const noexcept;

Valore restituito

true se i year_month valori year e month sono compresi nell'intervallo valido. In caso contrario, false.

operator+=

Aggiungere mesi o anni a questo year_monthoggetto .

1) constexpr year_month& operator+=(const months& dm) noexcept;
2) constexpr year_month& operator+=(const years& dy) noexcept;

Parametri

dm
Numero di mesi da aggiungere.

dy
Numero di anni da aggiungere.

Valore restituito

*this, che riflette il risultato dell'addizione.

Esempio: operator +=

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

    std::cout << ym << '\n';

    ym += months{2};
    ym += years{1};

    std::cout << ym;
    
    return 0;
}
2021/Jun
2022/Aug

operator-=

Sottrae mesi o anni da questo year_monthoggetto .

1) constexpr year_month& operator-=(const months& dm) noexcept;
2) constexpr year_month& operator-=(const years& dy) noexcept;

Parametri

dm
Numero di mesi da sottrarre.

dy
Numero di anni da sottrarre.

Valore restituito

*this, che riflette il risultato della sottrazione.

Esempio: operator -=

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

    std::cout << ym << '\n';

    ym -= months{2};
    ym -= years{1};

    std::cout << ym;
    
    return 0;
}
2021/Jun
2020/Apr

Vedi anche

<chrono>
year
year_month_day
year_month_day_last
year_month_weekday
year_month_weekday_last
operator/
Riferimento file di intestazione