名前空間
変種
操作

std::chrono::year_month_day::ok

From cppreference.com
 
 
 
 
constexpr bool ok() const noexcept;
(C++20以降)

このyear_month_dayオブジェクトが有効な暦日を表しているかを確認します。

[編集] 戻り値

true このyear_month_dayオブジェクトが有効な暦日を表している場合。つまり、格納されている年、月、日の値がすべて有効であり、格納されている日の値が指定された年と月の合計日数内にある場合。それ以外の場合は false

[編集] 実装例

constexpr bool std::chrono::year_month_day::ok() const noexcept
{
    return year().ok() && month().ok() && day().ok() &&
        day() <= (year()/month()/std::chrono::last).day();
}

[編集]

#include <chrono>
 
int main()
{
    constexpr auto ymd1 {std::chrono::day(1)/std::chrono::July/2020};
    static_assert(ymd1.ok());
 
    constexpr auto ymd2 {std::chrono::year(2020)/7/42};
    static_assert(not ymd2.ok());
 
    constexpr auto ymd3 {std::chrono::February/29/2020}; // ok, leap year
    static_assert(ymd3.ok());
 
    constexpr auto ymd4 = ymd3 + std::chrono::years{1}; // bad, not a leap year
    static_assert(ymd4 == std::chrono::February/29/2021 and not ymd4.ok());
 
    // to fix the bad date we may want to snap to the last day of the month:
    if constexpr (!ymd4.ok())
    {
        constexpr auto ymd = ymd4.year()/ymd4.month()/std::chrono::last;
        static_assert(ymd == std::chrono::February/28/2021 and ymd.ok());
    }
 
    // or we may want to overflow to the next month:
    if constexpr (!ymd4.ok())
    {
        constexpr auto st = std::chrono::sys_time<std::chrono::days>{ymd4};
        constexpr auto ymd = std::chrono::year_month_day{st};
        static_assert(ymd == std::chrono::March/1/2021 and ymd.ok());
    }
}
English 日本語 中文(简体) 中文(繁體)