std::timespec_get
来自 cppreference.cn
定义于头文件 <ctime> |
||
int timespec_get( std::timespec* ts, int base ); |
(1) | (C++17 起) |
#define TIME_UTC /* implementation-defined */ |
(2) | (C++17 起) |
2) 展开为一个适合用作
std::timespec_get
的 base 参数的值。实现可以提供其他以 TIME_
开头的宏常量以指示额外的时间基准。
如果 base 是 TIME_UTC
,则
- ts->tv_sec 被设置为自实现定义纪元以来的秒数,截断为整数值,
- ts->tv_nsec 成员被设置为纳秒的整数值,四舍五入到系统时钟的分辨率。
目录 |
[编辑] 参数
ts | - | 指向 std::timespec 类型对象的指针 |
base | - | TIME_UTC 或另一个非零整数值,指示时间基准 |
[编辑] 返回值
如果成功则返回 base 的值,否则返回零。
[编辑] 注意
POSIX 函数 clock_gettime(CLOCK_REALTIME, ts)
也可以用来填充一个包含自纪元以来时间的 std::timespec
。
[编辑] 示例
运行此代码
#include <ctime> #include <iostream> int main() { std::timespec ts; std::timespec_get(&ts, TIME_UTC); char buf[100]; std::strftime(buf, sizeof buf, "%D %T", std::gmtime(&ts.tv_sec)); std::cout << "Current time: " << buf << '.' << ts.tv_nsec << " UTC\n"; }
可能的输出
Current time: 06/24/16 20:07:42.949494132 UTC
[编辑] 参阅
(C++17) |
秒和纳秒时间 (结构体) |
返回系统当前时间,作为自纪元以来的时间 (函数) | |
C 文档 关于 timespec_get
|