• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    class
    <chrono>

    std::formatter

    namespace std {
      template <class charT>
      struct formatter<chrono::year_month_day, charT>;
    }
    

    概要

    year_month_dayクラスに対するstd::formatterクラステンプレートの特殊化。

    フォーマットフラグとしては、以下を使用できる:

    フォーマットフラグ 説明
    %D %m/%d/%yと等価
    %F %Y-%m-%dと等価
    %j 年の日 (1月1日を001とした経過日)
    %x ロケール依存の日付表現
    %Ex %xの異なる表現

    その他、daymonthyearで利用可能なフォーマットフラグを使用できる。

    #include <iostream>
    #include <chrono>
    #include <format>
    
    namespace chrono = std::chrono;
    using namespace std::chrono_literals;
    
    int main() {
      chrono::year_month_day date = 2020y/3/1;
    
      // デフォルトフォーマットはoperator<<と同じ
      std::cout << std::format("1 : {}", date) << std::endl;
    
      std::cout << std::format("2 : {:%D}", date) << std::endl;
      std::cout << std::format("3 : {:%F}", date) << std::endl;
      std::cout << std::format("4 : {:%x}", date) << std::endl;
      std::cout << std::format("5 : {:%Y年%m月%d日}", date) << std::endl;
    
      // ロケール依存の出力
      std::cout << std::format(std::locale("ja_JP.UTF-8"), "6 : {:%x}", date) << std::endl;
      std::cout << std::format(std::locale("ja_JP.UTF-8"), "7 : {:%Ex}", date) << std::endl;
    }
    

    出力

    1 : 2020-03-01
    2 : Mar/01/2020
    3 : 2020-03-01
    4 : 03/01/20
    5 : 2020年03月01日
    6 : 2020年03月01日
    7 : 令和02年03月01日
    

    バージョン

    言語

    • C++20

    処理系

    関連項目