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)
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++17)

Elementary string conversions
(C++17)
(C++17)
 
Date and time utilities
(C++11)
(C++11)
Time of day
(C++20)



(C++20)(C++20)(C++20)(C++20)
Clocks
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time zones
(C++20)
(C++20)
(C++20)
(C++20)
C-style date and time
 
 
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.

Example

#include <iostream>
#include <chrono>
 
int main()
{
    constexpr int iy1{2020};
    std::cout << iy1 << " year is "
              << (std::chrono::year{iy1}.ok() ? "valid" : "invalid") << '\n';
 
    constexpr int iy2{1 << 15};
    std::cout << iy2 << " year is "
              << (std::chrono::year{iy2}.ok() ? "valid" : "invalid") << '\n';
}

Output:

2020 year is valid
32768 year is invalid