命名空间
变体
操作

timespec_get

来自 cppreference.cn
< c‎ | chrono
定义于头文件 <time.h>
int timespec_get( struct timespec *ts, int base );
(1) (自 C11 起)
#define TIME_UTC /* implementation-defined */
(2) (自 C11 起)
1) 修改 timespec 对象,该对象由 ts 指向,以保存时间基准 base 中的当前日历时间。
2) 扩展为适合用作 timespec_getbase 参数的值

实现可以提供其他以 TIME_ 开头的宏常量,以指示其他时间基准

如果 baseTIME_UTC,则

  • ts->tv_sec 被设置为自实现定义的纪元以来的秒数,截断为整数值
  • ts->tv_nsec 成员被设置为纳秒的整数,四舍五入到系统时钟的分辨率

目录

[编辑] 参数

ts - 指向 struct timespec 类型的对象的指针
base - TIME_UTC 或另一个非零整数值,指示时间基准

[编辑] 返回值

如果成功,则返回 base 的值,否则返回零。

[编辑] 注意

POSIX 函数 clock_gettime(CLOCK_REALTIME, ts) 也可用于使用自纪元以来的时间填充 timespec

[编辑] 示例

#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);
}

可能的输出

Current time: 02/18/15 14:34:03.048508855 UTC

[编辑] 参考文献

  • C23 标准 (ISO/IEC 9899:2024)
  • 7.27.2.5 timespec_get 函数 (页码: 待定)
  • C17 标准 (ISO/IEC 9899:2018)
  • 7.27.2.5 timespec_get 函数 (页码: 286)
  • C11 标准 (ISO/IEC 9899:2011)
  • 7.27.2.5 timespec_get 函数 (页码: 390)

[编辑] 参见

以秒和纳秒为单位的时间
(结构体)[编辑]
返回基于给定时间基准的日历时间分辨率
(函数) [编辑]
返回系统当前日历时间,以自纪元以来的时间表示
(函数) [编辑]
C++ 文档 关于 timespec_get