Header file: Time. h
Function prototype: time_t time (time_t * timer)
Function: Obtain the current system time. The returned result is of the time_t type (int64 type). In fact, it is a large integer whose value indicates from cut (Coordinated Universal Time) the number of seconds from 00:00:00, January 1, January 1, 1970 (EPOCH time for Unix systems) to the current time.
You can call localtime to convert the cut time represented by time_t to the local time (we are in the + 8 zone and 8 hours longer than the cut time) and convert it to the struct TM type, each data member of this type represents year, month, day, hour, minute, and second respectively.
The structure of struct TM is:
Struct TM {
Int tm_sec;/* seconds after the minute-[0, 61] */
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 */
};
In Win32, there is a systemtime data structure.
The systemtime structure is defined as follows:
Systemtime struct {
Word wyear; // year
Word wmonth; // month
Word wdayofweek; // week, 0 = Sunday, 1 = Monday...
Word wday; // day
Word whour; // hour
Word wminute; // minute
Word wsecond; // second
Word wmilliseconds; // millisecond
};
/*
** Systemtime to time_t
*/
Time_t systime_to_timet (const systemtime & St)
{
Struct tm gm = {st. wsecond, st. wminute, st. whour, st. wday, St. wMonth-1, st. wYear-1900, st. wdayofweek, 0, 0 };
Return mktime (& GM );
}
It can be seen from the above that the value range of the struct TM structure and the struct systemtime structure is different from that of the month and year:
TM. tm_mon = systemtime. wmonth-1
TM. tm_year = systemtime. wyear-1900
/*
** Time_t to systemtime
*/
Systemtime time_ttosystemtime (time_t)
{
TM temptm = * localtime (& T );
Systemtime ST = {1900 + temptm. tm_year,
1 + temptm. tm_mon,
Temptm. tm_wday,
Temptm. tm_mday,
Temptm. tm_hour,
Temptm. tm_min,
Temptm. tm_sec,
0 };
Return st;
}
Another way is to use struct filetime as the intermediate volume to convert time_t and systemtime
/*
** Time_t to systemtime
*/
Systemtime timettosystemtime (time_t T)
{
Filetime ft;
Systemtime PST;
Longlong nll = int32x32to64 (T, 10000000) + 116444736000000000;
Ft. dwlowdatetime = (DWORD) nll;
Ft. dwhighdatetime = (DWORD) (nll> 32 );
Filetimetosystemtime (& ft, & PST );
Return PST;
}
/*
** Systemtime to time_t
*/
Time_t systemtimetotimet (systemtime st)
{
Filetime ft;
Systemtimetofiletime (& St, & ft );
Longlong nll;
Ularge_integer UI;
Ui. lowpart = ft. dwlowdatetime;
Ui. highpart = ft. dwhighdatetime;
Nll = (FT. dwhighdatetime <32) + ft. dwlowdatetime;
Time_t Pt = (long) (Longlong) (ui. quadpart-116444736000000000)/10000000 );
Return pt;
}