1,
Use ctime (MFC)
1) obtain the current time.
Ctime time;
Time = ctime: getcurrenttime ();
2) obtain the time element.
Int year = time. getyear ();
Int month = time. getmonth ();
Int day = time. getday ();
Int hour = time. gethour ();
Int minute = time. getminute ();
Int second = time. getsecond ();
Int dayofweek = time. getdayofweek ();
3) obtain the time interval.
Ctimespan timespan (0, 0, 1, 0); // days, hours, minutes, seconds
Timespan = ctime: getcurrenttime ()-time;
4) converts the time to a string.
Cstring sdate, stime, selapsed time;
Sdate = time. Format ("% m/% d/% Y"); // ex: 12/10/98
Stime = time. Format ("% H: % m: % s"); // ex: 9:12:02
Selapsed time = timespan. Format ("% d: % H: % m: % s"); // % d is total elapsed days
5) convert the string to time.
Cstring sdatetime;
Int nyear, nmonth, ndate, nhour, Nmin, nsec;
Sscanf (sdatetime, "% d-% d: % d", & nyear, & nmonth, & ndate, & nhour, & Nmin, & nsec );
Ctime stime (nyear, nmonth, ndate, nhour, Nmin, nsec );
For more information about the time format, see strftime In the MFC documentation.
2,
Use the coledatetime class (MFC)
Coledatetime is the encapsulation of the date type. The date type is used to mark a minute or second as a number 0 on a certain day in a certain year, January 1, January, And the other time as a positive real number if a negative real number, that is, a number that contains information about year, month, day, hour, minute, and second.
Ctimespan m_validity;
Ctime gdate (gyear, gmonth, gday, 0, 0, 0 );
Ctime edate (eyear, emonth, eday, 0, 0, 0 );
M_validity = edate-gdate;
Cstring m_strvalidity;
M_strvalidity.format ("% d days", m_validity.getdays ());
Coledatetime localtime = coledatetime: getcurrenttime ();
Coledatetime endtime = getdatadue (); // The coledatetime class object is returned on the right. It encapsulates the date object and can be directly assigned to the left.
Coledatetimespan timespan;
Timespan = endtime-localtime;
Cstring timespandays;
Timespandays. Format ("% d days", timespan. getdays ());
1) Get a day of the year.
Coledatetime datetime;
Datetime = coledatetime: getcurrenttime ();
Int dayofyear = datetime. getdayofyear ();
2) read time from text strings.
Coledatetime datetime;
Datetime. parsedatetime ("12:12:23 27 January 93 ");
3) obtain the time interval.
// For example, calculate the date difference
Coledatetime begin_date (1970, 1, 1, 0, 0 );
Coledatetime end_date (1990, 1, 1, 0, 0, 0 );
Coledatetimespan timespan; // calculate the time difference
Timespan = end_date-begin_date;
Long expi_date = timespan. getdays ();
3,
SYSTEMTIME (win32)
typedef struct _SYSTEMTIME { WORD wYear; WORD wMonth; WORD wDayOfWeek; WORD wDay; WORD wHour; WORD wMinute; WORD wSecond; WORD wMilliseconds; } SYSTEMTIME, *PSYSTEMTIME;
(1) getlocaltime: Get local time
void GetLocalTime( LPSYSTEMTIME lpSystemTime );
SetlocaltimeSet local time
(2) getsystemtime obtains the Greenwich Mean Time.
Setsystemtime sets the Greenwich Mean Time.
Description
■ Ctime and coledatetime have almost the same functions. However, coledatetime allows users to get a day of the year (a good way to create a Julian Date) and analyze a time text string.
■ Compared with ctime, coledatetime has the advantage that it supports DWORD variables. The number of digits used by coledatetime is twice that of the Double Floating Point. Since ctime is only a simple calculation of the number of seconds passed since January 1, January 1, 1970, it will reach 2037 by January 1, 4294967295, and thus cannot be used any more. Instead, coledatetime is
Floating Point number, which indicates the number of days since January 1, December 30, 1900 (the hour is the decimal part of the day.