C/C++時間函數的使用

一、獲取日曆時間
time_t是定義在time.h中的一個類型,表示一個日曆時間,也就是從1970年1月1日0時0分0秒到此時的秒數,原型是:
typedef long time_t; /* time value */
可以看出time_t其實是一個長整型,由於長整型能表示的數值有限,因此它能表示的最遲時間是2038年1月18日19時14分07秒。

函數time可以獲取當前日曆時間時間,time的定義:
time_t time(time_t *)

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t nowtime;
  7. nowtime = time(NULL); //獲取當前時間
  8. cout << nowtime << endl;
  9. return 0;
  10. }

輸出結果:1268575163

二、獲取本地時間
time_t只是一個長整型,不符合我們的使用習慣,需要轉換成本地時間,就要用到tm結構,time.h中結構tm的原型是:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [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 January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};


可以看出,這個機構定義了年、月、日、時、分、秒、星期、當年中的某一天、夏令時。可以用這個結構很方便的顯示時間。

用localtime獲取當前系統時間,該函數將一個time_t時間轉換成tm結構表示的時間,函數原型:
struct tm * localtime(const time_t *)
使用gmtime函數獲取格林尼治時間,函數原型:
struct tm * gmtime(const time_t *)

爲了方便顯示時間,定義了一個函數void dsptime(const struct tm *);

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. void dsptime(const struct tm *); //輸出時間。
  5. int main(void)
  6. {
  7. time_t nowtime;
  8. nowtime = time(NULL); //獲取日曆時間
  9. cout << nowtime << endl; //輸出nowtime
  10. struct tm *local,*gm;
  11. local=localtime(&nowtime); //獲取當前系統時間
  12. dsptime(local);
  13. gm=gmtime(&nowtime); //獲取格林尼治時間
  14. dsptime(gm);
  15. return 0;
  16. }
  17. void dsptime(const struct tm * ptm)
  18. {
  19. char *pxq[]={"日","一","二","三","四","五","六"};
  20. cout << ptm->tm_year+1900 << "年" << ptm->tm_mon+1 << "月" << ptm->tm_mday << "日 " ;
  21. cout << ptm->tm_hour << ":" << ptm->tm_min << ":" << ptm->tm_sec <<" " ;
  22. cout << " 星期" <<pxq[ptm->tm_wday] << " 當年的第" << ptm->tm_yday << "天 " << endl;
  23. }

輸出結果:
1268575163
2010年3月14日 21:59:23 星期日 當年的第72天
2010年3月14日 13:59:23 星期日 當年的第72天

三、輸出時間
C/C++語言提供了用字符串格式表示時間的函數。
char * asctime(const struct tm *)
char * ctime(const time_t *)
這兩個函數返回值都是一個表示時間的字符串,區別在於傳入的參數不同。

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t nowtime;
  7. nowtime = time(NULL); //獲取日曆時間
  8. cout << nowtime << endl; //輸出nowtime
  9. struct tm *local;
  10. local=localtime(&nowtime); //獲取當前系統時間
  11. cout << asctime(local) ;
  12. cout << ctime(&nowtime) ;
  13. return 0;
  14. }

輸出結果:
1268575163
Sun Mar 14 13:59:23 2010
Sun Mar 14 21:59:23 2010

四、計算時間間隔
可以通過difftime來計算兩個時間的間隔,可以精確到秒,函數原型:
double difftime(time_t, time_t)
要想精確到毫秒,就要使用clock函數了,函數原型:
clock_t clock(void)
從定義可以看出clock返回一個clock_t類型,這個類型也定義在time.h中,原型是:
typedef long clock_t
clock_t也是一個長整型,表示的是從程序開始運行到執行clock函數時所經過的cpu時鐘計時單元數。

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t start, ends;
  7. clock_t cstart,cends;
  8. start=time(NULL);
  9. cstart=clock();
  10. system("pause");
  11. ends=time(NULL);
  12. cends=clock();
  13. cout << "時間差:" << difftime(ends,start) << endl;
  14. cout << "Clock時間差:" << cends-cstart << endl;
  15. return 0;
  16. }

輸出結果:
請按任意鍵繼續. . .
時間差:3
Clock時間差:3094

在time.h中定義了一個CLOCKS_PER_SEC
/* Clock ticks macro - ANSI version */
#define CLOCKS_PER_SEC 1000
表示1秒鐘內有多少個時鐘計時單元,在標準C/C++中,最小的計時單位是1毫秒。


五、自定義時間格式
C/C++在time.h中提供了一個自定義時間格式的函數strftime,函數原型:
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
參數說明:
char *strDest:用來存放格式化後的字符串緩存,
size_t maxsize:指定最多可以輸出的字符數,
const char *format:格式化字符串,
const struct tm *timeptr:要轉換的時間。

可使用的格式化字符串:
%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 時區名稱,如果不能得到時區名稱則返回空字符。
%% 百分號

  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. int main(void)
  5. {
  6. time_t nowtime;
  7. nowtime = time(NULL); //獲取日曆時間
  8. cout << nowtime << endl; //輸出nowtime
  9. struct tm *local;
  10. local=localtime(&nowtime); //獲取當前系統時間
  11. char buf[80];
  12. strftime(buf,80,"格式化輸出:%Y-%m-%d %H:%M:%S",local);
  13. cout << buf << endl;
  14. return 0;
  15. }

輸出結果:
1268575163
格式化輸出:2010-03-14 21:59:23

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