MFC 支持多种不同的日期和时间使用方法:
支持自动化
DATE数据类型。DATE支持日期、时间和日期/时间值。COleDateTime和COleDateTimeSpan类封装了此功能。 它们通过自动化支持来使用COleVariant类。常规用途时间类。
CTime和CTimeSpan类封装了与 ANSI 标准时间库关联的大部分功能,该库在time.h中声明。支持系统时钟。 在 MFC 版本 3.0 中,向
CTime添加了对 Win32SYSTEMTIME和FILETIME数据类型的支持。
日期和时间:自动化支持
COleDateTime 类提供了表示日期和时间信息的方法。 它提供比 CTime 类更精细的粒度和更大的范围。 COleDateTimeSpan 类表示经过的时间,例如两个 COleDateTime 对象之间的差异。
COleDateTime 和 COleDateTimeSpan 类设计用于 COleVariant 类。 COleDateTime 和 COleDateTimeSpan 在 MFC 数据库编程中也很有用,但是,只要你想要操作日期和时间值,就可以使用它们。 虽然 COleDateTime 类有比 CTime 类更大的值范围和更精细的粒度,但它需要的单个对象的存储比 CTime 更多。 使用基础 DATE 类型时,还有一些特殊注意事项。 若要详细了解如何实现 DATE,请参阅 DATE 类型。
COleDateTime 对象可用于表示 100 年 1 月 1 日至 9999 年 12 月 31 日之间的日期。 COleDateTime 对象是浮点值,大致分辨率为 1 毫秒。 COleDateTime 基于在 MFC 文档中的 COleDateTime::operator DATE 下定义的 DATE 数据类型。 DATE 的实际实现超出这些边界。 COleDateTime 实现会施加这些边界,以便更轻松地处理类。
COleDateTime 不支持儒略日期。 假定公历可追溯到 100 年 1 月 1 日。
COleDateTime 忽略夏令时 (DST)。 以下代码示例比较了两种计算跨 DST 切换日期的时间跨度的方法:一种使用 CRT,另一种使用 COleDateTime。
第一种方法使用标准 C 类型结构 tm 和 time_t分别将两个 CTime 对象(time1 和 time2)设置为 4 月 5 日和 4 月 6 日。 代码显示 time1 和 time2 以及它们之间的时间跨度。
第二种方法创建两个 COleDateTime 对象(oletime1 和 oletime2),并将它们设置为与 time1 和 time2 相同的日期。 它显示 oletime1 和 oletime2 以及它们之间的时间跨度。
CRT 正确计算了 23 小时的差值。 COleDateTimeSpan 计算了 24 小时的差值。
void CDTDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
time_t date1_t, date2_t;
tm date_tm;
date_tm.tm_hour = 12;
date_tm.tm_min = 0;
date_tm.tm_mon = 3;
date_tm.tm_sec = 0;
date_tm.tm_wday = 0; //Day of week (0-6; Sunday = 0)
date_tm.tm_yday = 0;
date_tm.tm_year = 97;
date_tm.tm_isdst = -1; //Positive if Daylight Saving Time is in effect;
//0 if Daylight Saving Time is not in effect;
//Negative if status of DST is unknown.
date_tm.tm_mday = 6;
date2_t = mktime(&date_tm);
date_tm.tm_mday = 5;
date_tm.tm_isdst = 0;
date1_t = mktime(&date_tm);
CTime time1(date1_t), time2(date2_t);
CTimeSpan ts = time2 - time1;
dc.TextOut(0, 0, CString(_T("CTime")));
dc.TextOut(0, 20, time1.Format(_T("%H:%M:%S %A, %B %d, %Y")));
dc.TextOut(0, 40, time2.Format(_T("%H:%M:%S %A, %B %d, %Y")));
dc.TextOut(0, 60, ts.Format(_T("%H:%M:%S and %D days")));
COleDateTime oletime1(date1_t), oletime2(date2_t);
COleDateTimeSpan olets = oletime2 - oletime1;
dc.TextOut(0, 120, CString(_T("COleDateTime")));
dc.TextOut(0, 140, oletime1.Format(_T("%H:%M:%S %A, %B %d, %Y")));
dc.TextOut(0, 160, oletime2.Format(_T("%H:%M:%S %A, %B %d, %Y")));
//Work-around bug in COleDateTime::Format("%D")
CString str;
str.Format(_T("%s and %d days"), (LPCTSTR)olets.Format(_T("%H:%M:%S")),
olets.GetDays());
dc.TextOut(0, 180, str);
}
获取当前时间
以下过程演示如何创建 COleDateTime 对象并使用当前时间初始化它。
获取当前时间的步骤
创建
COleDateTime对象。调用
GetCurrentTime。COleDateTime timeNow; timeNow = COleDateTime::GetCurrentTime();
计算运行时间
此过程演示如何计算两个 COleDateTime 对象之间的差异并获取 COleDateTimeSpan 结果。
计算运行时间的步骤
创建两个
COleDateTime对象。将
COleDateTime对象之一设置为当前时间。执行一些耗时的任务。
将另一
COleDateTime对象设置为当前时间。采用两个时间之间的差异。
COleDateTime timeStart, timeEnd; timeStart = COleDateTime::GetCurrentTime(); // ... perform time-consuming task timeEnd = COleDateTime::GetCurrentTime(); COleDateTimeSpan spanElapsed = timeEnd - timeStart;
设置时间格式
设置时间格式的步骤
使用 COleDateTime 或 COleDateTimeSpan 的 Format 成员函数创建表示时间或运行时间的字符串。
COleDateTime time(1970, 12, 18, 17, 30, 0);
// 18 December 1970, 5:30 PM
CString s = time.Format(VAR_DATEVALUEONLY);
// s contains the date formatted based on
// the current national language specifications
// (locale ID). The time portion is ignored for
// formatting purposes in this case.
有关详细信息,请参阅 COleVariant 类。
日期和时间:数据库支持
从版本 4.0 开始,MFC 数据库编程使用 COleDateTime 和 COleDateTimeSpan 类来表示日期和时间数据。 也在自动化中使用的这些类派生自 COleVariant 类。 它们为管理日期和时间数据提供比 CTime 和 CTimeSpan 提供的支持更好的支持。
日期和时间:SYSTEMTIME 支持
COleDateTime 类有接受来自 Win32 的系统和文件时间的构造函数。
Win32 FILETIME 结构将时间表示为 64 位值。 它是比 SYSTEMTIME 结构以及 Win32 用来表示文件创建时间的格式更便利的内部存储格式。 有关 SYSTEMTIME 结构的信息,请参阅 SYSTEMTIME。 有关 FILETIME 结构的信息,请参阅 FILETIME。