I. Basic concepts:
■ Time classification
◆ Local time
◆ Coordinated Universal Time (UTC), which is the Greenwich Mean Time.
The difference between Greenwich Mean Time and local time, that is, the time difference we usually call. Because we are in Beijing time (also known as GMT), all the difference values are 8.
■ Common classes (structures)
● Time provided by CRT
◆ Time_t: It is Ong type. We generally cannot understand the meaning of this value. It must be converted.
◆ The TM structure is defined as follows:
Struct TM
{
Int tm_sec;/* seconds after the minute-[0, 59] */
Int tm_min;/* minutes after the hour-[0, 59] */
Int tm_hour;/* hours since midnight-[0, 23] */
Int tm_mday;/* day of the month-[1, 31] */
Int tm_mon;/* months since January-[0, 11] */
Int tm_year;/* years since 1900 */
Int tm_wday;/* Days since Sunday-[0, 6] */
Int tm_yday;/* Days since January 1-[0,365] */
Int tm_isdst;/* daylight savings time flag */
};
This structure seems easier to understand.
If the time_t format is obtained, localtime can be used for conversion.
If you want reverse conversion (Tm-> time_t), you need to use
Time_t mktime (struct TM * timeptr );
In addition, if you want to convert it to the time corresponding to UTC, you need to use
Errno_t gmtime_s (struct TM * _ TM, const time_t * time );
● SDK time. The above two types of time are for CRT. Below are the two equivalent time values of the corresponding SDK.
◆ Typedef struct _ filetime
{
DWORD dwlowdatetime;
DWORD dwhighdatetime;
} Filetime, * pfiletime;
Corresponding to time_t
◆ Typedef struct _ systemtime
{
Word wyear;
Word wmonth;
Word wdayofweek;
Word wday;
Word whour;
Word wminute;
Word wsecond;
Word wmilliseconds;
} Systemtime, * psystemtime;
Corresponding to TM, but the last item is added here (the description is more accurate)
■ Some common functions for obtaining and transforming them
● Obtain the UTC time:
Void getsystemtime (lpsystem lpsystemtime );
● Obtain the local time (Beijing Time)
Void getlocaltime (lpsystem lpsystemtime );
● Convert filetime to systemtime Function
Bool systemtimetofiletime (const systemtime * lpsystemtime,
Lpfiletime );
● Convert systemtime to filetime Function
Bool filetimetosystemtime (const filetime * lpfiletime,
Lpsystemtime lpsystem );
It also provides two functions for mutual Conversion Between UTC and local time.
Bool localfiletimetofiletime (const filetime * lplocalfiletime,
Lpfiletime );
Bool filetimetolocalfiletime (const filetime * lpfiletime,
Lpfiletime lplocalfiletime );
The last one is the ularge_integer structure, which is defined as follows:
Typedef Union ularge_integer {
Struct {
DWORD lowpart;
DWORD highpart;
};
Struct {
DWORD lowpart;
DWORD highpart;
} U;
Ulonglong quadpart;
} Ularge_integer;
This structure is not very different from filetime, mainly because the former 32 is aligned, and the latter 64-bit alignment. to convert, you must assign values.
Some simple reference code:
Ctime Ct = ctime: getcurrenttime ();
//
Time_t TT = CT. gettime ();
//
TM * PT = localtime (& TT );
// Cstring strtime;
// Strtime. format ("% d-% d: % d", Pt-> maid + 1900, Pt-> tm_mon + 1, Pt-> tm_mday, pt-> tm_hour, Pt-> tm_min, Pt-> tm_sec );
// MessageBox (strtime );
// Systemtime stutc, stlocal;
// Getsystemtime (& stutc );
// Getlocaltime (& stlocal );
Handle hfile = createfile ("C: \ ysl.txt", generic_read | generic_write, 0, null, open_always, file_attribute_readonly, null );
Filetime ftcreate, ftaccess, ftwrite, ftlocal;
Systemtime st;
Getfiletime (hfile, & ftcreate, & ftaccess, & ftwrite );
Filetimetolocalfiletime (& ftcreate, & ftlocal );
Filetimetosystemtime (& ftlocal, & St );
Ularge_integer Uli;
Uli. highpart = ftlocal. dwhighdatetime;
Uli. lowpart = ftlocal. dwlowdatetime;
Cstring strtime;
Strtime. Format ("% d", Uli. quadpart );
MessageBox (strtime );
/*
Strtime. Empty ();
Strtime. format ("% d-% d: % d", St. wyear, St. wmonth, St. wday, St. whour, St. wminute, St. wsecond );
MessageBox (strtime );
*/