std::timespec
来自 cppreference.cn
定义于头文件 <ctime> |
||
struct timespec; |
(C++17 起) | |
一个结构体,用于存储分解为秒和纳秒的时间间隔。
目录 |
[编辑] 数据成员
成员 | 描述 |
std::time_t tv_sec |
整秒数,值 >= 0 (公有成员对象) |
long tv_nsec |
纳秒数,值在范围 [ 0, 999999999] (公有成员对象) |
tv_sec
和 tv_nsec
的声明顺序未指定。实现可以向 timespec
添加其他数据成员。
[编辑] 注意
在某些平台上,tv_nsec
的类型是 long long,这在 C++ 中目前是不符合规范的,但在 C23 中,C 语言允许这样做。
[编辑] 示例
运行此代码
#include <ctime> #include <iostream> int main() { std::timespec ts; std::timespec_get(&ts, TIME_UTC); char buff[0x80]; std::strftime(buff, sizeof buff, "%D %T", std::gmtime(&ts.tv_sec)); // auto [sec, nsec] = ts; // UB: structured bindings should not be used because the // declaration order and data member list are unspecified std::cout << "Current time: " << buff << " (UTC)\n" << "Raw timespec.tv_sec: " << ts.tv_sec << '\n' << "Raw timespec.tv_nsec: " << ts.tv_nsec << '\n'; }
可能的输出
Current time: 04/06/23 12:03:31 (UTC) Raw timespec.tv_sec: 1680782611 Raw timespec.tv_nsec: 678437213
[编辑] 参阅
(C++17) |
根据给定时间基准返回以秒和纳秒表示的日历时间 (函数) |
日历时间类型 (类) | |
C 文档 关于 timespec
|