timespec
|
定义于头文件
<time.h>
|
||
|
struct
timespec
;
|
(C11 起) | |
用于存储按秒和纳秒分解的时间间隔的结构体。
目录 |
成员对象
| 成员 | 描述 |
time_t
tv_sec
|
整秒数(有效值 >= 0 ) |
/* see below */
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 时间组件 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018):
-
- 7.27.1/3 时间组件 (p: 284)
- C11 标准 (ISO/IEC 9899:2011):
-
- 7.27.1/3 时间组件 (p: 388)
参见
|
(C11)
|
基于给定时间基准返回以秒和纳秒表示的日历时间
(函数) |
|
日历时间类型
(结构体) |
|
|
C++ documentation
for
timespec
|
|