To a time () function and localtime () function in the header file of # inlcude <time. h>
Function prototype: time_t time (time_t * timer)
Function purpose: Obtain the calendar time of the machine or set the calendar time.
Header file: Time. h
If the input parameter is Timer: = NULL, the calendar time of the machine is obtained. If the input parameter is = Time, the calendar time is set;
Time_t is a long type
Function prototype: struct TM * localtime (const time_t * timer)
Function purpose: return the machine time information expressed in the TM structure.
Header file: Time. h
Input parameter: Timer: The machine time obtained by using the time () function;
The structure TM is defined:
Struct TM
{
Int tm_sec;/* seconds: 0-59 (K & R says 0-61 ?) */
Int tm_min;/* minutes: 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 Jan. 1: 0-365 */
Int tm_isdst;/* + 1 daylight savings time, 0 No DST,
*-1 don't know */
};
For example:
# Include "stdafx. H"
# Include <time. h>
# Include <stdio. h>
# Include <wtypes. h>
Int main (INT argc, char * argv [])
{
Time_t T;
TM * TP;
T = time (null );
// Since time_t is actually a long integer, to a future day, from a time point (usually January 1, 1970 00:00:00)
// The number of seconds (that is, the calendar time) at that time is beyond the range of the number that can be expressed by a long integer. What should I do? For values of the time_t data type,
// It indicates that the time cannot be later than January 18, 2038. Some compiler vendors
// A 64-bit or longer integer is introduced to save the calendar time. For example, Microsoft uses the _ time64_t data type in Visual C ++.
// Save the calendar time and use the _ time64 () function to obtain the calendar time (instead of using the 32-bit time () function ).
// You can use this data type to save the time before 00:00:00, January 1, January 1, 3001 (excluding this time point.
TP = localtime (& T );
Printf ("% d/N", TP-> tm_mon + 1, TP-> tm_mday, TP-> tm_year + 1900 );
Printf ("% d: % d/N", TP-> tm_hour, TP-> tm_min, TP-> tm_sec );
Systemtime stcurtime = {0 };
: Getlocaltime (& stcurtime );
// Differentiate getsystemtime (& stcurtime );
Printf ("% d/N", stcurtime. wmonth, stcurtime. wday, stcurtime. wyear );
Printf ("% d: % d/N", stcurtime. whour, stcurtime. wminute, stcurtime. wsecond );
Return 0;
}