std::chrono::year::ok

From cppreference.com
< cpp‎ | chrono‎ | year
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
 
 
constexpr bool ok() const noexcept;
(since C++20)

Checks if the year value stored in *this is in the valid range, i.e., [-32767, 32767].

Return value

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

Possible implementation

See the implementations in libstdc++, libc++, and Howard Hinnant's date.h.

class Year
{
    short year_; /* exposition only */
 
public:
 
    bool ok() const noexcept { return year_ != std::numeric_limits<short>::min(); }
 
    /*...*/
};

Example

#include <iostream>
#include <iomanip>
#include <chrono>
 
int main()
{
    for (const int i : {2020, 0x8000, 0x8001, 0xFFFF, 0x18000}) {
        const std::chrono::year y{i};
        std::cout << std::boolalpha
            << "input year: " << std::setw(6) << i
            << " │ internal value: " << std::setw(7) << static_cast<int>(y)
            << " │ ok(): " << y.ok() << '\n';
    }
}

Possible output:

input year:   2020 │ internal value:    2020 │ ok(): true
input year:  32768 │ internal value:  -32768 │ ok(): false
input year:  32769 │ internal value:  -32767 │ ok(): true
input year:  65535 │ internal value:      -1 │ ok(): true
input year:  98304 │ internal value:  -32768 │ ok(): false