最終更新日時:
が更新

履歴 編集

function
<ctime>

std::strftime

namespace std {
  size_t strftime(char* s, size_t maxsize,
                  const char* format, const tm* timeptr);
}

概要

カレンダー時間 (tm構造体) を、指定したフォーマットに従って文字列化する。

書式は現在のロケール (<locale>) に影響される。

効果

timeptrが指すカレンダー時間を、formatで指定された変換指定に従って文字列に変換し、sが指す配列に格納する。終端のヌル文字を含めて、最大でmaxsizeバイトを書き込む。

formatには、以下のような変換指定を含めることができる (一部を抜粋):

変換指定 意味
%Y 年 (西暦)
%m 月 (0112)
%d 日 (0131)
%H 時 (24時間表記、0023)
%M 分 (0059)
%S 秒 (0060)
%a 曜日の短縮名 (ロケール依存)
%A 曜日の完全名 (ロケール依存)
%% 文字%

戻り値

終端のヌル文字を含まない、sに書き込んだ文字数を返す。

生成される文字列 (終端のヌル文字を含む) がmaxsizeバイトを超える場合、0を返し、sの内容は不定となる。

#include <ctime>
#include <iostream>

int main()
{
  // 2026年1月1日 12:00:00
  std::tm t{};
  t.tm_year = 2026 - 1900;
  t.tm_mon = 0;
  t.tm_mday = 1;
  t.tm_hour = 12;
  t.tm_min = 0;
  t.tm_sec = 0;

  char buf[64];
  std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &t);
  std::cout << buf << std::endl;
}

出力

2026-01-01 12:00:00

バージョン

言語

  • C++03

関連項目