Questions about time programming under Linux: 1. Time-related structures under Linux struct Timeval { int tv_sec; int tv_usec; }; Where Tv_sec is the number of seconds from the beginning of the morning, Tv_usec is microseconds (10E-6 second). struct TIMEZONE { int tv_minuteswest; int tv_dsttime; }; Tv_minuteswest is the time difference between GMT and West, and Tv_dsttime is the way to fix it. struct TIMESPEC { long int tv_sec; long int tv_nsec; }; Tv_nsec is the nano second (10E-9 second). struct TM { int tm_sec; int tm_min; int tm_hour; int tm_mday; int Tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; Tm_sec table "Seconds" number, between [0,61], the extra two seconds is used to deal with the problem of jumping seconds. Tm_min table "Minutes", between [0,59]. Tm_hour table "Hours", between [0,23]. Tm_mday table "Day of the Month", between [1,31]. Tm_mon table "Month of the Year", between [0,11]. Tm_year to add 1900 to indicate that year. Tm_wday table "The first few days", between [0,6]. Tm_yday table "The first few days of this year", between [0,365], leap years have 366 days. TM_ISDST table is "Daylight saving Time". struct Itimerval { struct Timeval it_interval; struct Timeval it_value; }; The It_interval member represents the initial value of the interval counter, and the It_value member represents the current value of the interval counter. 2. Get the current time Under all UNIX, there is a time () function time_t time (time_t *t); This function returns the number of seconds that are calculated from the epoch, and if T is non-null, it will fill the time value into T. Linux provides gettimeofday () for certain requirements that require high accuracy. int gettimeofday (struct timeval * tv,struct timezone *tz); int settimeofday (const struct Timeval * tv,const struct timezone *tz); struct TM format time function struct TM * GMTIME (const time_t * t); Converted into Greenwich mean. Sometimes called GMT or UTC. struct TM * localtime (const time_t *t); Convert cost to time. It can be modified by the TZ environment variables in a machine, different users to represent different times. time_t mktime (struct TM *TP); Convert TM into time_t format, using local time. tme_t TIMEGM (Strut TM *TP); Convert TM into time_t format, using UTC time. Double Difftime (time_t t2,time_t t1); Calculates the second difference. 3. Text Time Format function char * asctime (struct TM *TP); char * CTime (struct TM *TP); Both functions convert the time format to the standard UNIX time format. Mon May 3 08:23:35 1999 CTime using local time, asctime is represented by timezone information within the TM structure. size_t strftime (char *str,size_t Max,char *fmt,struct TM *TP); Strftime is a bit like sprintf, whose format is specified by the FMT. %a: The name of the first day, abbreviated. %A: The name of the day of the first. %b: Month name, abbreviation. %B: Month, full name. %c: Same as Ctime/asctime format. %d: The name of the day of the month, calculated from 0. %H: The first few hours of the day, 24-hour system, from 0. %I: The first few hours of the day, 12-hour system, from 0. %j: The first day of the year, from 0. %m: The first few months of the year, from 0. %M: The first part of the hour is counted from 0. %p:am or PM. %s: The second of the minute, calculated from 0. %u: The first day of the year, starting from the beginning of the calculation. %W: The first of the year, calculated from the beginning. %w: When the first day, the 0 is counted. %x: local date. %x: local time. %y: A two-digit year. %Y: four-digit year. %Z: The abbreviation for the time zone name. Percent:% symbol. char * strptime (char *s,char *fmt,struct TM *TP); As with scanf, the interpretation string becomes a TM format. %h: With%b and%b. %c: Reads the%x and%x formats. %c: Read the%c format. %e: Same as%d. %d: Read%m/%d/%y format. %k: With%h. %l: With%i. %r: reads "%i:%m:%s%p" format. %R: reads "%h:%m" format. %T: reads "%h:%m:%s" format. %y: Reads two-digit years. %Y: Reads the four-digit year. Here is a small example of how to get the current time of the system: time_t now; struct TM *timenow; Char strtemp[255]; Time (&now); TimeNow = LocalTime (&now); printf ("Recent time is:%s", Asctime (TimeNow)) |