最終更新日時:
が更新

履歴 編集

class template
<locale>

std::time_put

namespace std {
  template <class charT, class OutputIterator = ostreambuf_iterator<charT> >
  class time_put : public locale::facet;
}

概要

time_putは、std::tmの内容を書式化して出力するためのロケールファセットである。書式指定子はstd::strftime()と同一に解釈される。

テンプレートパラメータOutputIteratorは、出力に使用するイテレータの型を表し、既定ではstd::ostreambuf_iterator<charT>である。

std::put_timeマニピュレータは、このファセットを介して日時の書式化を行う。

メンバ関数

publicメンバ関数

名前 説明
(constructor) コンストラクタ
put 日時を出力する

静的メンバ変数

名前 説明
static locale::id id; このファセットを識別するためのID

protectedメンバ関数

名前 説明
(destructor) デストラクタ
do_put 日時を出力する (virtual)

メンバ型

名前 説明
char_type 文字型 charT
iter_type 出力のイテレータ型 OutputIterator

基本的な使い方

#include <iostream>
#include <sstream>
#include <locale>
#include <iterator>
#include <ctime>

int main()
{
  std::tm t{};
  t.tm_year = 126; // 2026年
  t.tm_mon = 7;    // 8月
  t.tm_mday = 25;

  std::ostringstream oss;

  // ストリームのロケールからtime_putファセットを取得する
  const auto& facet = std::use_facet<std::time_put<char>>(oss.getloc());

  const char pattern[] = "%Y/%m/%d";
  facet.put(std::ostreambuf_iterator<char>{oss}, oss, ' ', &t, pattern, pattern + 8);

  std::cout << oss.str() << std::endl;
}

出力

2026/08/25

ロケールによる違い

%x(日付)・%A(曜日名)・%B(月名)のように、Cロケールに依存すると規定されている書式指定子は、ストリームのロケールによって生成される文字列が変わる。

#include <iostream>
#include <sstream>
#include <locale>
#include <iterator>
#include <ctime>
#include <string>
#include <stdexcept>

// 書式化の処理自体はロケールに依存しない
std::string format_time(const std::locale& loc, const std::tm& t, const std::string& pattern)
{
  std::ostringstream oss;
  oss.imbue(loc);

  const auto& facet = std::use_facet<std::time_put<char>>(oss.getloc());
  facet.put(std::ostreambuf_iterator<char>{oss}, oss, ' ', &t,
            pattern.data(), pattern.data() + pattern.size());

  return oss.str();
}

int main()
{
  std::tm t{};
  t.tm_year = 126; // 2026年
  t.tm_mon = 7;    // 8月
  t.tm_mday = 25;
  t.tm_wday = 2;   // 火曜日

  for (const char* name : {"en_US.UTF-8", "ja_JP.UTF-8", "de_DE.UTF-8"}) {
    try {
      std::locale loc{name};

      std::cout << name << std::endl;
      std::cout << "  %x : " << format_time(loc, t, "%x") << std::endl;
      std::cout << "  %A : " << format_time(loc, t, "%A") << std::endl;
      std::cout << "  %B : " << format_time(loc, t, "%B") << std::endl;
    }
    catch (const std::runtime_error&) {
      // 指定した名前のロケールが利用できない場合
      std::cout << name << " : not available" << std::endl;
    }
  }
}

出力例

en_US.UTF-8
  %x : 08/25/2026
  %A : Tuesday
  %B : August
ja_JP.UTF-8
  %x : 2026/08/25
  %A : 火曜日
  %B : 8月
de_DE.UTF-8
  %x : 25.08.2026
  %A : Dienstag
  %B : August

  • %xが生成する日付の書式は、米国では月・日・年、日本では年・月・日、ドイツでは日・月・年の順となる。この順序はstd::time_get::date_order()で取得できる
  • Cロケールに依存すると規定されている書式指定子について生成される文字列は、処理系定義である
  • 妥当なロケール名は処理系定義である。指定した名前のロケールが利用できない場合、std::localeのコンストラクタはstd::runtime_errorを送出し、上記の例ではnot availableが出力される

バージョン

言語

  • C++98

関連項目