year 類別

代表公曆中的一年。

語法

class year; // C++20

備註

year可以保留介於 -32767 到 32767 之間的年份值。

成員

名稱 描述
建構函式 建構 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== 判斷兩年是否相等。
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
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 除以 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
標頭檔參考