Namespaces
Variants

timespec_getres

From cppreference.net
定义于头文件 <time.h>
int timespec_getres ( struct timespec * ts, int base ) ;
(C23 起)

如果 ts 非空且 base timespec_get 支持,则修改 * ts 以保存 timespec_get base 提供的时间分辨率。对于每个支持的 base ,在同一程序执行期间对 timespec_getres 的多次调用将产生相同结果。

目录

参数

ts - 指向 struct timespec 类型对象的指针
base - TIME_UTC 或表示时间基准的其他非零整数值

返回值

如果支持 base ,则返回 base 的值,否则返回零。

注释

POSIX函数 clock_getres(clock_id, ts) 也可用于将 timespec 结构填充为由 clock_id 标识的时间精度值。

示例

#include <stdio.h>
#include <time.h>
int main(void)
{
    char buff[128];
    struct timespec ts;
    const int res = timespec_getres(&ts, TIME_UTC);
    if (res == TIME_UTC) {
        struct tm timer;
        strftime(buff, sizeof buff, "%D %T", gmtime_r(&ts.tv_sec, &timer));
        printf("Time resolution info: %s.%09ld UTC\n", buff, ts.tv_nsec);
    } else {
        printf("TIME_UTC base is not supported.");
    }
}

可能的输出:

Time resolution info: 01/01/70 00:00:00.000000001 UTC

参见

以秒和纳秒表示的时间
(结构体)
基于给定时间基准返回以秒和纳秒表示的日历时间
(函数)
返回系统当前日历时间(自纪元起算的时间)
(函数)