您的位置:首页 > 其它

clock_gettime获取时间

2017-11-04 14:20 85 查看
            linux下clock_getting可以用来获取时间并精度到纳秒,其中需要用到struct timespec结构体,struct timespec结构体如下:

struct timespec
{
time_t tv_sec;
long tv_nsec;
}


写个小程序测试一下:
#include<stdio.h>
#include<time.h>
#include<sys/time.h>

void test_time()
{
struct timespec time;
clock_gettime(CLOCK_REALTIME,&time);
printf("tv_sec=%ld,tv_nsec=%ld\n",time.tv_sec,time.tv_nsec);
}

int main()
{
test_time();

return 0;
}

编译运行:

[mapan@localhost thread]$ gcc -lrt time1.c
[mapan@localhost thread]$ ./a.out
tv_sec=1509774203,tv_nsec=413767266
[mapan@localhost thread]$


linux下还有其他的时间结构体,但是struct timespec可以精确到纳秒。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐