다음을 통해 공유


month 클래스

1년 월을 나타냅니다. 예를 들어 7월입니다.

구문

class month; // C++20

설명

month A는 일반적으로 [1, 12] 범위의 값을 보유합니다. 음수가 아닌 값을 이 범위 밖에 유지할 수도 있습니다. 클래스와 함께 사용할 수 있는 상수는 아래의 월 상수를 month 참조하세요.

멤버

속성 설명
생성자 를 생성합니다 month.
ok 월 값이 유효한 범위 [1,12]에 있는지 확인합니다.
operator++ 를 증분합니다 month.
operator+= 지정한 월 수를 이 month에 추가합니다.
operator-- month값은 감소합니다.
operator-= 지정된 월 수를 이 month월에서 뺍니다.
operator unsigned 값을 가져옵니다 month .

비멤버

이름 설명
from_stream 지정된 형식을 month 사용하여 지정된 스트림에서 구문 분석합니다.
operator+ 지정된 개월 수를 이 month값에 추가하여 새 month 인스턴스를 반환합니다.
operator- 이번 달에서 지정한 월 수를 뺍니다. 새 month 인스턴스를 반환합니다.
operator== 두 달이 같은지 여부를 확인합니다.
operator<=> 이번 달을 다른 달과 비교합니다. >, >=, <=, <, != 연산자는 컴파일러에 의해 합성됩니다.
operator<< 지정된 스트림에 출력 month 합니다.

요구 사항

헤더:<chrono> (C++20 이후)

네임스페이스:std::chrono

컴파일러 옵션:/std:c++latest

생성자

를 생성합니다 month.

1) month() = default;
2) explicit constexpr month(unsigned m) noexcept;

매개 변수

m
with month 값을 생성합니다 m.

설명

1) 기본 생성자는 일 값을 초기화하지 않습니다.
2) 일 값을 초기화하여 생성 month 합니다 m.

ok

month에 저장된 값이 유효한 범위에 있는지 확인합니다.

constexpr bool ok() const noexcept;

반환 값

true 월 값이 [1,12] 범위에 있으면 입니다. 그렇지 않으면 false입니다.

operator++

월 값을 증분합니다.

1) constexpr month& operator++() noexcept;
2) constexpr month operator++(int) noexcept;

반환 값

1) 증분된 월(후위 증분)에 대한 참조 *this 입니다.
2) 증분되기 전month복사본입니다(접두사 증가).

예: operator++

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

using namespace std::chrono;

int main()
{
    month m{ January };
    month m2{4}; // April

    std::cout << m << " " << ++m << "\n"; // constexpr month& operator++() noexcept
    std::cout << m << " " << m++ << "\n"; // constexpr month operator++(int) noexcept
    std::cout << m << "\n";
    std::cout << m2 << "\n";

    return 0;
}
Jan Feb
Feb Feb
Mar
Apr

설명

결과가 12를 초과하면 1로 설정됩니다.

operator--

월 값에서 1을 뺍니다.

1) constexpr month& operator--() noexcept;
2) constexpr month operator--(int) noexcept;

반환 값

1) 감소된 후의 참조*thismonth입니다(후위 감소).
2) 감소되기 전month복사본입니다(접두사 감소).

예: operator--

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

using namespace std;
using namespace std::chrono;

int main()
{
    month m{May};

    cout << m << " " << --m << "\n"; // constexpr month& operator++() noexcept
    cout << m << " " << m-- << "\n"; // constexpr month operator++(int) noexcept
    cout << m << "\n";

    return 0;
}
May Apr
Apr Apr
Mar

설명

감소된 결과가 1보다 작으면 12로 설정됩니다.

operator+=

month에 월을 추가합니다.

constexpr month& operator+=(const months& m) noexcept;

매개 변수

m
추가할 월 수입니다.

반환 값

*this

operator-=

에서 month빼기months.

constexpr month& operator-=(const months& m) noexcept;

매개 변수

m
뺄 개월입니다.

반환 값

*this.

operator unsigned

서명 month 되지 않은 값을 가져옵니다.

explicit constexpr operator unsigned() const noexcept;

반환 값

이 값의 부호 없는 값입니다. month

예: operator unsigned()

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

using namespace std::chrono;

int main()
{
    month m{July};
    unsigned monthValue = static_cast<unsigned>(m);
    std::cout << monthValue << "\n";

    return 0;
}
7

월 상수

(C++20) 헤더는 <chrono> 코드의 편의성, 형식 안전성 및 기본 향상을 위해 사용할 month 수 있는 다음 상수들을 정의합니다. 이러한 상수는 범위에 있는 경우 std::chrono 범위에 있습니다.

// Calendrical constants
inline constexpr month January{1};
inline constexpr month February{2};
inline constexpr month March{3};
inline constexpr month April{4};
inline constexpr month May{5};
inline constexpr month June{6};
inline constexpr month July{7};
inline constexpr month August{8};
inline constexpr month September{9};
inline constexpr month October{10};
inline constexpr month November{11};
inline constexpr month December{12};

참고 항목

<chrono>
month_day 클래스
month_day_last 클래스
month_weekday 클래스
month_weekday_last 클래스