Muokkaa

Jaa


year class

Represents a year in the Gregorian calendar.

Syntax

class year; // C++20

Remarks

A year can hold a year value between -32767 to 32767.

Members

Name Description
Constructors Construct a year
is_leap Determine if the year is a leap year.
max Returns the largest possible year value.
min Returns the smallest possible year value.
ok Verify that the year value is in the valid range [-32767, 32767].
operator+ Unary plus.
operator++ Increment the year.
operator+= Add the specified number of years to this year.
operator- Unary minus.
operator-- Decrement the year.
operator-= Subtract the specified number of years from this year.
operator int Get the year value.

Non-members

Name Description
from_stream Parse a year from a stream using the specified format
operator+ Add years.
operator- Subtract years.
operator== Determine whether two years are equal.
operator<=> Compare this year against another year. The >, >=, <=, <, != operators are synthesized by the compiler.
operator<< Output a year to the given stream.
operator""y Create a year literal.

Requirements

Header: <chrono> (since C++20)

Namespace: std::chrono

Compiler Option: /std:c++latest

Constructors

Construct a year.

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

Parameters

y
Construct a year with value y.

Remarks

1) The default constructor doesn't initialize the year value.
2) Construct a year with the specified value.

Example: Create a 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

Check if the value stored in this year is in the valid range.

constexpr bool is_leap() const noexcept;

Return value

true if the year value is a leap year. Otherwise, false. A leap year is a year divisible by 4 but not 100--or is divisible by 400.

max

Returns the largest possible year.

static constexpr year max() noexcept;

Return value

year{32767}

min

Returns the smallest possible year.

static constexpr year min() noexcept;

Return value

year{-32767}

ok

Check if the value stored in this year is in the valid range.

constexpr bool ok() const noexcept;

Return value

true if the year value is in the range [-32676, 32767]. Otherwise, false.

operator+

Apply unary plus.

constexpr year operator+() const noexcept;

Return value

Returns *this

Example: unary 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++

Add 1 to the year value.

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

Return value

1) Returns reference to this year after it has been incremented (a postfix increment).
2) Returns a copy of the year, before it has been incremented (a prefix increment).

Example: 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

Remarks

If the incremented result exceeds 32767, it overflows to -32768

operator-

Unary minus. Negate the year.

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

Return value

Returns a negated copy of the year.

Example: unary 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--

Subtract 1 from the year value.

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

Return value

1) A reference to this year after it has been decremented (a postfix decrement).
2) A copy of the year before it has been decremented (a prefix decrement).

Example: 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

Remarks

If the decremented result is less than -32768, it's set to 32767.

operator+=

Add days to this year.

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

Parameters

y
The number of years to add.

Return value

*this If the incremented result exceeds 32767, it overflows to -32768.

operator-=

Subtract days from this year.

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

Parameters

y
The number of years to subtract.

Return value

*this. If the decremented result is less than -32768, it's set to 32767.

operator int

Get the year value.

explicit constexpr operator int() const noexcept;

Return value

The value of the year

Example: 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

See also

<chrono>
year_month
year_month_day
year_month_day_last
year_month_weekday
year_month_weekday_last
Header Files Reference