本文轉自 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定義如下:
- struct tm
- {
- int tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */
- int tm_min; /* Minutes: 0-59 */
- int tm_hour; /* Hours since midnight: 0-23 */
- int tm_mday; /* Day of the month: 1-31 */
- int tm_mon; /* Months *since* january: 0-11 */
- int tm_year; /* Years since 1900 */
- int tm_wday; /* Days since Sunday (0-6) */
- int tm_yday; /* Days since Jan. 1: 0-365 */
- int tm_isdst; /* +1 Daylight Savings Time, 0 No DST,* -1 don't know */
- };
參數說明: timer-使用time()函數獲得的機器時間
- #include <time.h>
- #include <stdio.h>
- #include <dos.h>
- int main()
- {
- time_t timer;
- struct tm *tblock;
- timer=time(NULL);
- tblock=localtime(&timer);
- printf("Local time is: %s",asctime(tblock));
- return 0;
- }
@函數名稱: asctime
函數原型: char* asctime(struct tm * ptr)
函數功能: 得到機器時間(日期時間轉換爲ASCII碼)
函數返回: 返回的時間字符串格式爲:星期,月,日,小時:分:秒,年
參數說明: 結構指針ptr應通過函數localtime()和gmtime()得到
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- int main()
- {
- struct tm t; //通過自己爲每個tm成員賦值,沒有什麼實際意思,大多數情況下都是通過系統函數計算time_t來得到tm中的各個數值
- char str[80];
- t.tm_sec=1;
- t.tm_min=3;
- t.tm_hour=7;
- t.tm_mday=22;
- t.tm_mon=11;
- t.tm_year=56;
- t.tm_wday=4;
- t.tm_yday=0;
- t.tm_isdst=0;
- strcpy(str,asctime(&t));
- printf("%s",str);
- return 0;
- }
@函數名稱: ctime
函數原型: char *ctime(const time_t *tp)
函數功能: 得到日曆時間
函數返回: 返回字符串格式:星期,月,日,小時:分:秒,年
參數說明: time-該參數應由函數time獲得 等同於 astime( localtime(tp) )
- #include <stdio.h>
- #include <time.h>
- int main()
- {
- time_t t;
- time(&t);
- printf("Today's date and time: %s",ctime(&t));
- return 0;
- }
@函數名稱: difftime
函數原型: double difftime(time_t time2, time_t time1)
函數功能: 得到兩次機器時間差,單位爲秒
函數返回: 時間差,單位爲秒
參數說明: time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得
- #include <time.h>
- #include <stdio.h>
- #include <dos.h>
- #include <conio.h>
- int main()
- {
- time_t first, second;
- System("cls");
- first=time(NULL);
- delay(2000);
- second=time(NULL);
- printf("The difference is: %f seconds",difftime(second,first));
- getch();
- return 0;
- }
@函數名稱: gmtime
函數原型: struct tm *gmtime(time_t *time)
函數功能: 得到以結構tm表示的時間信息
函數返回: 以結構tm表示的時間信息指針
參數說明: time-用函數time()得到的時間信息
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <dos.h>
- char *tzstr="TZ=PST8PDT";
- int main()
- {
- time_t t;
- struct tm *gmt, *area;
- putenv(tzstr);
- 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;
- }
@函數名稱: time
函數原型: time_t time(time_t *timer)
函數功能: 得到機器的日曆時間或者設置日曆時間
函數返回: 機器日曆時間
參數說明: timer=NULL時得到機器日曆時間,timer=時間數值時,用於設置日曆時間,time_t是一個long類型
- #include <time.h>
- #include <stdio.h>
- #include <dos.h>
- int main()
- {
- time_t t;
- t=time(NULL);
- printf("The number of seconds since January 1,1970 is %ld\n",t);
- return 0;
- }
@函數名稱: tzset
函數原型: void tzset(void)
函數功能: UNIX兼容函數,用於得到時區,在DOS環境下無用途
函數返回:
參數說明:
- #include <time.h>
- #include <stdlib.h>
- #include <stdio.h>
- int main()
- {
- time_t td;
- putenv("TZ=PST8PDT");
- tzset();
- time(&td);
- printf("Current time=%s",asctime(localtime(&td)));
- return 0;
- }
@函數名稱: 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.
- /*
- 類似於sprintf(),識別以百分號(%)開始 的格式命令集合,格式化輸出結果放在一個字符串中。格式命令列在下面,它們是區分大小寫的。
- %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>
- void main()
- {
- struct tm *newtime;
- char tmpbuf[128];
- time_t lt1;
- time( <1 );
- newtime=localtime(<1);
- strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);
- printf(tmpbuf);
- return 0;
- }
- /*
- 運行結果:
- Today is Saturday, day 30 of July in the year 2005.
- */
=================================================================================
知識點:
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)
- //time.h
- #ifndef _CLOCK_T_DEFINED
- typedef long clock_t;
- #define _CLOCK_T_DEFINED
- #endif
- //time.h
- #define CLOCKS_PER_SEC ((clock_t)1000) //CLOCK_PER_SEC表示一秒會有多少個時鐘計時單元,標準c/c++中,最小的計時單位是一毫秒,
- //可通過clock()/CLOCK_T_SEC得到進程自身運行時間
6.存儲時間和日期的數據結構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;//從每年的1月1日開始計數,[0-365]
- int tm_isdst;//夏令標識符,實行夏令時,tm_isdst爲正;不實行夏令時,tm_isdst爲0;不瞭解情況時,tm_isdst爲負
- };
- #define _TM_DEFINED
- #endif
7.日曆時間用time_t存儲:
- //time.h
- #ifndef _TM_DEFINED
- //time.h
- #ifndef _TIME_T_DEFINED
- typedef long time_t;
- #define _TIME_T_DEFINED
- #endif
使用time_t表示的日曆時間只能存儲到2038年1月18日19時14分07秒,爲了能表示更久遠的時間,一些編譯器引入了64位甚至更長的整形數保 存日曆時間,如VC中採用_time64_t數據類型,通過_time64()函數獲得日曆時間(不是32位的time())這樣可以計算3001年1月 1日0時0分0秒(不包括該時間點)之前的時間。