Share via


year 클래스

그레고리오력의 연도를 나타냅니다.

구문

class year; // C++20

설명

A year 는 -32767에서 32767 사이의 연도 값을 보유할 수 있습니다.

멤버

속성 설명
생성자 a 생성 year
is_leap 연도가 윤년인지 확인합니다.
max 가능한 가장 큰 연도 값을 반환합니다.
min 가능한 가장 작은 연도 값을 반환합니다.
ok 연도 값이 유효한 범위 [-32767, 32767]에 있는지 확인합니다.
operator+ 단항 플러스.
operator++ 연도를 증분합니다.
operator+= 지정된 연도 수를 이 year연도에 추가합니다.
operator- 단항 빼기.
operator-- 연도를 줄입니다.
operator-= 이 값에서 year지정된 연도 수를 뺍니다.
operator int 값을 가져옵니다 year .

비멤버

이름 설명
from_stream 지정된 형식을 year 사용하여 스트림에서 구문 분석
operator+ 연도를 추가합니다.
operator- 연도를 뺍니다.
operator== 2년이 같은지 여부를 확인합니다.
operator<=> 다른 year에 대해 비교합니다year. >, >=, <=, <, != 연산자는 컴파일러에 의해 합성됩니다.
operator<< 지정된 스트림에 출력 year 합니다.
operator""y 리터럴을 만듭니다 year .

요구 사항

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

네임스페이스:std::chrono

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

생성자

를 생성합니다 year.

1) year() = default;
2) explicit constexpr year(unsigned y) noexcept;

매개 변수

y
with year 값을 생성합니다 y.

설명

1) 기본 생성자는 값을 초기화 year 하지 않습니다.
2) 지정된 값을 사용하여 생성 year 합니다.

예: year 만들기

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

using namespace std::chrono;

int main()
{
    year y{2020};
    year y2 = 2021y;
    
    std::cout << y << ", " << y2;

    return 0;
}
2020, 2021

is_leap

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

constexpr bool is_leap() const noexcept;

반환 값

true 연도 값이 윤년인 경우 그렇지 않으면 false입니다. 윤년은 4로 나눌 수 있지만 100이 아닌 연도이거나 400으로 나눌 수 있습니다.

max

가능한 가장 큰 연도를 반환합니다.

static constexpr year max() noexcept;

반환 값

year{32767}

min

가능한 가장 작은 연도를 반환합니다.

static constexpr year min() noexcept;

반환 값

year{-32767}

ok

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

constexpr bool ok() const noexcept;

반환 값

true 연도 값이 [-32676, 32767] 범위에 있으면 입니다. 그렇지 않으면 false입니다.

operator+

단항 플러스를 적용합니다.

constexpr year operator+() const noexcept;

반환 값

*this를 반환합니다.

예: 단항 operator+

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

using namespace std::chrono;

int main()
{
   year y{-1};
   std::cout << +y;
   return 0;
}
-0001

operator++

연도 값에 1을 추가합니다.

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

반환 값

1) 증가된 후 올해에 대한 참조를 반환합니다(후위 증가).
2) 증분되기 전에 (접두사 증가)의 year복사본을 반환합니다.

예: operator++

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

using namespace std::chrono;

int main()
{
    year y{2021};

    std::cout << y << " " << ++y << "\n"; // constexpr year& operator++() noexcept
    std::cout << y << " " << y++ << "\n"; // constexpr year operator++(int) noexcept
    std::cout << y << "\n";
    return 0;
}
2021 2022
2022 2022
2023

설명

증가된 결과가 32767을 초과하면 -32768로 오버플로됩니다.

operator-

단항 빼기. 를 부정하십시오 year.

constexpr year operator-() const noexcept; // C++20

반환 값

의 부정된 복사본을 반환합니다 year.

예: 단항 operator-

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

using namespace std::chrono;

int main()
{
   year y{1977};
   std::cout << -y << '\n';

   return 0;
}
-1977

operator--

연도 값에서 1을 뺍니다.

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

반환 값

1) 감소된 후 이에 year대한 참조입니다(후위 감소).
2) 감소되기 전year복사본입니다(접두사 감소).

예: operator--

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

using namespace std::chrono;

int main()
{
   year y{2021};

    std::cout << y << " " << --y << "\n"; // constexpr year& operator++() noexcept
    std::cout << y << " " << y-- << "\n"; // constexpr year operator++(int) noexcept
    std::cout << y << "\n";

    return 0;
}
2021 2020
2020 2020
2019

설명

감소된 결과가 -32768보다 작으면 32767로 설정됩니다.

operator+=

year날짜에 일을 추가합니다.

constexpr year& operator+=(const years& y) noexcept;

매개 변수

y
추가할 연도 수입니다.

반환 값

*this 증가된 결과가 32767을 초과하면 -32768로 오버플로됩니다.

operator-=

year날짜에서 일을 뺍니다.

constexpr year& operator-=(const years& y) noexcept;

매개 변수

y
뺄 연도 수입니다.

반환 값

*this. 감소된 결과가 -32768보다 작으면 32767로 설정됩니다.

operator int

값을 가져옵니다 year .

explicit constexpr operator int() const noexcept;

반환 값

의 값입니다. year

예: operator int()

// compile using: /std:c++latest

 #include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year y{2020};
    int yearValue = static_cast<int>(y);
    std::cout << yearValue;

    return 0;
}
2020

참고 항목

<chrono>
year_month
year_month_day
year_month_day_last
year_month_weekday
year_month_weekday_last
헤더 파일 참조