Linux obtains the current time-general Linux technology-Linux programming and kernel information. The following is a detailed description. Recently, I have been querying about 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 );
Time of conversion cost. It can be used to modify the TZ environment variables on a single machine. 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 ));
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.