1. 程式人生 > >C語言中的time函式總結

C語言中的time函式總結

程式設計中經常用到時間表達及轉換的函式,它們都定義在time.h庫函式中,在此做一下總結,以方便後續檢視使用。

幾個時間概念:

1:Coordinated Universal Time(UTC):
協調世界時,又稱世界標準時間,也即格林威治標準時間(Greenwich Mean Time,GMT),中國內地的時間與UTC得時差為+8,也即UTC+8,美國為UTC-5。

2:Calendar Time:
日曆時間,是用“從一個標準時間點到此時的時間經過的秒數”來表示的時間。標準時間點對不同編譯器可能會不同,但對一個編譯系統來說,標準時間是不變的。一般是表示距離UTC時間 1970-01-01 00:00:00的秒數。
3:epoch:
時間點。在標準c/c++中是一個整數,用此時的時間和標準時間點相差的秒數(即日曆時間)來表示。
4:clock tick:
時鐘計時單元(而不叫做時鐘滴答次數),一個時鐘計時單元的時間長短是由cpu控制的,一個clock tick不是cpu的一個時鐘週期,而是c/c++的一個基本計時單位。

time.h的定義

time.h 標頭檔案定義了四個變數型別、兩個巨集和各種操作日期和時間的函式。

4個變數

size_t 是無符號整數型別,它是 sizeof 關鍵字的結果。
clock_t 這是一個適合儲存處理器時間的型別,型別為unsigned long
time_t 這是一個適合儲存日曆時間型別。
struct tm 這是一個用來儲存時間和日期的結構。

tm 結構的定義如下:

struct tm 
{
   int tm_sec;         /* 秒,範圍從 0 到 59      */
   int tm_min;         /* 分,範圍從 0 到 59      */
int tm_hour; /* 小時,範圍從 0 到 23 */ int tm_mday; /* 一月中的第幾天,範圍從 1 到 31 */ int tm_mon; /* 月,範圍從 0 到 11(注意) */ int tm_year; /* 自 1900 年起的年數 */ int tm_wday; /* 一週中的第幾天,範圍從 0 到 6 */ int tm_yday; /* 一年中的第幾天,範圍從 0 到 365 */ int tm_isdst; /* 夏令時 */
};

兩個巨集

NULL 這個巨集是一個空指標常量的值。
CLOCKS_PER_SEC 這個巨集表示每秒的處理器時鐘個數。用於將clock()函式的結果轉化為以秒為單位的量,這個量的具體值是與作業系統相關的,通常為1000。

庫函式

1:clock函式

函式原型:   clock_t clock(void)
函式返回:返回clock函式執行起(一般為程式的開頭),處理器時鐘所使用的時間。
函式功能:用來計算程式或程式的某一段的執行時間。
#include<stdio.h>
#include<time.h>

int main()
{
    clock_t start_t,finish_t;
    double total_t = 0;
    int i = 0;
    start_t = clock();
    for(;i<100000;++i)
    {
        //do someting;
    }
    finish_t = clock();
    total_t = (double)(finish_t - start_t) / CLOCKS_PER_SEC;//將時間轉換為秒

    printf("CPU 佔用的總時間:%f\n", total_t);
    return 0;
}

2:time函式

函式原型: time_t time(time_t *timer)
引數說明: timer=NULL時得到當前日曆時間(從1970-01-01 00:00:00到現在的秒數),timer=時間數值時,用於設定日曆時間,time_t是一個unsigned long型別。如果 timer不為空,則返回值也儲存在變數 timer中。
函式功能: 得到當前日曆時間或者設定日曆時間
函式返回: 當前日曆時間
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t seconds;

  seconds = time(NULL);
  printf("自 1970-01-01 起的小時數 = %ld\n", seconds/3600);

  return(0);
}

3:asctime函式

函式原型: char* asctime(struct tm * ptr)
函式功能:將結構struct tm * ptr所表示的時間以字串表示
函式返回: 返回的時間字串格式為:星期,月,日,小時:分:秒,年
引數說明: 結構指標ptr應通過函式localtime()或gmtime()得到
#include <stdio.h>
#include <time.h>

int main()
{
   struct tm t;//更多情況下是通過localtime函式及gmtime函式獲得tm結構

   t.tm_sec    = 10;
   t.tm_min    = 10;
   t.tm_hour   = 6;
   t.tm_mday   = 25;
   t.tm_mon    = 2;
   t.tm_year   = 89;
   t.tm_wday   = 6;

   printf("%s\n",asctime(&t));

   return(0);
}

4:localtime函式

函式原型: struct tm *localtime(const time_t *timer)
函式功能: 使用 timer 的值來填充 tm 結構。timer 的值被分解為 tm 結構,並用本地時區表示。
函式返回: 以tm結構表達的時間
#include <stdio.h>
#include <time.h>

int main ()
{
   time_t timer;
   struct tm *Now;

   time( &timer );
   Now = localtime( &timer );
   printf("當前的本地時間和日期:%s", asctime(Now));

   return(0);
}

5:ctime函式

函式原型: char *ctime(const time_t * timer)
函式功能: 將日曆時間引數timer轉換為一個表示本地當前時間的字串
函式返回: 返回字串格式:星期,月,日,小時:分:秒,年
引數說明: timer引數應由函式time獲得,其等價於 astime( localtime(timer) )
#include <stdio.h>
#include <time.h>

int main ()
{
   time_t curtime;
   time(&curtime);
   printf("當前時間 = %s", ctime(&curtime));

   return(0);
}

6:difftime函式

函式原型: double difftime(time_t time2, time_t time1)
函式功能: 得到兩次機器時間差,單位為秒
函式返回: 時間差,單位為秒
引數說明: time1,time2分別表示兩個不同的機器時間,該引數應使用time函式獲得
#include <time.h>  
#include <stdio.h>  
int main()  
{  
    time_t first,second;  
    time(&first);  
    sleep(2000);  
    time(&second); 
    printf("The difference is: %f seconds",difftime(second,first));  

    return 0;  
}  

7:gmtime函式

函式原型: struct tm *gmtime(time_t *timer)
函式功能: 得到以結構tm表示的時間資訊,並用格林威治標準時間表示
函式返回: 以結構tm表示的時間資訊指標
引數說明: timer用函式time()得到的時間資訊
#include <stdio.h>
#include <time.h>

#define BST (+1)
#define CCT (+8)

int main ()
{

   time_t rawtime;
   struct tm *info;

   time(&rawtime);
   /* 獲取 GMT 時間 */
   info = gmtime(&rawtime );

   printf("當前的世界時鐘:\n");
   printf("倫敦:%2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min);
   printf("中國:%2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min);

   return(0);
}

8:mktime函式

函式原型:time_t mktime(struct tm *timeptr)
函式功能:把 timeptr 所指向的結構轉換為一個依據本地時區的 time_t 值
函式返回:該函式返回一個 time_t 值,該值對應於以引數傳遞的日曆時間。如果發生錯誤,則返回 -1 值。
#include <stdio.h>
#include <time.h>

int main ()
{
   int ret;
   struct tm info;
   char buffer[80];

   info.tm_year = 2001 - 1900;
   info.tm_mon = 7 - 1;
   info.tm_mday = 4;
   info.tm_hour = 0;
   info.tm_min = 0;
   info.tm_sec = 1;
   info.tm_isdst = -1;

   ret = mktime(&info);
   if( ret == -1 )
   {
       printf("錯誤:不能使用 mktime 轉換時間。\n");
   }
   else
   {
      strftime(buffer, sizeof(buffer), "%c", &info );
      print(buffer);
   }

   return(0);
}

9:strftime函式

函式原型: size_t strftime( char *strDest, size_t  maxsize, const char *format, const struct tm  *timeptr);
函式功能: 根據format指向字串中格式命令把timeptr中儲存的時間資訊放在strDest指向的字串中,最多向 strDest中存放maxsize個字元。
引數說明: 轉化結果存在s中,最多maxsize個字元寫到s中
函式返回: 該函式返回向strDest指向的字串中放置的字元數(不包括'\0'),如果字元數多於maxsize,函式返回0.
/*format如下:它們是區分大小寫的。 
    %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 時區名稱,如果不能得到時區名稱則返回空字元。 
    %% 百分號 
*/  

#include <stdio.h>
#include <time.h>

int main ()
{
   time_t rawtime;
   struct tm *info;
   char buffer[80];

   time( &rawtime );

   info = localtime( &rawtime );

   strftime(buffer,80,"%Y%m%e_%H%M%S", info);//以年月日_時分秒的形式表示當前時間
   printf("%s\n", buffer );

   return(0);
}