Windows中如何計算時間間隔(1)使用CTime 和CTimeSpan
原創(chuàng) 2011年10月17日 10:56:22 標簽:windows /pascal /structure /struct /object /system 1038
時間函數在我們的程序中是使用頻率較高的函數,現將其歸納總結一下,這一章主要講下CTime 和CTimeSpan,前者表示一個時間點,而后表示一個時間段。CTime代表的是絕對時間,CTime andCTimeSpan 沒有虛函數,大部分函數為內聯函數,類對象的大小都為8.
CTime類
1.構造和初始化CTime類對象
CTime類有下列構造函數:
CTime();
CTime(const CTime& timeSrc );
CTime(time_t time )
CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1 );
CTime(WORD wDosDate, WORD wDosTime, int nDST = -1);
CTime(const SYSTEMTIME& sysTime, int nDST = -1);
CTime(const FILETIME& fileTime, int nDST = -1):
說明:以不同的方式構造一個CTime對象??梢杂靡粋€已經存在的CTime對象或一個time_t(在time.h中被定義為long)類型變量來構造和初始化CTime對象,也可以用年、月、日、小時、分、秒來構造和初始化CTime對象,還可以用SYSTEMTIME、FILETIME結構來構造和初始化CTime對象。SYSTEMTIME、FILETIME結構定義如下:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME;
typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME, *PFILETIME, *LPFILETIME;
2.時間值的提取函數
(1)GetCurrentTime() 獲取系統(tǒng)當前時間。
原型:static CTime PASCAL GetCurrentTime();
(2)GetTime() 由CTime對象返回一個time_t變量。
原型:time_t GetTime()const;
(3)GetYear()獲取CTime對象代表的年。
原型:int GetYear()const;
以下(4)至(9)函數原型與GetYear()類似。
(4)GetMonth()獲取CTime對象代表的月。
(5)GetDay()獲取CTime對象代表的日期。
(6)GetHour()獲取CTime對象代表的小時。
(7)GetMinute()獲取CTime對象代表的分。
(8)GetSecond()獲取CTime對象代表的秒。
(9)GetDayOfWeek()獲取CTime對象代表的星期幾,1代表周日、2代表周一、等等。
3.格式化時間
成員函數Format() 將CTime對象中的時間信息轉化為一個格式化的字符串。其函數原型為:
CString Format(LPCTSTR pFormat ) const;
CString Format(UINT nFormatID ) const;
參數pFormat是格式字符串,類似于printf中的格式字符串,格式字符如下:
%a:周的英文縮寫形式;
%A:周的英文全名形式;
%b: 月的英文縮寫形式;
%B:月的英文全名形式;
%c: 完整的日期和時間;
%d:十進制形式的日期(01-31);
%H:24小時制的小時(00-23);
%I: 12小時制的小時(00-11);
%j: 十進制表示的一年中的第幾天(001-366);
%m: 月的十進制表示(01-12);
%M:十進制表示的分鐘(00-59);
%p: 12小時制的上下午標示(AM/PM);
%S: 十進制表示的秒(00-59);
%U: 一年中的第幾個星期(00-51),星期日是一周的第一天;
%W: 一年中的第幾個星期(00-51),星期一是一周的第一天;
%w: 十進制表示的星期幾(0-6);
%Y: 十進制表示的年;
參數nFormatID 是格式字符串資源的ID號。
4.重載運算符
operator = : 賦予新的時間。
operator + : 增加CTime和CTimeSpan對象。
operator – : 減小CTime和CTimeSpan對象。
operator += : CTime對象加一個CTimeSpan對象。
operator -= : CTime對象減一個CTimeSpan對象。
operator == : 比較兩個絕對時間是否相等。
operator != : 比較兩個絕對時間是否不相等。
operator > : 比較兩個絕對時間,是否前一個大于后一個。
operator < : 比較兩個絕對時間,是否前一個小于后一個。
operator >= : 比較兩個絕對時間,是否前一個大于等于后一個。
operator <= : 比較兩個絕對時間,是否前一個小于等于后一個。
The upper date limit is 12/31/3000. The lower limit is 1/1/1970 12:00:00 AM GMT.
CTimeSpan類
The CTimeSpan object is stored in a __time64_t structure, which is 8 bytes.
1.構造函數。
CTimeSpan類有下列構造函數:
(1)CTimeSpan() ;
(2)CTimeSpan(const CTimeSpan& timeSpanSrc);
(3)CTimeSpan(time_t time)
(4)CTimeSpan(LONG lDays, int nHours, int nMins, int nSecs);
參數timeSpanSrc為一個已存在的 CTimeSpan 對象,time為一個time_t 類型的時間值,lDays, nHours, nMins, nSecs分別為天數、小時數、分數和秒數。
2.時間值的提取函數
(1)GetDays()獲得CTimeSpan類對象中包含的完整的天數。
(2)GetHours() 獲得當天的小時數,值在-23到23之間。
(3)GetTotalHours() 獲得CTimeSpan類對象中包含的完整的小時數。
(4)GetMinutes() 獲得當前小時包含的分數,值在-59到59之間。
(5)GetTotalMinutes() 獲得CTimeSpan類對象中包含的完整的分數。
(6)GetSeconds() 獲得當前分鐘包含的秒數,值在-59到59之間。
(7)GetTotalSeconds() 獲得CTimeSpan類對象中包含的完整的秒數。
格式化時間
Format()將一個CTimeSpan對象轉換成格式字符串。使用方式與CTime類似,格式化字符包括以下幾個:
%D: CTimeSpan的總天數;
%H: 不足整天的小時數;
%M: 不足1小時的分數;
%S: 不足1分鐘的秒數;
%%: 百分號。
4.重載運算符
CTimeSpan類也重載了運算符“=”,“+”,“-”,“+=”,“-=”,“==”,“!=”,“<”,“>”,“<=”,“>=”,用于CTimeSpan對象的賦值、加減運算及兩個CTimeSpan對象的比較。
eg.
#include <iostream>
#include <atltime.h>
using namespace std;
void main()
{
// CTime 函數
CTime curtime =CTime::GetCurrentTime();
int iyear = curtime.GetYear();
int imonth = curtime.GetMonth();
int iday = curtime.GetDay();
int idayofweek = curtime.GetDayOfWeek();
int ihour = curtime.GetHour();
int iminute = curtime.GetMinute();
int isecond = curtime.GetSecond();
cout<<"當前時間:"<<endl;
cout<<iyear<<"年";
cout<<imonth<<"月";
cout<<iday<<"日";
cout<<ihour<<"時";
cout<<iminute<<"分";
cout<<isecond<<"秒"<<endl;
cout<<"星期"<<idayofweek<<"(周日算1)"<<endl<<endl;
// 時間比較
CTime begintime =CTime(2006,1,1,0,0,0);
cout<<"起始時間:"<<endl;
cout<<begintime.GetYear()<<"年";
cout<<begintime.GetMonth()<<"月";
cout<<begintime.GetDay()<<"日";
cout<<begintime.GetHour()<<"時";
cout<<begintime.GetMinute()<<"分";
cout<<begintime.GetSecond()<<"秒"<<endl;
cout<<"星期"<<begintime.GetDayOfWeek()<<"(周日算1)"<<endl<<endl;
CTimeSpan span;
span = curtime - begintime;
cout<<"兩時間相差:"<<endl;
cout<<span.GetDays()<<"天";
cout<<span.GetHours()<<"小時";
cout<<span.GetMinutes()<<"分";
cout<<span.GetSeconds()<<"秒"<<endl;
cout<<"總小時"<<span.GetTotalHours()<<"小時"<<endl;
cout<<"總分鐘"<<span.GetTotalMinutes()<<"分"<<endl;
cout<<"總秒"<<span.GetTotalSeconds()<<"秒"<<endl;
cout<<endl;
// CTime到CString的轉化
CString strtime;
strtime = curtime.Format(_T("%Y-%m-%d %X"));
wcout<<(LPCTSTR)strtime<<endl; // 因為使用UNICODE,或用下面的方面顯示
wcout<<strtime.GetString()<<endl;
system("pause");
}
結果為
當前時間:
2011年2月16日20時37分3秒
星期4(周日算1)
起始時間:
2006年1月1日0時0分0秒
星期1(周日算1)
兩時間相差:
1872天20小時37分3秒
總小時44948小時
總分鐘2696917分
總秒161815023秒
2011-02-16 20:37:03
2011-02-16 20:37:03
請按任意鍵繼續(xù). . .
注意:
在控制臺中使用cout顯示CString類,
std::cout不支持寬字符,如果需要使用UNICODE,需要使用std::wcout輸出寬字符。同時,需要對CString做一下轉換,LPCTSTR(cpath)或者wcout<<cpath.GetString()<<endl;;或者使用NotSet或者Multi-CharSet就可以使用cout輸出了