天天看點

C程式中對時間的處理——time庫函數詳解以及系統時間結構體類型

包含檔案:<sys/time.h> <time.h> 一、在C語言中有time_t, tm, timeval等幾種類型的時間 1、time_t     長整型,一般用來表示從1970-01-0100:00:00時以來的秒數, 精确度:秒;由函數time()擷取;

    該類型定義在頭檔案/usr/include/sys/time.h 中:

    #define _TIME_T

      typedef  long  time_t;        

    #endif 

     函數定義:time_t  time(time_t*  lpt);

    如:time_t   time = time(NULL);

2、timeval      它有兩個成員;一個是秒,另一個表示微秒, 精确度:微秒(10E-6);

     由函數gettime0fday()擷取;

      struct timeval結構定義為:

     struct  timeval

     {

         long tv_sec;    

         long tv_usec;  

     }

     讀取struct timeval結構資料的函數說明:

     #include <sys/time.h>

     int  gettimeofday(struct timeval*  tv,struct timezone*  tz);

     該函數會提取系統目前時間,并把時間分為秒和微秒兩部分填充到結構struct timeval中;同時把當地的時區信

     息填充到結構struct  timezone中;

      傳回值:成功則傳回0,失敗傳回-1,錯誤代碼存于errno。附加說明EFAULT指針tv和tz所指的記憶體空間超出存

     取權限。

     struct  timezone結構的定義為:

     struct  timezone

     {

        int  tz_minuteswest; 

        int  tz_dsttime;         

     }

    上述兩個結構都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀态如下

     DST_NONE

     DST_USA

     DST_AUST

     DST_WET

     DST_MET  

      DST_EET

     DST_CAN

     DST_GB

     DST_RUM

     DST_TUR

     DST_AUSTALT

  3、tm tm是一個結構體,定義為: struct tm

{

    int tm_sec;           int tm_min;         int tm_hour;       int tm_mday;       int tm_mon;       int tm_year;       int tm_wday;       int tm_yday;  

    int tm_isdst;   };

4、struct timeb結構:它有兩個主要成員,一個是秒,另一個是毫秒;精确度:毫秒(10E-3秒);

     由函數ftime()擷取structtimeb結構的時間;其定義如下:

     structtimeb

     {

       time_t time;                    

       unsigned shortmillitm;    

       short  timezone;             

       short  dstflag;                

     };

     #include <sys/timeb.h>

     int  ftime(struct timeb*  tp);

     調用成功傳回0;調用失敗傳回-1;

5、struct  timespec結構:它是POSIX.4标準定義的一個時間結構,精确度:納秒(10E-9秒);

     由函數gethrestime()或gethrestime_lasttick()擷取目前系統struct timespec結構的時間;其定義如下:

     struct timespec              

     {

         time_t   tv_sec;           

         long      tv_nsec;         

     };

    typedef  struct timespec   timespec_t;

    該結構定義在頭頭檔案 /usr/include/sys/time_impl.h 中;

    extern  void   gethrestime(timespec_t*);

    extern   void  gethrestime_lasttick(timespec_t*);

6、clock_t 類型:由函數clock()擷取;

    #include <time.h>

     clock_t   clock(void);

    該函數以微秒的方式傳回CPU的時間;

    類型 clock_t 定義在頭檔案/usr/include/sys/types.h中:

    #ifndef _CLOCK_T

    #define _CLOCK_T

     typedef   long  clock_t;

    #endif 

7、Unix對時間機關的定義:

    #defineSEC                1                       // 秒

    #defineMILLISEC       1000                 // 毫秒

    #defineMICROSEC     1000000           // 微秒

    #defineNANOSEC       1000000000     // 納秒

二、具體操作函數

time()函數   原 型:time_t time(time_t * timer)   功 能:   擷取目前的系統時間,傳回的結果是一個time_t類型,其實就是一個大整數,其值表示從CUT(Coordinated Universal Time)時間1970年1月1日00:00:00(稱為UNIX系統的Epoch時間)到目前時刻的秒數。然後調用localtime将time_t所表示的CUT時間轉換為本地時間(我們是+8區,比CUT多8個小時)并轉成struct tm類型,該類型的各資料成員分别表示年月日時分秒。  程式例1:   time函數獲得月曆時間。 月曆時間,是用“從一個标準時間點到此時的時間經過的秒數”來表示的時間。這個标準時間點對不同的編譯器來說會有所不同,但對一個編譯系統來說,這個标準時間點是不變的,該編譯系統中的時間對應的月曆時間都通過該标準時間點來衡量,是以可以說月曆時間是“相對時間”,但是無論你在哪一個時區,在同一時刻對同一個标準時間點來說,月曆時間都是一樣的。   #include < time.h>   #include <stdio.h>   #include < dos.h>   int main(void)   {   time_t t; t = time(NULL);   printf("The number of seconds since January 1, 1970 is %ld",t);   return 0;   }  程式例2:   //time函數也常用于 随機數的生成,用月曆時間作為種子。   #include <stdio.h>   #include <time.h>   #include< stdlib.h>   int main(void)   {   int i;    srand((unsigned) time(NULL));   printf("ten random numbers from 0 to 99\n\n");   for(i=0;i<10;i++)    printf("%d\n",rand()%100);   return 0;   }  程式例3:   用time()函數結合其他函數(如:localtime、 gmtime、 asctime、 ctime)可以 獲得目前系統時間或是标準時間。   #include <stdio.h>   #include < stddef.h>   #include <time.h>   int main(void)   {   time_t timer;//time_t就是long int 類型   struct tm *tblock;   timer = time(NULL);//這一句也可以改成time(&timer);   tblock = localtime(&timer);   printf("Local time is: %s\n",asctime(tblock));   return 0;   }   gmtime()函數   原 型:struct tm *gmtime(long *clock);   功 能:把日期和時間轉換為格林威治(GMT)時間的函數。将參數timep 所指的time_t 結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後将結果由結構tm傳回

        說 明:此函數傳回的時間日期未經時區轉換,而是UTC時間。

        傳回值:傳回結構tm代表目前UTC 時間

        程式例

      #include "stdio.h"

  #include "time.h"

  #include "stdlib.h"

  int main(void)

  {

  time_t t;

  struct tm *gmt, *area;

  tzset();

  t = time(NULL);

  area = localtime(&t);

  printf("Local time is: %s", asctime(area));

  gmt = gmtime(&t);

  printf("GMT is: %s", asctime(gmt));

  return 0;

  }

localtime()函數   功 能: 把從1970-1-1零點零分到目前時間系統所偏移的秒數時間轉換為月曆時間 。   說 明:此函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間。   用 法:   struct tm *localtime(const time_t *clock);   傳回值:傳回指向tm 結構體的指針.tm結構體是time.h中定義的用于分别存儲時間的各個量(年月日等)

的結構體.

  程式例1:   #include <stdio.h>   #include <stddef.h>   #include <time.h>   int main(void)   {   time_t timer;//time_t就是long int 類型   struct tm *tblock;   timer = time(NULL);   tblock = localtime(&timer);   printf("Local time is: %s\n",asctime(tblock));   return 0;   }   執行結果:   Local time is: Mon Feb 16 11:29:26 2009 程式例2:   上面的例子用了asctime函數,下面這個例子不使用這個函數一樣能擷取系統目前時間。

        需要注意的是年份加上1900,月份加上1。   #include<time.h>   #include<stdio.h>   int main()   {   struct tm *t;   time_t tt;   time(&tt);   t=localtime(&tt);   printf("%4d年%02d月%02d日 %02d:%02d:%02d\n",

               t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);   return 0;   }   localtime()和gmtime()的差別:   gmtime()函數功能類似擷取目前系統時間,隻是擷取的時間未經過時區轉換。   localtime函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間。   localtime_r()和 gmtime_r()函數

  struct tm * gmtime_r (const time_t *timep, struct tm *result);  

  struct tm *localtime_r(const time_t *timep, struct tm *result);   gmtime_r()函數功能與此相同,但是它可以将資料存儲到使用者提供的結構體中。   localtime_r()函數功能與此相同,但是它可以将資料存儲到使用者提供的結構體中。它不需要設定tzname。   使用gmtime和localtime後要立即處理結果,否則傳回的指針指向的内容可能會被覆寫。

       一個好的方法是使用gmtime_r和localtime_r,由于使用了使用者配置設定的記憶體,這兩個函數是不會出錯的。   asctime()函數    功 能: 轉換日期和時間為相應的字元串(英文簡寫形式,形如: Mon Feb 16 11:29:26 2009)   用 法: char *asctime(const struct tm *tblock);   ctime()函數   功 能: 把日期和時間轉換為字元串。 (英文簡寫形式,形如: Mon Feb 16 11:29:26 2009)   用 法: char *ctime(const time_t *time);   說 明:ctime同asctime的差別在于,ctime是通過月曆時間來生成時間字元串,

                  而asctime是通過tm結構來生成時間字元串。   mktime()函數

   功 能:将tm時間結構資料轉換成經過的秒數(月曆時間)。   原 型: time_t mktime(strcut tm * timeptr);。   說 明:mktime()用來将參數timeptr所指的tm結構資料轉換成

                   從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。   傳回值:傳回經過的秒數。   difftime()函數   功 能:計算時間間隔才長度,以秒為機關,且隻能精确到秒。   原 型:double difftime(time_t time1, time_t time0);   說 明:雖然該函數傳回值是double類型的,但這并不說明該時間間隔具有同double一樣的精度,

                   這是由它的參數決定的。   strftime()函數   功 能:将時間格式化,或者說:格式化一個時間字元串。我們可以使用strftime()函數将時間格式化為我們想要的格式。   原 型: size_t strftime(char *strDest,size_t maxsize,const char *format,const struct tm *timeptr);   參 數:我們可以根據format指向字元串中格式指令把timeptr中儲存的時間資訊放在strDest指向的字元串中,

                   最多向strDest中存放maxsize個字元。   傳回值:該函數傳回向strDest指向的字元串中放置的字元數。   類似于sprintf():識别以百分号(%)開始的格式指令集合,格式化輸出結果放在一個字元串中。

                    格式化指令說明串strDest中各種日期和時間資訊的确切表示方法。格式串中的其他字元原樣放進串中。

                    格式指令列在下面,它們是區分大小寫的。   %a 星期幾的簡寫   %A 星期幾的全稱   %b 月份的簡寫   %B 月份的全稱   %c 标準的日期的時間串   %C 年份的後兩位數字   %d 十進制表示的每月的第幾天   %D 月/天/年   %e 在兩字元域中,十進制表示的每月的第幾天   %F 年-月-日   %g 年份的後兩位數字,使用基于周的年   %G 年份,使用基于周的年   %h 簡寫的月份名   %H 24小時制的小時   %I 12小時制的小時   %j 十進制表示的每年的第幾天   %m 十進制表示的月份   %M 十時制表示的分鐘數   %n 新行符   %p 本地的AM或PM的等價顯示   %r 12小時的時間   %R 顯示小時和分鐘:hh:mm   %S 十進制的秒數   %t 水準制表符   %T 顯示時分秒:hh:mm:ss   %u 每周的第幾天,星期一為第一天 (值從0到6,星期一為0)   %U 第年的第幾周,把星期日作為第一天(值從0到53)   %V 每年的第幾周,使用基于周的年   %w 十進制表示的星期幾(值從0到6,星期天為0)   %W 每年的第幾周,把星期一做為第一天(值從0到53)   %x 标準的日期串   %X 标準的時間串   %y 不帶世紀的十進制年份(值從0到99)   %Y 帶世紀部分的十制年份   %z,%Z 時區名稱,如果不能得到時區名稱則傳回空字元。   %% 百分号   提示:與 gmstrftime() 的行為相同,不同的是傳回時間是本地時間。

繼續閱讀