time,gettimeofday,clock_gettime,_ftime

来源:互联网 发布:网络协议的端口号 编辑:程序博客网 时间:2024/04/28 18:06
  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;  

  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;  

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

    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
    原创粉丝点击