First, the time type is divided into two types:
- Coordinated Universal Time (UTC): The world's standard time, that is, Greenwich Mean Time (GMT)
- Calendar time: the calendar time, represented by a standard time point (for example, on January 1, January 1, 1970) to the number of seconds that have elapsed.
Time Acquisition
# Include <time. h> time_t time <time_t * tloc> // function: Obtain the calendar time, from on January 1, January 1, 1970 to the number of seconds that have elapsed since now. // typedef long time_t
Time Conversion
Struct TM * gmtime (const time_t * timep) // function: converts the calendar time to Greenwich Mean Time and saves it to the TM structure. Struct TM * localtime (const time_t * timep) // function converts the calendar time to the local time and saves it to the TM structure struct TM {int tm_sec; // The second value int tm_min; // minute value int tm_hour; // Hour value int tm_mday; // the nth day of the month int tm_mon; // the nth month of the year int tm_year; // which year int tm_wday; // The day of the week int tm_yday; // The day of the year int tm_lsdst; // Daylight Saving Time}
Time Display
Char * asctime (const struct TM * TM) // function: converts the time in the TM format to a string. For example, // sat Jul 30 08:43:03 2010 char * ctime (const time_t * timep) // function: converts the calendar time to a string of local time
Get time
Int gettimeofday (struct timeval * TV, struct timezone * tz) // function: gets the time difference from the early morning of the current day, which is often used to calculate the event time. Struct timeval {int TV _sec; // number of seconds int TV _usec; // Number of microseconds}
Latency
Unsigned int sleep (unsigned int seconds) // function: Enable the program to sleep seconds void usleep (unsigned long USEC) // function: Enable the program to sleep USEC microseconds
The following code provides a simple time programming to show the current time of the system.
# Include <stdio. h> # include <time. h> int main (void) {time_t seconds; char * timestr; seconds = Time (null); // obtain the calendar time timestr = ctime (& seconds ); // convert the calendar time to the string printf ("% s \ n", timestr); // print the returned 0 ;}
Output:
Wed Dec 12 14:14:57 2012