命名空间
变体
操作

std::ctime

来自 cppreference.cn
< cpp‎ | chrono‎ | c
 
 
 
 
定义于头文件 <ctime>
char* ctime( const std::time_t* time );

将给定的 epoch 时间转换为日历本地时间,然后转换为文本表示,如同调用 std::asctime(std::localtime(time)) 一样。结果字符串具有以下格式

Www Mmm dd hh:mm:ss yyyy\n
  • Www - 星期几(MonTueWedThuFriSatSun 之一)。
  • Mmm - 月份(JanFebMarAprMayJunJulAugSepOctNovDec 之一)。
  • dd - 月份中的日期。
  • hh - 小时。
  • mm - 分钟。
  • ss - 秒。
  • yyyy - 年份。

此函数不支持本地化。

目录

[edit] 参数

time - 指向 std::time_t 对象的指针,指定要打印的时间

[edit] 返回值

指向静态空终止字符字符串的指针,该字符串保存日期和时间的文本表示。该字符串可能在 std::asctimestd::ctime 之间共享,并且可能在每次调用这些函数中的任何一个时被覆盖。

[edit] 注意

此函数返回指向静态数据的指针,并且不是线程安全的。此外,它修改了静态 std::tm 对象,该对象可能与 std::gmtimestd::localtime 共享。POSIX 标记此函数已过时,并建议改用 std::strftime

对于 std::time_t 的值,如果生成的字符串长度超过 25 个字符(例如,年份 10000),则行为可能是未定义的。

[edit] 示例

#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

[edit] 参见

std::tm 对象转换为文本表示
(函数) [编辑]
std::tm 对象转换为自定义文本表示
(函数) [编辑]
(C++11)
根据指定的格式格式化并输出日期/时间值
(函数模板) [编辑]
C 文档 关于 ctime