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

履歴 編集

class
<chrono>

std::formatter(C++20)

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

概要

year_month_day_lastクラスに対する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_last date = 2020y/2/chrono::last;

  // デフォルトフォーマットは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/Feb/last
2 : Feb/29/2020
3 : 2020-02-29
4 : 02/29/20
5 : 2020年02月29日
6 : 2020年02月29日
7 : 令和02年02月29日

バージョン

言語

  • C++20

処理系

  • Clang: (9.0時点で実装なし)
  • GCC: (9.2時点で実装なし)
  • Visual C++: (2019 Update 3時点で実装なし)

関連項目