Time programming in Linux: structtimeval {inttv_sec; inttv_usec;}, a time-related struct in 1 Linux, where TV _sec is the number of seconds counted from the early morning, TV _
Time programming in Linux:
1. time-related struct in Linux
- Struct timeval
- {
- Int TV _sec;
- Int TV _usec;
- };
TV _sec is the number of seconds from the early morning, and TV _usec is the microsecond (10E-6 second ).
- Struct timezone
- {
- Int TV _minuteswest;
- Int TV _dsttime;
- };
TV _minuteswest is the time difference from Greenwich Mean Time to the west, while TV _dsttime is the time correction method.
- Struct timespec
- {
- Long int TV _sec;
- Long int TV _nsec;
- };
TV _nsec is 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;
- };
The number of seconds in the tm_sec table. in the range of [0, 61], two seconds are used to handle the second hop problem.
The number of "points" in the tm_min table, which is between [0, 59.
The "hour" number in the tm_hour table, which is between [0, 23.
The tm_mday table "day of the month" is in the range.
The month of the current year in the tm_mon table, between [0, 11.
Tm_year adds 1900 to indicate that year.
The date of the tm_wday table, which is in the range of [0, 6.
The tm_yday table "the day of the current year". in the range of [0,365], the leap year has 366 days.
Whether the tm_isdst table is daylight saving time 」.
- Struct itimerval
- {
- Struct timeval it_interval;
- Struct timeval it_value;
- };
The it_interval member indicates the initial value of the interval counter, while the it_value member indicates the current value of the interval counter.
2. obtain the current time
In all UNIX systems, there is a time () function.
Time_t time (time_t * t );
This function returns the number of seconds since epoch. if t is non-null, it will enter the time value in t.
For some requirements that require high accuracy, Linux provides gettimeofday ().
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 );
Converts it to Greenwich Mean Time. It is sometimes called GMT or UTC.
Struct tm * localtime (const time_t * t );
The local time of conversion, which can be modified on a machine by modifying the TZ environment variable. different users indicate different times.
Time_t mktime (struct tm * tp );
Convert tm to time_t format and use local time.
Tme_t timegm (strut tm * tp );
Convert tm to time_t format and use UTC time.
Double difftime (time_t t2, time_t t1 );
Calculate the second difference.
3. text Time format functions
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 uses the local time, while asctime uses the timezone information in the tm structure.
Size_t strftime (char * str, size_t max, char * fmt, struct tm * tp );
Strftime is a bit like sprintf, and its format is specified by fmt.
% A: The name of the day.
% A: The full name of the day.
% B: name of the month.
% B: The full name of the month.
% C: The format is the same as that of ctime/asctime.
% D: name of the day of the month, counted from zero.
% H: The Hour of the day, in the 24-hour format, counted from zero.
% I: The Hour of the day, in 12-hour format, counted from zero.
% J: The day of the current year, counted from zero.
% M: The month of the current year, counted from zero.
% M: the fraction of the hour, counted from zero.
% P: AM or PM.
% S: the second of the minute, counted from zero.
% U: The number of the current year, which is calculated from the first day.
% W: The number of the current year, which is calculated from the first one.
% W: The number of days counted from zero.
% X: Local date.
% X: local time.
% Y: two-digit year.
% Y: The four-digit year.
% Z: abbreviation of the time zone name.
%: % Symbol.
Char * strptime (char * s, char * fmt, struct tm * tp );
Like scanf, the interpretation string is in the tm format.
% H: same as % B and % B.
% C: read % x and % X format.
% C: read % C format.
% E: same as % d.
% D: read % m/% d/% y format.
% K: same as % H.
% L: same as % I.
% R: read the format "% I: % M: % S % p.
% R: read the format "% H: % M.
% T: read "% H: % M: % S" format.
% Y: read two-digit year.
% Y: read the four-digit year.
The following example shows how to obtain the current system Time:
- Time_t now;
- Struct tm * timenow;
- Char strtemp [255];
- Time (& now );
- Timenow = localtime (& now );
- Printf ("recent time is: % s n", asctime (timenow ))