C++编程中时间使用总结

各种时间类/数据结构与时间相关的类和数据结构有: 1. CTime2. CTimeSpan3. __time64_t和time_t4. struct tm5. SYSTEMTIME6. FILETIME7. DBTIMESTAMP一. CTime// 微软文档http://msdn.microsoft.com/zh-cn/library/78zb0ese
摘要由CSDN通过智能技术生成

各种时间类/数据结构

与时间相关的类和数据结构有

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 构造

// 199331922:15:00 (年月日时分秒)

CTime t(1999, 3, 19, 22, 15, 0);

nYear  19703000

nMonth 112

nDay 131

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    

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值