ctime库函数的使用
本文探討了C/C++中對日期和時間操作所用到的常用功能,并以大量的實例向你展示了#include <ctime>頭文件中聲明的各種函數和數據結構的詳細使用方法.
基本概念的理解:
Coordinated Universal Time(UTC):世界標準時間,也就是大家所熟知的格林威治標準時間(Greenwich Mean Time,GMT)比如,中國內地的時間與UTC的時差為東八區,表示為:UTC+8
Calendar Time:日歷時間,表示從1970年1月1日0時0點到現在所經過的時間秒數,日歷時間是相對時間,無論你在哪一個時區,在同一時刻對同一個標準時間點來說,日歷時間都是一樣的.?
epoch:英文武譯為(新紀元;新時代;時間上的一點),在標準C/C++中是一個整數,它用此時的時間和標準時間點相差的秒數(即日歷時間)來表示?
一、與日期和時間相關的數據結構?
在標準C/C++中,我們可通過tm結構來獲得日期和時間,tm結構在ctime頭文件中的定義如下:?
#ifndef _TM_DEFINED
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代表一月) - 取值區間為[0,11] */ ?
int tm_year; ? ? ??/* 年份 - 其值等于實際年份減去1900 */ ?
int tm_wday; ? ? ?/* 星期 - 取值區間為[0,6],其中0代表星期天,1代表星期一,以此類推 */ ?
int tm_yday; ? ? ?/* 從每年的1月1日開始的天數 - 取值區間為[0,365],其中0代表1月1日,1代表1月2日,以此類推 */
}; ?
#define _TM_DEFINED ?
#endif?
日歷時間(Calendar Time)是通過?time_t?數據類型來表示的,用time_t表示的時間(日歷時間)是從一個時間點(例如:1970年1月1日0時0分0秒)到此時的秒數在ctime中,我們也可以看到time_t是一個長整型數:?
#ifndef _TIME_T_DEFINED ?
?typedef long time_t; ? ? ? ? ? ? ?/* 時間值 */ ?
#define _TIME_T_DEFINED ? ? ? ? ? ??/* 避免重復定義 time_t */ ?
#endif?
在ctime頭文件中,我們可能會看到一些常用函數,它們都是以time_t為參數類型或返回值類型的函數:?
double difftime(time_t time1, time_t time0); ? ?//計算兩個時間點之間的差值
time_t mktime(struct tm * timeptr); ? ? ? ? ? ? ? ??//把結構化時間轉化為日歷時間,即一個長整數
time_t time(time_t * timer); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//通過參數timer獲得指定日歷時間,參數為NULL,代表當前時間
char * asctime(const struct tm * timeptr); ? ? ?//得到固定的時間格式
char * ctime(const time_t *timer); ? ? ? ? ? ? ? ? ?//得到固定的時間格式
struct tm * gmtime(const time_t *timer); ? ? ???//把日歷時間轉化為結構化時間
struct tm * localtime(const time_t * timer); ? ?//把日歷時間轉化為結構化時間(針對本地的)
二、與日期和時間相關的函數及應用 ?
在本節,我將向大家展示上面七個常用函數的使用例子。
1. 獲得日歷時間
我們可以通過time()函數來獲得日歷時間(Calendar Time),其原型為:time_t time(time_t * timer);?
如果你已經聲明了參數timer,你可以從參數timer返回現在的日歷時間,同時也可以通過返回值返回現在的日歷時間,即從一個時間點(例如:1970年1月1日0時0分0秒)到現在此時的秒數如果參數為空(NULL),函數將只通過返回值返回現在的日歷時間,比如下面這個例子用來顯示當前的日歷時間:?
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
int main(int argc,char *argv[]){
?? ?time_t it = time(NULL);
?? ?cout << "the calendar time now is :" << it << endl;
?? ?return 0;
}
運行的結果與當時的時間有關,我當時運行的結果是:?
The Calendar Time now is 1295939665
其中1295939665就是我運行程序時的日歷時間即從1970年1月1日0時0分0秒到此時(2011-01-25 15:33)時的秒數?
2 獲得日期和時間?
這里說的日期和時間就是我們平時所說的年月日時分秒等信息,這里我們將一個日歷時間保存為一個tm結構的對象. 其中可以使用的函數是gmtime()和localtime(),這兩個函數的原型為:?
struct tm * gmtime(const time_t *timer); ?
struct tm * localtime(const time_t * timer);?
其中gmtime()函數是將日歷時間轉化為世界標準時間(即格林尼治時間),并返回一個tm結構體來保存這個時間,而localtime()函數是將日歷時間轉化為本地時間,比如現在用gmtime()函數獲得的世界標準時間是2011年1月25日15點33分22秒,那么我用localtime()函數在中國地區獲得的本地時間會比世界標準時間晚8個小時,即2011年1月25日23點33分22秒,下面是個例子:?
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
int main(int argc,char *argv[]){
?? ?time_t it = time(NULL);
?? ?struct tm *nowTime;
?? ?struct tm *local;
?? ?nowTime = std::gmtime(&it);
?? ?local = std::localtime(&it);
?? ?cout << "Local hour is: " << (local->tm_hour)<< endl;
?? ?cout << "UTC hour is: " << (nowTime>tm_hour)<< endl;
?? ?return 0;
}
運行結果是:?
Local hour is: 23 ?
UTC hour is: 15?
3 固定的時間格式?
我們可以通過asctime()函數和ctime()函數將時間以固定的格式顯示出來,兩者的返回值都是char*型的字符串,返回的時間格式為:
星期幾 月份 日期 時:分:秒 ? ?年 ? \n\0
?Wed ? Jan ? 02 ? 02:03:55 1980 \n\0
char * asctime(const struct tm * timeptr); ?
char * ctime(const time_t *timer);?
其中asctime()函數是通過tm結構來生成具有固定格式的保存時間信息的字符串,而ctime()是通過日歷時間來生成時間字符串,這樣的話,asctime()函數只是把tm結構對象中的各個域填到時間字符串的相應位置就行了,而ctime()函數需要先參照本地的時間設置,把日歷時間轉化為本地時間,然后再生成格式化后的字符串,示例:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;
int main(int argc,char *argv[]){
?? ?time_t it = time(NULL);
?? ?struct tm *local;
?? ?local = std::localtime(&it);
?? ?std::string str(std::asctime(local));
?? ?std::string cstr(std::ctime(&it));
?? ?cout << "the current time: " << str << endl;
?? ?cout << "the c-current time: " << cstr << endl;
?? ?return 0;
}
運行結果:?
the current time: Tue Jan 25 15:14:25 2011
the c-current time: Tue Jan 25 15:14:25 2011
4 計算持續時間的長度?
有時候在實際應用中要計算一個事件持續的時間長度,我們可以使用difftime()函數,但它只能精確到秒,該函數的定義如下:?
double difftime(time_t time1, time_t time0);?
雖然該函數返回的以秒計算的時間間隔是double類型的,但這并不說明該時間具有同double一樣的精確度,這是由它的參數覺得的(time_t是以秒為單位計算的)比如下面一段程序:?
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <windows.h>
using namespace std;
int main(int argc,char *argv[]){
?? ? time_t start = time(NULL);
?? ?Sleep(1000);
?? ?time_t end = time(NULL);
?? ?cout << "durition time: " << std::difftime(start,end) << endl;
?? ?return 0;
}
運行結果為:durition time: ?1.000000 seconds. ?
可以想像,暫停的時間并不那么巧是整整1秒鐘.
5 分解時間轉化為日歷時間?
這里說的分解時間就是以年月日時分秒等分量保存的時間結構,在C/C++中是tm結構我們可以使用mktime()函數將用tm結構表示的時間轉化為日歷時間其函數原型如下:?
time_t mktime(struct tm * timeptr);?
其返回值就是轉化后的日歷時間這樣我們就可以先制定一個分解時間,然后對這個時間進行操作了,下面的例子可以計算出1997年7月1日是星期幾:?
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
?
using namespace std;
int main(int argc,char *argv[]){
?? ? time_t it = time(NULL);
?? ? struct tm *local;
?? ?local = std::gmtime(&it);
?? ?cout << "calendar time: " << std::mktime(local) << endl;
?? ?return 0;
}
運行結果:calendar time: 1295939665
6. 處定義輸出時間格式
我們可以使用strftime()函數將時間格式化為我們想要的格式它的原型如下:?
size_t ?strftime( ?
char *str, ?
size_t maxsize, ?
const char *format, ?
const struct tm *timeptr ?
); ?
我們可以根據format指向字符串中格式命令把timeptr中保存的時間信息放在strDest指向的字符串中,最多向str中存放maxsize個字符,該函數返回向str指向的字符串中放置的字符數,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 <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include <windows.h>
using namespace std;
?
int main(int argc,char *argv[]){
?? ? ? time_t it = time(NULL);
?? ? ?struct tm *local;
?? ? ?local = std::gmtime(&it);
?? ? ?char cTime[200];
?? ? ?size_t size = strftime(cTime,sizeof(cTime),"%Y-%m-%d %X",local);
?? ? cout << "size: " << size << setw(5)<< cTime << endl;
?? ? time_t t = time(0);
?? ? char tmp[64];
?? ??strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A",localtime(&t) );
?? ? cout << tmp << endl;
?? ?return 0;
return 0;
}
輸出結果為:
size: 192011-01-25 08:27:35
2011/01/25 16:27:35 Tuesday
?
今天就到此為止,上面的代碼在?CDT+Mingw4.4?下調試。
總結
以上是生活随笔為你收集整理的ctime库函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: markdown常用操作(特殊字符显示、
- 下一篇: 关于vs中代码生成的运行库