다음을 통해 공유


Date and Time: Automation Support

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Date and Time: Automation Support.

This article describes how to take advantage of the class library services related to date and time management. Procedures described include:

The COleDateTime class provides a way to represent date and time information. It provides finer granularity and a greater range than the CTime class. The COleDateTimeSpan class represents elapsed time, such as the difference between two COleDateTime objects.

The COleDateTime and COleDateTimeSpan classes are designed to be used with the COleVariant class used in Automation. COleDateTime and COleDateTimeSpan are also useful in MFC database programming, but they can be used whenever you want to manipulate date and time values. Although the COleDateTime class has a greater range of values and finer granularity than the CTime class, it requires more storage per object than CTime. There are also some special considerations when working with the underlying DATE type. See The DATE Type for more details on the implementation of DATE.

COleDateTime objects can be used to represent dates between January 1, 100, and December 31, 9999. COleDateTime objects are floating point values, with an approximate resolution of 1 millisecond. COleDateTime is based on the DATE data type, defined in the MFC documentation under COleDateTime::operator DATE. The actual implementation of DATE extends beyond these bounds. The COleDateTime implementation imposes these bounds to facilitate working with the class.

COleDateTime does not support Julian dates. The Gregorian calendar is assumed to extend back in time to January 1, 100.

COleDateTime ignores Daylight Saving Time (DST). The following code example compares two methods of calculating a time span that crosses the DST switchover date: one using the CRT, and the other using COleDateTime. DST switches over, in most locales, in the second week in April and the third in October.

The first method sets two CTime objects, time1 and time2, to April 5 and April 6 respectively, using the standard C type structures tm and time_t. The code displays time1 and time2 and the time span between them.

The second method creates two COleDateTime objects, oletime1 and oletime2, and sets them to the same dates as time1 and time2. It displays oletime1 and oletime2 and the time span between them.

The CRT correctly calculates a difference of 23 hours. COleDateTimeSpan calculates a difference of 24 hours.

Note that a workaround is used near the end of the example to display the date properly using COleDateTime::Format. See the Knowledge Base article "BUG: Format("%D") Fails for COleDateTime and COleDateTimeSpan" (Q167338).

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);
}

See Also

Date and Time