std::ctime

定义于头文件 <ctime>
char* ctime( const std::time_t* time );

转换给定的从纪元起时间为日历时间,再转换为文本表示,如同通过调用 std::asctime(std::localtime(time)) 。 产生的字符串拥有如下格式:

Www Mmm dd hh:mm:ss yyyy\n

函数不支持本地化。

目录

参数

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

返回值

返回指向静态空终止字符串的指针,字符串保有上述的日期和时间的文本表示。字符串可以在 std::asctimestd::ctime 间共享,并且可以被每次对这些函数的调用重写。

注意

ctime 返回指向静态数据的指针,而且非线程安全。另外它修改可能会为 std::gmtimestd::localtime 所共享的静态 std::tm 对象。 POSIX 标记此函数标为过时,并推荐用 std::strftime 替代。

对于导致字符串长于 25 字符(例如, 10000 年)的 time_t 的值, ctime 的行为可能未定义。

示例

#include <ctime>
#include <iostream>
 
int main()
{
    std::time_t result = std::time(nullptr);
    std::cout << std::ctime(&result);
}

输出:

Tue Dec 27 17:21:29 2011

参阅

转换 tm 对象为文本表示
(函数)
转换 tm 对象到自定义的文本表示
(函数)
(C++11)
按照指定格式格式化并输出日期/时间值
(函数模板)
ctimeC 文档

版本历史

  • (当前 | 先前 2017年10月10日 (二) 23:14Fruderica讨论 | 贡献. . (2,028字节) (-1,589). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:39P12bot讨论 | 贡献 . . (3,617字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 08:19P12bot讨论 | 贡献 . . (3,617字节) (-74). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:52P12bot讨论 | 贡献 . . (3,691字节) (+130). . (r2.7.3) (机器人添加:de, en, es, it, pt, ru 去除:zh) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 10:49P12讨论 | 贡献 . . (3,561字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 07:00TranslationBot讨论 | 贡献. . (3,561字节) (-101). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:14P12讨论 | 贡献 . . (3,662字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 13:00TranslationBot讨论 | 贡献. . (3,662字节) (+2,925). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年5月4日 (五) 12:19P12bot讨论 | 贡献 . . (737字节) (+79). . (r2.7.3) (机器人添加:en:cpp/chrono/c/ctime, fr:cpp/chrono/c/ctime, ja:cpp/chrono/c/ctime (撤销)
  • 当前 | 先前 2012年5月1日 (二) 04:36P12讨论 | 贡献 . . (658字节) (0). . (1个修订: Import from Dokuwiki) (撤销)
  • 当前 | 先前) 2012年5月1日 (二) 04:36P12讨论 | 贡献. . (658字节) (+658). . (Import from dokuwiki)