1. 程式人生 > >linux下常用的幾個時間函數:time,gettimeofday,clock_gettime,_ftime

linux下常用的幾個時間函數:time,gettimeofday,clock_gettime,_ftime

turn mon nan start 存儲 mono star 結合 eva

  1. time()提供了秒級的精確度
  2. 1、頭文件 <time.h>
  3. 2、函數原型
  4. time_t time(time_t * timer)
  5. 函數返回從TC1970-1-1 0:0:0開始到現在的秒數
  6. 用time()函數結合其他函數(如:localtime、gmtime、asctime、ctime)可以獲得當前系統時間或是標準時間。
  7. #include <time.h>
  8. #include <stdio.h>
  9. int main(void)
  10. {
  11. time_t t;
  12. t = time(NULL);
  13. printf("The number of seconds since January 1, 1970 is %ld",t);
  14. return 0;
  15. }
  16. #include <stdio.h>
  17. #include <stddef.h>
  18. #include <time.h>
  19. int main(void)
  20. {
  21. time_t timer;//time_t就是long int 類型
  22. struct tm *tblock;
  23. timer = time(NULL);//這一句也可以改成time(&timer);
  24. tblock = localtime(&timer);
  25. printf("Local time is: %s/n",asctime(tblock));
  26. return 0;
  27. }


  1. gettimeofday()提供了微秒級的精確度
  2. 1、頭文件 <time.h>
  3. 2、函數原型
  4. int gettimeofday(struct timeval *tv, struct timezone *tz);
  5. gettimeofday()會把目前的時間由tv所指的結構返回,當地時區的信息則放到tz所指的結構中(可用NULL)。
  6. 參數說明:
  7. timeval結構定義為:
  8. struct timeval
  9. {
  10. long tv_sec; /*秒*/
  11. long tv_usec; /*微秒*/
  12. };
  13. timezone 結構定義為:
  14. struct timezone
  15. {
  16. int tz_minuteswest; /*和Greenwich 時間差了多少分鐘*/
  17. int tz_dsttime; /*日光節約時間的狀態*/
  18. };
  19. 上述兩個結構都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態如下
  20. DST_NONE /*不使用*/
  21. DST_USA /*美國*/
  22. DST_AUST /*澳洲*/
  23. DST_WET /*西歐*/
  24. DST_MET /*中歐*/
  25. DST_EET /*東歐*/
  26. DST_CAN /*加拿大*/
  27. DST_GB /*大不列顛*/
  28. DST_RUM /*羅馬尼亞*/
  29. DST_TUR /*土耳其*/
  30. DST_AUSTALT /*澳洲(1986年以後)*/
  31. 返回值: 成功則返回0,失敗返回-1,錯誤代碼存於errno。附加說明EFAULT指針tv和tz所指的內存空間超出存取權限。
  32. #include<stdio.h>
  33. #include<time.h>
  34. int main(void)
  35. {
  36. struct timeval tv;
  37. struct timezone tz;
  38. gettimeofday (&tv , &tz);
  39. printf(“tv_sec; %d/n”, tv,.tv_sec) ;
  40. printf(“tv_usec; %d/n”,tv.tv_usec);
  41. printf(“tz_minuteswest; %d/n”, tz.tz_minuteswest);
  42. printf(“tz_dsttime, %d/n”,tz.tz_dsttime);
  43. return 0;
  44. }

  1. clock_gettime( ) 提供了納秒級的精確度
  2. 1、頭文件 <time.h>
  3. 2、編譯&鏈接。在編譯鏈接時需加上 -lrt ;因為在librt中實現了clock_gettime函數
  4. 3、函數原型
  5. int clock_gettime(clockid_t clk_id, struct timespect *tp);
  6. 參數說明:
  7. clockid_t clk_id 用於指定計時時鐘的類型,有以下4種:
  8. CLOCK_REALTIME:系統實時時間,隨系統實時時間改變而改變,即從UTC1970-1-1 0:0:0開始計時,中間時刻如果系統時間被用戶該成其他,則對應的時間相應改變
  9. CLOCK_MONOTONIC:從系統啟動這一刻起開始計時,不受系統時間被用戶改變的影響
  10. CLOCK_PROCESS_CPUTIME_ID:本進程到當前代碼系統CPU花費的時間
  11. CLOCK_THREAD_CPUTIME_ID:本線程到當前代碼系統CPU花費的時間
  12. struct timespect *tp用來存儲當前的時間,其結構如下:
  13. struct timespec
  14. {
  15. time_t tv_sec; /* seconds */
  16. long tv_nsec; /* nanoseconds */
  17. };
  18. 返回值。0成功,-1失敗
  19. #include<stdio.h>
  20. #include<time.h>
  21. int main()
  22. {
  23. struct timespec ts;
  24. clock_gettime(CLOCK_REALTIME, &ts);
  25. printf("CLOCK_REALTIME: %d, %d", ts.tv_sec, ts.tv_nsec);
  26. clock_gettime(CLOCK_MONOTONIC, &ts);//打印出來的時間跟 cat /proc/uptime 第一個參數一樣
  27. printf("CLOCK_MONOTONIC: %d, %d", ts.tv_sec, ts.tv_nsec);
  28. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  29. printf("CLOCK_PROCESS_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);
  30. clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
  31. printf("CLOCK_THREAD_CPUTIME_ID: %d, %d", ts.tv_sec, ts.tv_nsec);
  32. printf("/n%d/n", time(NULL));
  33. return 0;
  34. }
  35. /proc/uptime裏面的兩個數字分別表示:
  36. the uptime of the system (seconds), and the amount of time spent in idle process (seconds).
  37. 把第一個數讀出來,那就是從系統啟動至今的時間,單位是秒

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

linux下常用的幾個時間函數:time,gettimeofday,clock_gettime,_ftime