• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <chrono>

    std::chrono::year_month_day::operator local_days

    constexpr explicit operator local_days() const noexcept; // (1) C++20
    

    概要

    year_month_dayオブジェクトをローカル時間の日付に、明示的に型変換する。

    戻り値

    備考

    • 日のみ範囲外の場合、以下のように、表そうとしている日付はそのままにしてシステム時間のエポックからの経過時間を求めて、ローカル時間に変換される
      • (うるう年以外の年)年2月29日は、(そのままの年)年3月1日に変換される
      • xxxx年1月0日は、xxxx - 1年12月31日に変換される

    #include <iostream>
    #include <chrono>
    
    namespace chrono = std::chrono;
    using namespace std::chrono_literals;
    
    int main()
    {
      // year_month_dayからlocal_daysへの明示的変換
      chrono::local_days date{2020y/3/1};
      std::cout << date << std::endl;
    
      // 日だけ範囲外に大きくなった場合でも、ローカル日付を求められる
      chrono::local_days over_day{2019y/2/29};
      std::cout << over_day << std::endl;
    
      chrono::local_days over_min_day{2020y/1/0};
      std::cout << over_min_day << std::endl;
    }
    

    出力

    2020-03-01 00:00:00
    2019-03-01 00:00:00
    2019-12-31 00:00:00
    

    バージョン

    言語

    • C++20

    処理系

    関連項目