#include 庫函數詳解

本文轉自 http://blog.csdn.net/chenyiming_1990/article/details/8682552

time.h 
有人總結成這麼幾句,的確是經典,自己好好編程試試效果吧,

兩個類型:

time_t:表示距離 UTC 時間 1970-01-01 00:00:00 的秒數。也叫做日曆時,類型是  long
clock_t: 只用於程序計時,貌似其他的沒它什麼事。
struct tm:通常用於存儲本地時。

 

常用函數

clock: 獲取程序開始執行後佔用的處理器時間,返回值clock_t。

time:獲取當前系統時間(UTC時間)的time_t值。

ctime:將time_t值轉換爲表示本地時間的字符串。

gmttime:將time_t值轉換爲表示GMT時間的字符串。

localtime:將time_t轉換爲表示本地時間的strunct tm結構。

mktime:將表示本地時間的struct tm轉換爲time_t。

asctime:將struct tm轉換爲字符串形式。

difftime:得到兩個日曆時之間的差值。

strftime:自定義把結構體tm的日期與時間信息轉換爲制定的格式。

========================================================================

@函數名稱: clock

函數原型: clock_t clcok()
函數功能: 獲取程序開始執行後佔用的處理器時間
函數返回: 一般 返回值/CLOCK_PER_SEC 來已秒來表示時間

@函數名稱: localtime

函數原型: struct tm *localtime(const time_t *timer)
函數功能: 返回一個以tm結構表達的機器時間信息
函數返回: 以tm結構表達的時間,結構tm定義如下:

  1. struct tm  
  2. {  
  3.     int tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */  
  4.     int tm_min; /* Minutes: 0-59 */  
  5.     int tm_hour;    /* Hours since midnight: 0-23 */  
  6.     int tm_mday;    /* Day of the month: 1-31 */  
  7.     int tm_mon; /* Months *since* january: 0-11 */  
  8.     int tm_year;    /* Years since 1900 */  
  9.     int tm_wday;    /* Days since Sunday (0-6) */  
  10.     int tm_yday;    /* Days since Jan. 1: 0-365 */  
  11.     int tm_isdst;   /* +1 Daylight Savings Time, 0 No DST,* -1 don't know */  
  12. };  


參數說明: timer-使用time()函數獲得的機器時間

  1. #include <time.h>  
  2. #include <stdio.h>  
  3. #include <dos.h>  
  4. int main()  
  5. {  
  6.     time_t timer;  
  7.     struct tm *tblock;  
  8.     timer=time(NULL);  
  9.     tblock=localtime(&timer);  
  10.     printf("Local time is: %s",asctime(tblock));  
  11.     return 0;  
  12. }  

@函數名稱: asctime

函數原型: char* asctime(struct tm * ptr)
函數功能: 得到機器時間(日期時間轉換爲ASCII碼)
函數返回: 返回的時間字符串格式爲:星期,月,日,小時:分:秒,年
參數說明: 結構指針ptr應通過函數localtime()和gmtime()得到

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <time.h>  
  4. int main()  
  5. {  
  6.     struct tm t; //通過自己爲每個tm成員賦值,沒有什麼實際意思,大多數情況下都是通過系統函數計算time_t來得到tm中的各個數值  
  7.     char str[80];  
  8.     t.tm_sec=1;  
  9.     t.tm_min=3;  
  10.     t.tm_hour=7;  
  11.     t.tm_mday=22;  
  12.     t.tm_mon=11;  
  13.     t.tm_year=56;  
  14.     t.tm_wday=4;  
  15.     t.tm_yday=0;  
  16.     t.tm_isdst=0;  
  17.     strcpy(str,asctime(&t));  
  18.     printf("%s",str);  
  19.     return 0;  
  20. }  

@函數名稱: ctime

函數原型: char *ctime(const time_t *tp)
函數功能: 得到日曆時間
函數返回: 返回字符串格式:星期,月,日,小時:分:秒,年
參數說明: time-該參數應由函數time獲得  等同於 astime(  localtime(tp) )

  1. #include <stdio.h>  
  2. #include <time.h>  
  3. int main()  
  4. {  
  5.     time_t t;  
  6.     time(&t);  
  7.     printf("Today's date and time: %s",ctime(&t));  
  8.     return 0;  
  9. }  

@函數名稱: difftime

函數原型: double difftime(time_t time2, time_t time1)
函數功能: 得到兩次機器時間差,單位爲秒
函數返回: 時間差,單位爲秒
參數說明: time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得

  1. #include <time.h>  
  2. #include <stdio.h>  
  3. #include <dos.h>  
  4. #include <conio.h>  
  5. int main()  
  6. {  
  7.     time_t first, second;  
  8.     System("cls");  
  9.     first=time(NULL);  
  10.     delay(2000);  
  11.     second=time(NULL);  
  12.     printf("The difference is: %f seconds",difftime(second,first));  
  13.     getch();  
  14.     return 0;  
  15. }  

@函數名稱: gmtime

函數原型: struct tm *gmtime(time_t *time)
函數功能: 得到以結構tm表示的時間信息
函數返回: 以結構tm表示的時間信息指針
參數說明: time-用函數time()得到的時間信息

 

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4. #include <dos.h>  
  5.   
  6. char *tzstr="TZ=PST8PDT";  
  7. int main()  
  8. {  
  9.     time_t t;  
  10.     struct tm *gmt, *area;  
  11.     putenv(tzstr);  
  12.     tzset();  
  13.     t=time(NULL);  
  14.     area=localtime(&t);  
  15.     printf("Local time is:%s", asctime(area));  
  16.     gmt=gmtime(&t);  
  17.     printf("GMT is:%s", asctime(gmt));  
  18.     return 0;  
  19. }  

@函數名稱: time

函數原型: time_t time(time_t *timer)
函數功能: 得到機器的日曆時間或者設置日曆時間
函數返回: 機器日曆時間
參數說明: timer=NULL時得到機器日曆時間,timer=時間數值時,用於設置日曆時間,time_t是一個long類型

  1. #include <time.h>  
  2. #include <stdio.h>  
  3. #include <dos.h>  
  4. int main()  
  5. {  
  6.     time_t t;  
  7.     t=time(NULL);  
  8.     printf("The number of seconds since January 1,1970 is %ld\n",t);  
  9.     return 0;  
  10. }  

@函數名稱: tzset

函數原型: void tzset(void)
函數功能: UNIX兼容函數,用於得到時區,在DOS環境下無用途
函數返回:
參數說明:

  1. #include <time.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. int main()  
  5. {  
  6.     time_t td;  
  7.     putenv("TZ=PST8PDT");  
  8.     tzset();  
  9.     time(&td);  
  10.     printf("Current time=%s",asctime(localtime(&td)));  
  11.     return 0;  
  12. }  

@函數名稱: strftime

函數原型: size_t strftime( char *strDest, size_t  maxsize, const char *format, const struct tm  *timeptr);
函數功能: 根據format指向字符串中格式命令把timeptr中保存的時間信息放在strDest指向的字符串中,最多向 strDest中存放maxsize個字符。
                  該函數返回向strDest指向的字 符串中放置的字符數。

參數說明: 轉化結果存在s中,最多maxsize個字符寫到s中
函數返回: 寫到s中的字符數(不包括'\0'),如果字符數多於ssizemax,函數返回0.

  1. /* 
  2.    類似於sprintf(),識別以百分號(%)開始 的格式命令集合,格式化輸出結果放在一個字符串中。格式命令列在下面,它們是區分大小寫的。 
  3.     %a 星期幾的簡寫 
  4.     %A 星期幾的全稱 
  5.     %b 月分的簡寫 
  6.     %B 月份的全稱 
  7.     %c 標準的日期的時間串 
  8.     %C 年份的後兩位數字 
  9.     %d 十進制表示的每月的第幾天 
  10.     %D 月/天/年 
  11.     %e 在兩字符域中,十進制表示的每月的第幾天 
  12.     %F 年-月-日 
  13.     %g 年份的後兩位數字,使用基於周的年 
  14.     %G 年分,使用基於周的年 
  15.     %h 簡寫的月份名 
  16.     %H 24小時制的小時 
  17.     %I 12小時制的小時 
  18.     %j 十進制表示的每年的第幾天 
  19.     %m 十進制表示的月份 
  20.     %M 十時製表示的分鐘數 
  21.     %n 新行符 
  22.     %p 本地的AM或PM的等價顯示 
  23.     %r 12小時的時間 
  24.     %R 顯示小時和分鐘:hh:mm 
  25.     %S 十進制的秒數 
  26.     %t 水平製表符 
  27.     %T 顯示時分秒:hh:mm:ss 
  28.     %u 每週的第幾天,星期一爲第一天 (值從0到6,星期一爲0) 
  29.     %U 第年的第幾周,把星期日做爲第一天(值從0到53) 
  30.     %V 每年的第幾周,使用基於周的年 
  31.     %w 十進制表示的星期幾(值從0到6,星期天爲0) 
  32.     %W 每年的第幾周,把星期一做爲第一天(值從0到53) 
  33.     %x 標準的日期串 
  34.     %X 標準的時間串 
  35.     %y 不帶世紀的十進制年份(值從0到99) 
  36.     %Y 帶世紀部分的十進制年份 
  37.     %z,%Z 時區名稱,如果不能得到時區名稱則返回空字符。 
  38.     %% 百分號 
  39. */  
  40.   
  41. #include <stdio.h>  
  42. #include <time.h>  
  43. void main()  
  44. {  
  45.        struct tm *newtime;  
  46.        char tmpbuf[128];  
  47.        time_t lt1;  
  48.        time( <1 );  
  49.        newtime=localtime(<1);  
  50.        strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);  
  51.        printf(tmpbuf);  
  52.          
  53.        return 0;  
  54. }  
  55.   
  56.   
  57. /* 
  58. 運行結果: 
  59. Today is Saturday, day 30 of July in the year 2005. 
  60. */  

=================================================================================

知識點:

 

1.Coordinated Universal Time(UTC):

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

2.Calendar Time:

 日曆時間,是用“從一個標準時間點到此時的時間經過的秒數”來表示的時間。標準時間點對不同編譯器可能會不同,但對一個編譯系統來說,標準時間是不變的。

3.epoch:時間點。

  在標準c/c++中是一個整數,用此時的時間和標準時間點相差的秒數(即日曆時間)來表示

4.clock tick:

時鐘計時單元(而不叫做時鐘滴答次數),一個時鐘計時單元的時間長短是由cpu控制的,一個clock tick不是cpu的一個時鐘週期,而是c/c++的一個基本計時單位。

5.計時函數:

clock_t clock(void);//返回值:從“開啓這個程序進程”到“程序中調用clock()函數”時之間的cpu時鐘計時單元(clock tick)數,MSDN中稱爲掛鐘時間(wal-clock)

  1. //time.h  
  2. #ifndef _CLOCK_T_DEFINED  
  3. typedef long clock_t;  
  4. #define _CLOCK_T_DEFINED  
  5. #endif  
  6. //time.h  
  7. #define CLOCKS_PER_SEC ((clock_t)1000) //CLOCK_PER_SEC表示一秒會有多少個時鐘計時單元,標準c/c++中,最小的計時單位是一毫秒,  
  8.                                              //可通過clock()/CLOCK_T_SEC得到進程自身運行時間  

6.存儲時間和日期的數據結構tm:

  1. struct tm  
  2. {  
  3.     int tm_sec;//秒,[0-59]  
  4.     int tm_min;//分,[0-59]  
  5.     int tm_hour;//時,[0-23]  
  6.     int tm_mday;//一個月中的日期,[1-31]  
  7.     int tm_mon;//月份,[0-11]  
  8.     int tm_year;//年份,其值爲實際年份-1900  
  9.     int tm_wday;//星期,[0-6]  
  10.     int tm_yday;//從每年的1月1日開始計數,[0-365]  
  11.     int tm_isdst;//夏令標識符,實行夏令時,tm_isdst爲正;不實行夏令時,tm_isdst爲0;不瞭解情況時,tm_isdst爲負  
  12. };  
  13. #define _TM_DEFINED  
  14. #endif  

7.日曆時間用time_t存儲:

  1. //time.h  
  2. #ifndef _TM_DEFINED  
  3. //time.h  
  4. #ifndef _TIME_T_DEFINED  
  5. typedef long time_t;  
  6. #define _TIME_T_DEFINED  
  7. #endif  

使用time_t表示的日曆時間只能存儲到2038年1月18日19時14分07秒,爲了能表示更久遠的時間,一些編譯器引入了64位甚至更長的整形數保 存日曆時間,如VC中採用_time64_t數據類型,通過_time64()函數獲得日曆時間(不是32位的time())這樣可以計算3001年1月 1日0時0分0秒(不包括該時間點)之前的時間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章