Required header file
# Include <time. h>
First, let's take an example of getting the current system time and explain the functions used.
Sample Code:
# Include <stdio. h>
# Include <time. h>
Int main ()
{
Time_t timep; // defines the time_t type variable timep, the type is long int type
Time (& timep); // store the obtained time
// Printf ("% s", asctime (localtime (& timep )));
// Convert the local time to ASCII code and print it out.
Return 0;
}
Function Definition
Char * asctime (const struct tm * timeptr)
Function Description
Asctime () converts the information in the tm structure referred to by the timeptr parameter to the time and date representation method used in the real world,
The result is returned as a string. The current time zone of this function is converted to the local time.
Localtime ()
Function: converts the number of seconds offset from 00:00 to the current time to the local time,
The time after the gmtimes function is converted does not pass the time zone conversion, which is the UTC time.
Note: The time of the tm struct obtained by this function is the calendar time.
Usage: struct tm * localtime (const time_t * clock );
Return Value: returns the pointer to the tm struct. The tm struct is the struct defined in time. h used to separately store the amount of time (year, month, day, and so on.
The following describes a detailed sample code for using localtime () and asctime ().
# Include <stdio. h>
# Include <time. h>
Int main ()
{
Time_t timep; // defines the time_t type variable timep, the type is long int type
Struct tm * block;
Timep = time (NULL); // returns the number of seconds between the current time and 00:00:00, January 1, January 1, 1970.
Printf ("% ld \ n", timep );
Block = localtime (& timep); // store the obtained time
Printf ("% s", asctime (block); // convert the local time to ASCII code and print it out.
Return 0;