Date and Time

MFC supports several different ways of working with dates and times:

  • Support for the Automation DATE data type. DATE supports date, time, and date/time values. The COleDateTime and COleDateTimeSpan classes encapsulate this functionality. They work with the COleVariant class using Automation support.

  • General-purpose time classes. The CTime and CTimeSpan classes encapsulate most of the functionality associated with the ANSI-standard time library, which is declared in time.h.

  • Support for system clock. With MFC version 3.0, support was added to CTime for the Win32 SYSTEMTIME and FILETIME data types.

Date and Time: Automation Support

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 for use with the COleVariant class. 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. For more information on the implementation of DATE, see The DATE Type.

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 make it easier to work with the class.

COleDateTime doesn't 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.

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.

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

Get the current time

The following procedure shows how to create a COleDateTime object and initialize it with the current time.

To get the current time

  1. Create a COleDateTime object.

  2. Call GetCurrentTime.

    COleDateTime timeNow;
    timeNow = COleDateTime::GetCurrentTime();   
    

Calculate elapsed time

This procedure shows how to calculate the difference between two COleDateTime objects and get a COleDateTimeSpan result.

To calculate elapsed time

  1. Create two COleDateTime objects.

  2. Set one of the COleDateTime objects to the current time.

  3. Perform some time-consuming task.

  4. Set the other COleDateTime object to the current time.

  5. Take the difference between the two times.

    COleDateTime timeStart, timeEnd;
    timeStart = COleDateTime::GetCurrentTime();
    // ... perform time-consuming task
    timeEnd = COleDateTime::GetCurrentTime();
    COleDateTimeSpan spanElapsed = timeEnd - timeStart;   
    

Format a time

To format a time

Use the Format member function of either COleDateTime or COleDateTimeSpan to create a character string representing the time or elapsed time.

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.   

For more information, see class COleVariant.

Date and Time: Database Support

Starting in version 4.0, MFC database programming uses the COleDateTime and COleDateTimeSpan classes to represent date and time data. These classes, also used in Automation, are derived from class COleVariant. They supply better support for managing date and time data than do CTime and CTimeSpan.

Date and Time: SYSTEMTIME Support

The COleDateTime class has constructors that accept system and file times from Win32.

The Win32 FILETIME structure represents time as a 64-bit value. It's a more convenient format for internal storage than a SYSTEMTIME structure, and the format used by Win32 to represent the time of file creation. For information about the SYSTEMTIME structure, see SYSTEMTIME. For information about the FILETIME structure, see FILETIME.

See also

Concepts
General MFC Topics