CTime类小结1
參考:http://www.cnblogs.com/chuncn/archive/2009/03/12/1409261.html
CTime類
1.構(gòu)造和初始化CTime類對象
CTime類有下列構(gòu)造函數(shù):
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 );
說明:以不同的方式構(gòu)造一個CTime對象。可以用一個已經(jīng)存在的CTime對象或一個time_t(在time.h中被定義為long)類型變量來構(gòu)造和初始化CTime對象,也可以用年、月、日、小時、分、秒來構(gòu)造和初始化CTime對象,還可以用SYSTEMTIME、FILETIME結(jié)構(gòu)來構(gòu)造和初始化CTime對象。SYSTEMTIME、FILETIME結(jié)構(gòu)定義如下:
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.時間值的提取函數(shù)
(1)GetCurrentTime()???? 獲取系統(tǒng)當(dāng)前時間。
原型:static CTime PASCAL GetCurrentTime( );
(2)GetTime()??? 由CTime對象返回一個time_t變量。
原型:time_t GetTime( ) const;
(3)GetYear()???? 獲取CTime對象代表的年。
原型:int GetYear( ) const;
以下(4)至(9)函數(shù)原型與GetYear()類似。
(4)GetMonth()獲取CTime對象代表的月。
(5)GetDay() 獲取CTime對象代表的日期。
(6)GetHour() 獲取CTime對象代表的小時。
(7)GetMinute()獲取CTime對象代表的分。
(8)GetSecond() 獲取CTime對象代表的秒。
(9)GetDayOfWeek() 獲取CTime對象代表的星期幾,1代表周日、2代表周一、等等。
3.格式化時間
成員函數(shù)Format() 將CTime對象中的時間信息轉(zhuǎn)化為一個格式化的字符串。其函數(shù)原型為:
CString Format( LPCTSTR pFormat ) const;
CString Format( UINT nFormatID ) const;
參數(shù)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小時制的上下午標(biāo)示(AM/PM);
%S: 十進制表示的秒(00-59);
%U: 一年中的第幾個星期(00-51),星期日是一周的第一天;
%W: 一年中的第幾個星期(00-51),星期一是一周的第一天;
%w: 十進制表示的星期幾(0-6);
%Y: 十進制表示的年;
參數(shù)nFormatID 是格式字符串資源的ID號。
4.重載運算符
operator = :??? 賦予新的時間。
operator + :??? 增加CTime和CTimeSpan對象。
operator – :??? 減小CTime和CTimeSpan對象。
operator += : CTime對象加一個CTimeSpan對象。
operator -= :??? CTime對象減一個CTimeSpan對象。
operator == :??? 比較兩個絕對時間是否相等。
operator != :??? 比較兩個絕對時間是否不相等。
operator > :??? 比較兩個絕對時間,是否前一個大于后一個。
operator < :??? 比較兩個絕對時間,是否前一個小于后一個。
operator >=??? : 比較兩個絕對時間,是否前一個大于等于后一個。
operator <=??? : 比較兩個絕對時間,是否前一個小于等于后一個。
?
CTimeSpan類
1.構(gòu)造函數(shù)。
CTimeSpan類有下列構(gòu)造函數(shù):
(1)CTimeSpan() ;
(2)CTimeSpan( const CTimeSpan& timeSpanSrc );
(3)CTimeSpan( time_t time );
(4)CTimeSpan( LONG lDays, int nHours, int nMins, int nSecs );
參數(shù)timeSpanSrc為一個已存在的 CTimeSpan 對象,time為一個time_t 類型的時間值,lDays, nHours, nMins, nSecs分別為天數(shù)、小時數(shù)、分?jǐn)?shù)和秒數(shù)。
2.時間值的提取函數(shù)
(1)GetDays()???? 獲得CTimeSpan類對象中包含的完整的天數(shù)。
(2)GetHours()???? 獲得當(dāng)天的小時數(shù),值在-23到23之間。
(3)GetTotalHours()???? 獲得CTimeSpan類對象中包含的完整的小時數(shù)。
(4)GetMinutes()???? 獲得當(dāng)前小時包含的分?jǐn)?shù),值在-59到59之間。
(5)GetTotalMinutes()??? 獲得CTimeSpan類對象中包含的完整的分?jǐn)?shù)。
(6)GetSeconds()???? 獲得當(dāng)前分鐘包含的秒數(shù),值在-59到59之間。
(7)GetTotalSeconds()??? 獲得CTimeSpan類對象中包含的完整的秒數(shù)。
格式化時間
Format() 將一個CTimeSpan對象轉(zhuǎn)換成格式字符串。使用方式與CTime類似,格式化字符包括以下幾個:
%D:???? CTimeSpan的總天數(shù);
%H:???? 不足整天的小時數(shù);
%M:???? 不足1小時的分?jǐn)?shù);
%S:???? 不足1分鐘的秒數(shù);
%%:???? 百分號。
4.重載運算符
CTimeSpan類也重載了運算符“=”,“+”,“-”,“+=”,“-=”,“==”,“!=”,“<”,“>”,“<=”,“>=”,用于CTimeSpan對象的賦值、加減運算及兩個CTimeSpan對象的比較。
?
eg.
#include <iostream>
#include <atltime.h>
using namespace std;
void main()
{
??? // CTime 函數(shù)
??? 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<<"當(dāng)前時間:"<<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的轉(zhuǎn)化
??? CString strtime;
??? strtime = curtime.Format(_T("%Y-%m-%d %X"));
???
??? wcout<<(LPCTSTR)strtime<<endl; // 因為使用UNICODE,或用下面的方面顯示
??? wcout<<strtime.GetString()<<endl;
??? system("pause");
}
結(jié)果為
當(dāng)前時間:
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做一下轉(zhuǎn)換,LPCTSTR(cpath)或者wcout<<cpath.GetString()<<endl;;或者使用NotSet或者Multi-CharSet就可以使用cout輸出了.
轉(zhuǎn)載于:https://www.cnblogs.com/MMLoveMeMM/articles/3089192.html
總結(jié)
- 上一篇: 求息开头的成语接龙!
- 下一篇: 求一个qq四字网名女生!