std::ctime
来自 cppreference.cn
| 定义于头文件 <ctime> |
||
| char* ctime( const std::time_t* time ); |
||
将给定自纪元以来的时间转换为日历本地时间,然后转换为文本表示,如同通过调用 std::asctime(std::localtime(time))。结果字符串具有以下格式:
Www Mmm dd hh:mm:ss yyyy\n
-
Www- 星期几(Mon,Tue,Wed,Thu,Fri,Sat,Sun之一)。 -
Mmm- 月份(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec之一)。 -
dd- 月份中的日期。 -
hh- 小时。 -
mm- 分钟。 -
ss- 秒。 -
yyyy- 年份。
该函数不支持本地化。
目录 |
[编辑] 参数
| time | - | 指向 std::time_t 对象的指针,指定要打印的时间 |
[编辑] 返回值
指向一个静态的以 null 结尾的字符字符串的指针,该字符串包含日期和时间的文本表示。该字符串可能在 std::asctime 和 std::ctime 之间共享,并且可能在这些函数每次调用时被覆盖。
[编辑] 注意
此函数返回指向静态数据的指针,并且不是线程安全的。此外,它会修改静态的 std::tm 对象,该对象可能与 std::gmtime 和 std::localtime 共享。POSIX 将此函数标记为已过时,并推荐使用 std::strftime。
对于导致字符串长度超过 25 个字符的 std::time_t 值(例如,年份 10000),行为可能是未定义的。
[编辑] 示例
运行此代码
#include <cassert> #include <cstring> #include <ctime> #include <iostream> int main() { std::time_t result = std::time(nullptr); std::cout << std::ctime(&result); char buffer[32]; std::strncpy(buffer, std::ctime(&result), 26); assert('\n' == buffer[std::strlen(buffer) - 1]); std::cout << buffer; }
可能的输出
Mon Oct 11 17:10:55 2021 Mon Oct 11 17:10:55 2021
[编辑] 参阅
| 将 std::tm 对象转换为文本表示 (函数) | |
| 将 std::tm 对象转换为自定义文本表示 (函数) | |
| (C++11) |
根据指定格式格式化并输出日期/时间值 (函数模板) |
| C 文档 关于 ctime
| |