time,gettimeofday,clock_gettime,_ftime

http://blog.csdn.net/sunlylorn/article/details/6313278

time,gettimeofday,clock_gettime,_ftime


[c-sharp] view plain copy print ?
  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. }  

 

[c-sharp] view plain copy print ?
  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. }  

 

[c-sharp] view plain copy print ?
  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. 把第一个数读出来,那就是从系统启动至今的时间,单位是秒  

 

[c-sharp] view plain copy print ?
  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] );  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值