std::time
来自 cppreference.cn
定义于头文件 <ctime> |
||
std::time_t time( std::time_t* arg ); |
||
返回当前日历时间,其编码为 std::time_t 对象,并将其存储在 arg 指向的对象中,除非 arg 是空指针。
目录 |
[edit] 参数
arg | - | 指向 std::time_t 对象的指针,用于存储时间,或为空指针 |
[edit] 返回值
成功时,返回编码为 std::time_t 对象的当前日历时间;错误时,返回 (std::time_t)(-1)。如果 arg 不为空,则返回值也会存储在 arg 指向的对象中。
[edit] 注解
std::time_t 中日历时间的编码是未指定的,但大多数系统都符合 POSIX 规范,并返回一个整数类型的值,该值等于自 Epoch 以来日历天数的 86400 倍,加上自上次 UTC 午夜以来经过的秒数。最值得注意的是,POSIX 时间不(且不能)将闰秒考虑在内,因此该整数值不等于自 epoch 以来经过的 S.I. 秒数,而是减少了自 epoch 以来发生的闰秒数。在 std::time_t 是 32 位有符号整数(许多历史实现)的实现中,在 2038 年会失败。
[edit] 示例
运行此代码
#include <ctime> #include <iostream> int main() { std::time_t result = std::time(nullptr); std::cout << std::asctime(std::localtime(&result)) << result << " seconds since the Epoch\n"; }
可能的输出
Wed Sep 21 10:27:52 2011 1316615272 seconds since the Epoch
[edit] 参见
(C++17) |
根据给定的时间基准返回以秒和纳秒为单位的日历时间 (函数) |
将自 epoch 以来的时间转换为表示为本地时间的日历时间 (函数) | |
将自 epoch 以来的时间转换为表示为世界协调时间的日历时间 (函数) | |
(C++11) |
来自系统范围实时时钟的挂钟时间 (类) |
C 文档 关于 time
|