has always been a simple time_t t,time (&t), CTime (&t) to represent time, and later to take time as the name of the log file, it is a little forced. Learn a bit.
TM structure: struct tm{int tm_sec; /* seconds – The value interval is [0,59] */ int tm_min; /*-range of values is [0,59] */ int tm_hour; /*-value interval is [0,23] */ int tm_mday; /* date in one months-range of values is [1,31] */ int Tm_mon; /* month (starting from January, 0 for January)-value range is [0,11] */ int tm_year; /* year with a value equal to the actual year minus 1900 */ int tm_wday; /* week – The value interval is [0,6], where 0 stands for Sunday, 1 for Monday, and so on */ int tm_yday; /* days starting January 1 of each year – the value interval is [0,365], where 0 is January 1, 1 is January 2, and so on */ int tm_isdst; /* daylight saving time identifier, the TM_ISDST is positive when the daylight is implemented. Without daylight saving time, TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */}
function to get local time: struct TM localtime (const time_t TIMEP);
The function of obtaining GMT: struct TM gmtime (const time_t TIMEP);
The usage of the two functions is the same, with LocalTime as an example to illustrate
#include <time.h>intMain () {Char*wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; time_t TIMEP;structTM *p; Time (&TIMEP); p = localtime (&TIMEP);//Get local timeprintf ("%d/%d/%d ", (1900+p->tm_year), (L+p->tm_mon), p->tm_mday); printf"%s%d:%d:%d\n", Wday[p->tm_wday], P->tm_hour, P->tm_min, p->tm_sec);}
Execution results
2016/06/3 Fri 21:25:34
There are also functions converted to the standard format size_t strftime (char strdest, size_t maxsize, const char format, const struct TM *timeptr)
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h> intMainintargcChar**ARGV) {time_t t;structTM *lt;Charnowtime[ -]; 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 = 2016-6-3 21:23:07
the selectable formats are%a shorthand for the day of the week %A Full name of the day of the week %b shorthand for the month %B Full name of the month %c time string of the standard date %c first two digits of the year %d The day ordinal of a month in decimal notation %d Month/day/year %e The day ordinal of a month in a two-character field, in decimal notation %F year-month-day %g the following two digits of the year, using the week-based %G year, using week-based %h Abbreviated month name %H 24-hour hour %I 12-hour hour %j The day ordinal of a year in decimal notation %m the month in decimal notation %M the number of minutes represented by the 10 o'clock system %n New Line character %p equivalent display of the local AM or PM %r 12 hours of time %R display hours and minutes: hh:mm %s number of seconds in decimal %t Horizontal Tab %T display time seconds: Hh:mm:ss %u Day of the week, Monday for the first days (values from 1 to 7, Monday to 1) %u Week of the year, Sunday as the first day (value from 0 to max) %V Week of the year, using week-based %w the day of the Week in decimal (value from 0 to 6, Sunday is 0) %W Week of the year, Monday as the first day (value from 0 to max) %x the standard date string %x Standard Time-series %y decimal Year without century (value from 0 to max) %Y 10 year with century part %z,%z The time zone name that returns a null character if the time zone name cannot be obtained. %% percent percent
Methods for obtaining microsecond time int gettimeofday (struct timeval TV, struct timezone TZ), the current time is returned with the structure referred to by TV, and the time zone information exists in the structure referred to by TZ.
struct timeval{ long tv_sec;/秒/ long tv_usec;/微秒/};struct timezone{ int tz_minutestwest;/和格林威治时间差几分钟/ int tz_dsttime;}
In general use, the set TZ is empty, only take the TV value.
#include <sys/time.h>#include <stdio.h>int main(void){ int i; struct timeval tv; for04; i++) { gettimeofday(&tv, NULL); printf("%d\t%d\n", tv.tv_usec, tv.tv_sec); sleep(1); } return0;}
The study of C-language time function