timespec
来自 cppreference.cn
定义于头文件 <time.h> |
||
struct timespec; |
(自 C11 起) | |
结构体,用于保存分解为秒和纳秒的时间间隔。
内容 |
[编辑] 成员对象
成员 | 描述 |
time_t tv_sec |
完整秒数 (有效值 >= 0) |
/* 见下方 */ tv_nsec |
纳秒 (有效值范围 [ 0, 999999999] ) |
|
(直到 C23) |
|
(自 C23 起) |
tv_sec
和 tv_nsec
的声明顺序未指定。实现可以向 struct timespec 添加其他成员。
[编辑] 注释
在某些平台上,tv_nsec
的类型是 long long,自 C23 起才符合标准。
[编辑] 示例
运行此代码
#include <stdint.h> #include <stdio.h> #include <time.h> int main(void) { struct timespec ts; timespec_get(&ts, TIME_UTC); char buff[100]; strftime(buff, sizeof buff, "%D %T", gmtime(&ts.tv_sec)); printf("Current time: %s.%09ld UTC\n", buff, ts.tv_nsec); printf("Raw timespec.tv_sec: %jd\n", (intmax_t)ts.tv_sec); printf("Raw timespec.tv_nsec: %09ld\n", ts.tv_nsec); }
可能的输出
Current time: 04/04/24 14:45:17.885909786 UTC Raw timespec.tv_sec: 1712241917 Raw timespec.tv_nsec: 885909786
[编辑] 参考文献
- C23 标准 (ISO/IEC 9899:2024)
- 7.27.1/3 时间组成部分 (页码: 待定)
- C17 标准 (ISO/IEC 9899:2018)
- 7.27.1/3 时间组成部分 (页码: 284)
- C11 标准 (ISO/IEC 9899:2011)
- 7.27.1/3 时间组成部分 (页码: 388)
[编辑] 参见
(C11) |
基于给定的时间基准返回日历时间的秒和纳秒表示 (函数) |
日历时间类型 (结构体) | |
C++ 文档 关于 timespec
|