当前位置: C语言 -- 标准库 -- <time.h> -- timespec_getres

timespec_getres函数


概要:
#include <time.h>
int timespec_getres(struct timespec *ts, int base);

描述:

如果参数ts是非空指针,并且timespec_get函数支持base值,timespec_getres函数会在参数ts指向的结构体中返回timespec_get函数对时间基准base所提供的时间分辨率。

对于每个timespec_get函数支持base值,同一次程序执行期间多次调用timespec_getres函数,其结果应相同。


参数:
struct timespec *ts

参数为指向struct timespec类型对象的指针。

int base

参数为宏TIME_UTC或者其它表示时间基准的非0整数值。


返回值:

如果timespec_get函数支持base值,timespec_getres函数返回非0值base;否则函数返回0。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
/*函数timespec_getres范例*/

#include <stdio.h>
#include <time.h>

int main(void)
{
    struct timespec ts = {0, 0};

    timespec_getres(&ts,TIME_UTC);
    printf("Time resolution for TIME_UTC: ");
    printf("%lld s %lld ns\n", (long long)ts.tv_sec, (long long)ts.tv_nsec);

    return 0;
}


结果:

编译时实现还未支持timespec_getres函数。


相关内容:
struct timespec 表示时间的结构类型。
TIME_UTC 表示以UTC时间为基准的整数常量的宏。
timespec_get 设置并保存基于指定时间基准的日历时间的函数。
TIME_MONOTONIC 表示以单调时间为基准的整型常量的宏。
TIME_ACTIVE 表示以总执行时间为基准的整型常量的宏。
TIME_THREAD_ACTIVE 表示以线程特定活动处理时间为基准的整型常量的宏。


主要参考资料:

1、ISO/IEC 9899:2024