C++ Topics and Explanations
Handling of Date objects

[ Follow Ups ] [ Post Follow Up ] [ C++ Topics and Explanations ]

Posted by Charles Gutzwiller on December 08, 1999 at 13:03:16:

Throughout the quarter we have used the CTime class as a way to represent date and time information in our programs. We have also used the CTimeSpan class, which represents elapsed time, such as the difference between two CTime objects. These two classes worked well for our purposes but are not without limitations.
CTime objects can only be used to represent dates between January 1, 1970, and January 18, 2038. So if a program needs to deal with dates that fall outside of this range the CTime class would not be adequate. However, for our purposes CTime and CTimeSpan work well, and are rather easy to use.
To build a CTime object we must include and then we have several options in the construction of the object. We can use any of the following constructors to build a CTime object:
CTime( );
CTime( const CTime& timeSrc ); (timeSrc represents an existing CTime object.)
CTime( time_t time ); (time represents a time value)
CTime( WORD wDosDate, WORD wDosTime, int nDST = -1 );
CTime( const SYSTEMTIME& sysTime, int nDST = -1 );
CTime( const FILETIME& fileTime, int nDST = -1 );

Each of these constructors is explained in further detail in the MSDN Library.

The constructor we used most often in this course was:

CTime( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1 );

This is the one that many of us used in our projects because we could have the user enter a date in the format mm/dd/yyyy. Then we could parse the string, use atoi to change the values into integers and then use the integers as the parameters. The following is an example of a function that uses a character array in the format mm/dd/yyyy to create a CTime object:

CTime convertToDate(char aDate[11])
{
char month[2],day[2],year[5];
int intMonth,intDay,intYear;

month[0] = aDate[0];
month[1] = aDate[1];
day[0] = aDate[3];
day[1] = aDate[4];
year[0] = aDate[6];
year[1] = aDate[7];
year[2] = aDate[8];
year[3] = aDate[9];
year[4] = '\0';
intYear = atoi(year);
intMonth = atoi(month);
intDay = atoi(day);
CTime date(intYear,intMonth,intDay,0,0,0) ;
return (date);
}



Follow Ups:


Post a Follow Up:

Name:
E-Mail:

Subject:

Comments:


[ Follow Ups ] [ Post Follow Up ] [ C++ Topics and Explanations ]