各种时间类/数据结构
与时间相关的类和数据结构有:
1. CTime
2. CTimeSpan
3. __time64_t和time_t
4. struct tm
5. SYSTEMTIME
6. FILETIME
7. DBTIMESTAMP
一. CTime
// 微软文档
http://msdn.microsoft.com/zh-cn/library/78zb0ese.aspx
1.1 定义
class CTime
{
public:
static CTime WINAPI GetCurrentTime() throw();
static BOOL WINAPI IsValidFILETIME(_In_ const FILETIME& ft) throw();
CTime() throw();
CTime(_In_ __time64_t time) throw();
CTime(_In_ int nYear, _In_ int nMonth, _In_ int nDay, _In_ int nHour, _In_ int nMin, _In_ int nSec, _In_ int nDST = -1);
CTime(_In_ WORD wDosDate, _In_ WORD wDosTime, _In_ int nDST = -1);
CTime(_In_ const SYSTEMTIME& st, _In_ int nDST = -1);
CTime(_In_ const FILETIME& ft, _In_ int nDST = -1);
CTime(_In_ const DBTIMESTAMP& dbts, _In_ int nDST = -1) throw();
CTime& operator=(_In_ __time64_t time) throw();
CTime& operator+=(_In_ CTimeSpan span) throw();
CTime& operator-=(_In_ CTimeSpan span) throw();
CTimeSpan operator-(_In_ CTime time) const throw();
CTime operator-(_In_ CTimeSpan span) const throw();
CTime operator+(_In_ CTimeSpan span) const throw();
bool operator==(_In_ CTime time) const throw();
bool operator!=(_In_ CTime time) const throw();
bool operator<(_In_ CTime time) const throw();
bool operator>(_In_ CTime time) const throw();
bool operator<=(_In_ CTime time) const throw();
bool operator>=(_In_ CTime time) const throw();
struct tm* GetGmtTm(_Out_ struct tm* ptm) const;
struct tm* GetLocalTm(_Out_ struct tm* ptm) const;
bool GetAsSystemTime(_Out_ SYSTEMTIME& st) const throw();
bool GetAsDBTIMESTAMP(_Out_ DBTIMESTAMP& dbts) const throw();
__time64_t GetTime() const throw();
int GetYear() const throw();
int GetMonth() const throw();
int GetDay() const throw();
int GetHour() const throw();
int GetMinute() const throw();
int GetSecond() const throw();
int GetDayOfWeek() const throw();
// formatting using "C" strftime
CString Format(_In_z_ LPCTSTR pszFormat) const;
CString FormatGmt(_In_z_ LPCTSTR pszFormat) const;
CString Format(_In_ UINT nFormatID) const;
CString FormatGmt(_In_ UINT nFormatID) const;
#if defined(_AFX) && defined(_UNICODE)
// for compatibility with MFC 3.x
CString Format(_In_z_ LPCSTR pFormat) const;
CString FormatGmt(_In_z_ LPCSTR pFormat) const;
#endif
#ifdef _AFX
CArchive& Serialize64(_In_ CArchive& ar);
#endif
private:
__time64_t m_time;
};
1.2 使用
1.2.1 构造
// 1993年3月19日22:15:00 (年月日, 时分秒)
CTime t(1999, 3, 19, 22, 15, 0);
nYear 1970–3000
nMonth 1–12
nDay 1–31
nHour 0-23
nMin 0-59
nSec 0-59
1.2.2 格式化成字符串
CTime t(1999, 3, 19, 22, 15, 0);
// "Friday, March 19, 1999"
CString s = t.Format(_T("%A, %B %d, %Y"));
CTime::Format的参数
%a 缩写的星期名称
%A 完整星期名称
%b 缩写的月份名称
%B 完整的月份名称。
%c 日期和时间表示恰当的区域设置
%d 日期为十进制数字 (01 - 31) 的月份
%H 以 24 小时格式 (00 - 23) 的点
%I 以