std:: asctime
|
ヘッダーで定義
<ctime>
|
||
|
char
*
asctime
(
const
std::
tm
*
time_ptr
)
;
|
||
与えられたカレンダー時間 std::tm を以下の固定25文字形式のテキスト表現に変換します: Www Mmm dd hh : mm : ss yyyy\n 。
-
Www- time_ptr - > tm_wday から取得される3文字の英語略記の曜日(Mon、Tue、Wed、Thu、Fri、Sat、Sunのいずれか) -
Mmm- time_ptr - > tm_mon から取得される3文字の英語略記の月名(Jan、Feb、Mar、Apr、May、Jun、Jul、Aug、Sep、Oct、Nov、Decのいずれか) -
dd- timeptr - > tm_mday から取得される月の2桁日付( sprintf で % 2d を使用して出力された場合と同様) -
hh- timeptr - > tm_hour から取得される2桁の時間( sprintf で % .2d を使用して出力された場合と同様) -
mm- timeptr - > tm_min から取得される2桁の分( sprintf で % .2d を使用して出力された場合と同様) -
ss- timeptr - > tm_sec から取得される2桁の秒( sprintf で % .2d を使用して出力された場合と同様) -
yyyy- timeptr - > tm_year + 1900 から取得される4桁の年( sprintf で % 4d を使用して出力された場合と同様)
* time_ptr のいずれかのメンバーが通常の範囲外である場合、動作は未定義です。
動作は未定義です、もしカレンダー年が time_ptr - > tm_year によって示される年が4桁を超えるか、1000年より小さい場合。
この関数はローカライゼーションをサポートしておらず、改行文字を削除することはできません。
この関数は静的ストレージを変更し、スレッドセーフではありません。
目次 |
パラメータ
| time_ptr | - | 出力する時間を指定する std::tm オブジェクトへのポインタ |
戻り値
日付と時刻の文字列表現を保持する、静的でヌル終端された文字列へのポインタ。この文字列は
std::asctime
と
std::ctime
の間で共有される可能性があり、これらの関数のいずれかが呼び出されるたびに上書きされる可能性があります。
注記
この関数は静的データへのポインタを返し、スレッドセーフではありません。POSIXはこの関数を廃止予定とし、ロケール依存の
std::strftime
の使用を推奨しています。
std::locale
("C")
では、
std::strftime
のフォーマット文字列
"%c
\n
"
は
std::asctime
の出力と完全に一致しますが、他のロケールではフォーマット文字列
"%a %b %e %H:%M:%S %Y
\n
"
がより近い(ただし常に正確ではない)一致となる可能性があります。
POSIXは未定義動作を、出力文字列が25文字を超える場合、
timeptr->tm_wday
または
timeptr->tm_mon
が期待される範囲内にない場合、あるいは
timeptr->tm_year
が
INT_MAX
-
1990
を超える場合にのみ制限しています。
一部の実装では、 timeptr - > tm_mday == 0 を前月の最終日を意味するものとして扱います。
例
#include <ctime> #include <iomanip> #include <iostream> int main() { const std::time_t now = std::time(nullptr); for (const char* localeName : {"C", "en_US.utf8", "de_DE.utf8", "ja_JP.utf8"}) { std::cout << "locale " << localeName << ":\n" << std::left; std::locale::global(std::locale(localeName)); std::cout << std::setw(40) << " asctime" << std::asctime(std::localtime(&now)); // strftime output for comparison: char buf[64]; if (strftime(buf, sizeof buf, "%c\n", std::localtime(&now))) std::cout << std::setw(40) << " strftime %c" << buf; if (strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Y\n", std::localtime(&now))) std::cout << std::setw(40) << " strftime %a %b %e %H:%M:%S %Y" << buf; std::cout << '\n'; } }
出力例:
locale C:
asctime Wed Nov 4 00:45:01 2020
strftime %c Wed Nov 4 00:45:01 2020
strftime %a %b %e %H:%M:%S %Y Wed Nov 4 00:45:01 2020
locale en_US.utf8:
asctime Wed Nov 4 00:45:01 2020
strftime %c Wed 04 Nov 2020 12:45:01 AM UTC
strftime %a %b %e %H:%M:%S %Y Wed Nov 4 00:45:01 2020
locale de_DE.utf8:
asctime Wed Nov 4 00:45:01 2020
strftime %c Mi 04 Nov 2020 00:45:01 UTC
strftime %a %b %e %H:%M:%S %Y Mi Nov 4 00:45:01 2020
locale ja_JP.utf8:
asctime Wed Nov 4 00:45:01 2020
strftime %c 2020年11月04日 00時45分01秒
strftime %a %b %e %H:%M:%S %Y 水 11月 4 00:45:01 2020
関連項目
|
std::time_t
オブジェクトをテキスト表現に変換する
(関数) |
|
|
std::tm
オブジェクトをカスタムテキスト表現に変換する
(関数) |
|
|
(C++11)
|
指定されたフォーマットに従って日付/時刻値をフォーマットし出力する
(関数テンプレート) |
|
Cドキュメント
for
asctime
|
|