linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime

  1. time()提供了秒级的精确度  
  2.   
  3. 1、头文件 <time.h>  
  4. 2、函数原型  
  5. time_t time(time_t * timer)   
  6. 函数返回从TC1970-1-1 0:0:0开始到现在的秒数  
  7.   
  8. 用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。  
  9.  
  10. #include <time.h>  
  11. #include <stdio.h>  
  12. int main(void)  
  13. {  
  14.     time_t t;   
  15.     t = time(NULL);  
  16.     printf("The number of seconds since January 1, 1970 is %ld",t);  
  17.       
  18.     return 0;  
  19. }   
  20.  
  21. #include <stdio.h>  
  22. #include <stddef.h>  
  23. #include <time.h>  
  24. int main(void)  
  25. {  
  26.     time_t timer;//time_t就是long int 类型  
  27.     struct tm *tblock;  
  28.     timer = time(NULL);//这一句也可以改成time(&timer);  
  29.     tblock = localtime(&timer);  
  30.     printf("Local time is: %s/n",asctime(tblock));  
  31.       
  32.     return 0;  
  33. }  


  1. gettimeofday()提供了微秒级的精确度  
  2.   
  3. 1、头文件 <time.h>  
  4. 2、函数原型  
  5. int gettimeofday(struct timeval *tv, struct timezone *tz);   
  6.   
  7. gettimeofday()会把目前的时间由tv所指的结构返回,当地时区的信息则放到tz所指的结构中(可用NULL)。  
  8. 参数说明:  
  9.     timeval结构定义为:  
  10.     struct timeval  
  11.     {  
  12.         long tv_sec; /*秒*/  
  13.         long tv_usec; /*微秒*/  
  14.     };  
  15.     timezone 结构定义为:  
  16.     struct timezone  
  17.     {  
  18.         int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/  
  19.         int tz_dsttime; /*日光节约时间的状态*/  
  20.     };  
  21.     上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下  
  22.         DST_NONE /*不使用*/  
  23.         DST_USA /*美国*/  
  24.         DST_AUST /*澳洲*/  
  25.         DST_WET /*西欧*/  
  26.         DST_MET /*中欧*/  
  27.         DST_EET /*东欧*/  
  28.         DST_CAN /*加拿大*/  
  29.         DST_GB /*大不列颠*/  
  30.         DST_RUM /*罗马尼亚*/  
  31.         DST_TUR /*土耳其*/  
  32.         DST_AUSTALT /*澳洲(1986年以后)*/  
  33.    
  34. 返回值: 成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。  
  35.  
  36. #include<stdio.h>  
  37. #include<time.h>  
  38. int main(void)  
  39. {  
  40.     struct timeval tv;  
  41.     struct timezone tz;  
  42.       
  43.     gettimeofday (&tv , &tz);  
  44.       
  45.     printf(“tv_sec; %d/n”, tv,.tv_sec) ;  
  46.     printf(“tv_usec; %d/n”,tv.tv_usec);  
  47.       
  48.     printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest);  
  49.     printf(“tz_dsttime, %d/n”,tz.tz_dsttime);  
  50.       
  51.     return 0;  
  52. }  

 

  1. clock_gettime( ) 提供了纳秒级的精确度  
  2.   
  3. 1、头文件 <time.h>  
  4. 2、编译&链接。在编译链接时需加上 -lrt ;因为在librt中实现了clock_gettime函数  
  5. 3、函数原型  
  6. int clock_gettime(clockid_t clk_id, struct timespect *tp);  
  7.     参数说明:  
  8.     clockid_t clk_id 用于指定计时时钟的类型,有以下4种:  
  9.         CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变  
  10.         CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响  
  11.         CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间  
  12.         CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间  
  13.     struct timespect *tp用来存储当前的时间,其结构如下:  
  14.         struct timespec  
  15.         {  
  16.             time_t tv_sec; /* seconds */  
  17.             long tv_nsec; /* nanoseconds */  
  18.         };  
  19.     返回值。0成功,-1失败  
  20.  
  21. #include<stdio.h>  
  22. #include<time.h>  
  23. int main()  
  24. {  
  25.     struct timespec ts;  
  26.       
  27.     clock_gettime(CLOCK_REALTIME, &ts);  
  28.     printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec);  
  29.       
  30.     clock_gettime(CLOCK_MONOTONIC, &ts);//打印出来的时间跟 cat /proc/uptime 第一个参数一样  
  31.     printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec);  
  32.       
  33.     clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);  
  34.     printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);  
  35.       
  36.     clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);  
  37.     printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);  
  38.       
  39.     printf("/n%d/n", time(NULL));  
  40.   
  41.     return 0;  
  42. }  
  43. /proc/uptime里面的两个数字分别表示:   
  44. the uptime of the system (seconds), and the amount of time spent in idle process (seconds).   
  45. 把第一个数读出来,那就是从系统启动至今的时间,单位是秒  

 

  1. _ftime()提供毫秒级的精确度  
  2.   
  3. 1、头文件 <sys/types.h> and <sys/timeb.h>   
  4. 2、函数原型  
  5. void _ftime(struct _timeb *timeptr);   
  6. 参数说明:  
  7.     struct _timeb   
  8.     {  
  9.         time_t time;  
  10.         unsigned short millitm;  
  11.         short timezone;  
  12.         short dstflag;  
  13.     };   
  14.  
  15. #include <stdio.h>  
  16. #include <sys/timeb.h>  
  17. #include <time.h>  
  18.   
  19. void main( void )  
  20. {  
  21.     struct _timeb timebuffer;  
  22.     char *timeline;  
  23.   
  24.     _ftime( &timebuffer );  
  25.     timeline = ctime( & ( timebuffer.time ) );  
  26.   
  27.     printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );  

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`gettimeofday()` 和 `clock_gettime()` 都是Unix/Linux系统提供的高级时间获取函数,在C语言中用来获得高精度的当前时间信息,它们通常比`time()`函数提供更好的时钟选择和精度。 ### gettimeofday() ```c #include <sys/time.h> #include <unistd.h> struct timeval tv; // 结构体,存储时间戳信息 // 获取时间戳 if (gettimeofday(&tv, NULL)) { perror("gettimeofday failed"); } else { struct tm *ltm = localtime(&tv.tv_sec); // 将时间戳转换为本地时间结构 printf("Current time: %d-%02d-%02d %02d:%02d:%02d\n", ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec); } ``` `gettimeofday()` 函数返回的是一个包含秒数(`tv_sec`)和微秒数(`tv_usec`)的`struct timeval`结构。 ### clock_gettime() ```c #include <time.h> #include <sys/time.h> clockid_t clk_id; switch (clk_id) { case CLOCK_MONOTONIC: // 持久不变的时钟,适合于计时 case CLOCK_MONOTONIC_RAW: // 类似上面的,但不受干扰 default: break; } struct timespec ts; if (clock_gettime(clk_id, &ts)) { perror("clock_gettime failed"); } else { long long total_seconds = ts.tv_sec + ts.tv_nsec / 1e9; // 时间戳总秒数 printf("Current time in seconds: %lld\n", total_seconds); } ``` `clock_gettime()` 可以指定不同的时钟源(如`CLOCK_REALTIME`, `CLOCK_MONOTONIC`等),其中`CLOCK_MONOTONIC`通常用于无跳变时间,适合于长时间计时,而`CLOCK_REALTIME`会受系统日历调整影响。 注意:这两个函数返回的结果都不保证是连续的,如果需要更高精度且连续计时,推荐使用` monotonic`时钟。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值