In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "Linux time function" article, so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Linux time function" article.
1. Summary of linux time function under linux, the commonly used functions to obtain time are as follows:
Asctime, ctime, gmtime, localtime, gettimeofday
Mktime, asctime_r, ctime_r, gmtime_r, localtime_r
2. Commonly used structures (1) struct tm
1 struct tm {2 int tm_sec; / * seconds * / 3 int tm_min; / * minutes * / 4 int tm_hour; / * hours * / 5 int tm_mday; / * day of the month * / 6 int tm_mon / * month * / 7 int tm_year; / * year * / 8 int tm_wday; / * day of the week * / 9 int tm_yday; / * day in the year * / 10 int tm_isdst; / * daylight saving time * / 11} 12 13 / / int tm_sec represents current seconds, normal range is 0-59, but allowed to 61 seconds 14 / / int tm_min represents current score, range 0-59 15 / int tm_hour hours from midnight, range 0-23 16 / / int tm_mday current month days, range 01-31 17 / / int tm_mon represents current month, from January Range from 0-11 18 / / int tm_year the number of days per week in 19 / / int tm_wday since 1900, from Monday, the range is 0-6 20 / / int tm_yday the number of days since January 1 this year, the range is 0-36521 / / the flag of int tm_isdst daylight saving time (2) struct timeval,struct timezone
1 struct timeval {2 time_t tv_sec; / * seconds (seconds) * / 3 suseconds_t tv_usec; / * microseconds (microseconds) * / 4}; 5 struct timezone {6 int tz_minuteswest; / * minuteswest of Greenwich * / 7 int tz_dsttime / * type of DST correction * / 8}; 9 int tz_minuteswest; / * time difference from Greenwich mean time to the west * / 10 int tz_dsttime; / * time correction * / 3. Introduction to time function: (1) time () function to get the current time
1 SYNOPSIS 2 # include 3 4 time_t time (time_t * t); 5 6 DESCRIPTION 7 time () returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 + 0000 (UTC). 8 / / this function returns the number of seconds that have elapsed since 00:00:00 of UTC time on January 1, 1970. If t is not a null pointer, this function also stores the return value to the memory referred to by the t pointer. 9 RETURN VALUE 10 On success, the value of time in seconds since the Epoch is returned. On error, (time_t)-1) is returned, and errno is 11 set appropriately. 12 ERRORS 13 EFAULT t points outside your accessible address space. 14 / / the number of seconds returned for success, and the error returns (time_t)-1). The cause of the error is stored in errno eg:
1 # include 2 # include 3 # include 4 5 int main () 6 {7 time_t seconds; 8 9 seconds = time ((time_t *) NULL); 10 printf ("% d\ n", seconds); 11 12 return 0; 13} (2) localtime_r () localtime () gets the local current time and date
The function prototype is as follows:
1 # include 2 3 struct tm * localtime (const time_t * timep); 4 struct tm * localtime_r (const time_t * timep, struct tm * result) 5 6 / * this function will have the value obtained by the time function timep to convert the time and date representation used in the real world, and then return the result from the structure tm to * / 7 8 / * * it should be noted that the localtime function can convert time to local time, but the localtime function is not thread safe. 9 in multithreaded applications, the localtime_r function should be used instead of the localtime function, because localtime_r is thread-safe * * / eg:
1 # include 2 # include 3 # include 4 5 int main () 6 {7 time_t timep; 8 struct tm * p; 9 10 time (& timep); 11 p = localtime (& timep) 12 13 printf ("% d-%d-%d% d:%d:%d\ n", (1900 + p-> tm_year), (1 + p-> tm_mon), p-> tm_mday, 14 (p-> tm_hour + 12), p-> tm_min, p-> tm_sec); 15 16 return 0 17} (3) asctime () asctime_r () returns the time and date as a string'
The function prototype is as follows:
1 # include 2 3 struct tm * gmtime (const time_t * timep); 4 struct tm * gmtime_r (const time_t * timep, struct tm * result); 56 char * asctime (const struct tm * tm); 7 char * asctime_r (const struct tm * tm, char * buf); 8 9 10 / * gmtime is a function of converting date and time into GMT time. Convert the information in the time_t structure referred to by the parameter time to the real-world time and date representation method, and then return the result from the structure tm to * * / 11 12 / * asctime and return the time to * * / eg in string format:
1 # include 2 # include 3 # include 4 5 int main () 6 {7 time_t timep; 8 time (& timep); 9 10 printf ("% s\ n", asctime (& timep)); 11 12 return 0; 13} (4) ctime (), ctime_r () represents the time and date in string format
The function prototype is as follows:
1 # include 2 3 char * ctime (const time_t * timep); 4 char * ctime_r (const time_t * timep, char * buf); 56 / * * ctime () converts the information in the time_t structure referred to by the parameter timep into the time and date representation used in the real world, and then returns the result as a string * * / eg:
1 # include 2 # include 3 # include 45 int main (void) 6 {7 time_t timep; 8 9 time (& timep); 10 printf ("% s\ n", ctime (& timep)); 11 12 return 0; 13} (5) mktime () converts the value of the time structure struct tm into the number of seconds elapsed
Function prototype:
1 # include 2 3 time_t mktime (struct tm * tm); 4 5 / * * convert the value of the time structure struct tm into the number of seconds elapsed * * / eg:
1 # include 2 # include 3 # include 45 int main () 6 {7 time_t timep; 8 struct tm * p; 9 10 time (& timep); 11 p = localtime (& timep); 12 timep = mktime (p); 13 14 printf ("% d\ n", timep); 15 16 return 0; 17} the final result shows that the time after mktime conversion is the same as that obtained by time function
(6) gettimeofday () gets the current time
The function prototype is as follows:
1 # include 2 3 int gettimeofday (struct timeval * tv, struct timezone * tz); 4 5 struct timeval {6 time_t tv_sec; / * seconds (seconds) * / 7 suseconds_t tv_usec; / * microseconds (microseconds) * / 8}; 9 struct timezone {10 int tz_minuteswest / * minutes west of Greenwich * / 11 int tz_dsttime; / * type of DST correction * / 12}; 13 / / the current time obtained by the gettimeofday function is stored in the tv structure, while the corresponding time zone information is stored in the tz structure. 14 / / it should be noted that tz depends on the system, and different systems may not be able to obtain it, so it is usually set to NULLeg:
1 # include 2 # include 3 # include 4 5 int main () 6 {7 struct timeval tv; 8 9 gettimeofday (& tv, NULL); 10 11 printf ("tv_sec:% d\ n", tv.tv_sec); 12 printf ("tv_usec:% d\ n", tv.tv_usec); 13 14 return 0 15} the above is about the content of this article on "what are the time functions of Linux". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.