C language time function learning and summary, C language function Summary
Time (& t), ctime (& t), and time_t (& t) are used to represent the time. It is a bit difficult to get started when time is used as the name of the log file. Learn more. Tm structure: struct tm {int tm_sec;/* Second-value range: [] */int tm_min;/* minute-value range: [] */int tm_hour; /* Time-value range: [] */int tm_mday;/* date in a month-value range: [] */int tm_mon; /* month (from January 1, 1900, 0 indicates January 1,)-the value range is [] */int tm_year;/* the year, and the value is equal to the actual year minus */int tm_wday; /* day-value range: [0, 6], where 0 represents Sunday, 1 represents Monday, and so on */int tm_yday; /* days starting from January 1, 0,365-value range: [1,]. 0 indicates January 1, and so on. */int tm_isdst;/* indicates the identifier of the daylight saving time. Tm_isdst is positive. When the sequence is not implemented, the tm_isdst value is 0. If the sequence is unknown, the tm_isdst () value is negative. */} Function for obtaining local time: struct tm * localtime (const time_t * timep); function for obtaining Greenwich Mean Time: struct tm * gmtime (const time_t * timep );, the usage of the two functions is the same. Use localtime as an example to describe # include <time. h> int main () {char * wday [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri ", "Sat"}; time_t timep; struct tm * p; time (& timep); p = localtime (& timep ); // obtain the local time printf ("% d/% d", (1900 + p-> tm_year), (l + p-> tm_mon ), p-> tm_mday); printf ("% s % d: % d \ n", wday [P-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec );} execution result Fri 21:25:34 also has the function size_t strftime converted to the standard format (char * strDest, size_t maxsize, const char * format, const struct tm * timeptr) # include <stdio. h> # include <stdlib. h> # include <string. h> # include <time. h> int main (int argc, char ** argv) {time_t t; struct tm * lt; char nowtime [24]; t = time (NULL ); lt = localtime (& t); memset (nowtime, 0, sizeof (nowtime ); Strftime (nowtime, sizeof (nowtime), "% Y-% m-% d % H: % M: % S", lt ); printf ("nowtime = % s \ n", nowtime); return 0 ;} output nowtime = 21:23:07 can be selected in the format of % a day of the week abbreviation % A day of the week full name % B month abbreviation % B month full name % c standard date time string % C the first two digits of the Year % d decimal represents the day of the month % D month/day/year % e in the two character fields, the last two digits of the day % F-month-day % g Year of the month in decimal format, which are based on the year % G Year of the week, the day of each year % m decimal month in the hour % I 12 hour format of the month name % h 24 hour format % M decimal representation of minutes % n newline operator % p equivalent display of local AM or PM % R 12 hour time % R display hour and minute: hh: mm % S decimal second % t horizontal tab % T display hour Second: hh: mm: ss % u the day of the week, Monday is the first day (value from 1 to 7, Monday is 1) % U the week of the year, take Sunday as the first day (value from 0 to 53) % V the week of the year, the day of the week (value from 0 to 6, sunday is 0) % W the week of the year, Monday as the first day (value from 0 to 53) % x standard date string % X standard time string % y does not contain century's decimal year (value ranges from 0 to 99) % Y with Century's tenth year % z, % Z Time Zone name. If the time zone name cannot be obtained, an empty character is returned. % Percent: int gettimeofday (struct timeval * TV, struct timezone * tz), the current time is returned in the structure indicated by TV, the time zone information is in the structure specified by tz. Struct timeval {long TV _sec;/Second/long TV _usec;/microsecond/}; struct timezone {int tz_minutestwest;/time difference between Greenwich Mean and several minutes/int tz_dsttime;} generally used, set tz to null and only take the TV value. # Include <sys/time. h> # include <stdio. h> int main (void) {int I; struct timeval TV; for (I = 0; I <4; I ++) {gettimeofday (& TV, NULL ); printf ("% d \ t % d \ n", TV. TV _usec, TV. TV _sec); sleep (1);} return 0 ;}