Reproduced in: http://blog.chinaunix.net/uid-25909722-id-2827364.html architecture and related functions representing "Time" in Linux 2011-09-13 17:01:13
Category: C/
In the Linux system, there are many structures representing the concept of "time", and there are a lot of related time-processing functions, which gives people a very confusing feeling. Cause when we really want to use these structures and functions, we do not know exactly which structure and which functions to use. It is necessary to summarize. By looking up documents/usr/include/time.h and/usr/include/bits/time.h
(1)We can find the following four types of representations
structure of "time":
- /* Returned by ' time '. */
- typedef __time_t time_t;
- /* A time value that's accurate to the nearest
- Microsecond but also have a range of years. */
- struct Timeval
- {
- __time_t tv_sec; /* Seconds. */
- __suseconds_t tv_usec; /* microseconds. */
- };
- struct TIMESPEC
- {
- __time_t tv_sec; /* Seconds. */
- long int tv_nsec; /* nanoseconds. */
- };
- struct TM
- {
- int tm_sec; /* Seconds. [0-60] (1 leap second) */
- int tm_min; /* Minutes. [0-59] */
- int tm_hour; /* Hours. [0-23] */
- int tm_mday; /* Day. [1-31] */
- int Tm_mon; /* Month. [0-11] */
- int tm_year; /* Year-1900. */
- int tm_wday; /* Day of week. [0-6] */
- int tm_yday; /* days in year. [0-365] */
- int tm_isdst; /* DST. [ -1/0/1]*/
- #ifdef __USE_BSD
- long int tm_gmtoff; /* Seconds east of UTC. */
- __const Char *tm_zone; /* Timezone abbreviation. */
- #else
- long int __tm_gmtoff; /* Seconds east of UTC. */
- __const Char *__tm_zone; /* Timezone abbreviation. */
- #endif
- };
whichtime_t is a long integer used to represent the number of seconds. struct Timeval structures are expressed in seconds and subtleties of time. the struct timespec struct represents time in seconds and nanoseconds. The struct TM represents time directly in seconds, minutes, hours, days, months, years, and so on. It is clear that their accuracy is different. A variety of different needs to provide a variety of different options.
(2)In a Linux system, how do we
gets the current time? #include
time_t Time (time_t *t);You can get the number of seconds that are accurate to the current distance 1970-01-01 00:00:00 +0000 (UTC). #include
int gettimeofday (struct timeval *tv, struct timezone *tz);You can obtain a microsecond number that is accurate to the current distance of 1970-01-01 00:00:00 +0000 (UTC). #include
int Clock_gettime (clockid_t clk_id, struct timespec *tp);You can get the number of nanoseconds that are accurate to nanosecond current distance 1970-01-01 00:00:00 +0000 (UTC). Example:
- #include <time.h>
- #include <stdio.h>
- int main ()
- {
- struct TIMESPEC ts;
- Clock_gettime (Clock_realtime, &ts);
- printf ("%.24s%ld nanoseconds\n", CTime (&ts.tv_sec), ts.tv_nsec);
- return 0;
- }
Compile: Gcc-wall-lrt-o mytime time.c (note to join link options-LRT) Run:./mytime results: Tue Sep 18:04:44 740953975 nanosecondsSummary: We can get the current time of three different precision through the above three functions.
Note:posix.1-2008 marks Gettimeofday () as obsolete, recommending the use of clock_gettime (2) instead.and, someone has done a test, two consecutive times using Gettimeofday, will be a small probability of "time back" phenomenon, the second function call to get less than or say earlier than the first tuneUsed to get the time. The Gettimeofday function is not so stable, no times or clock timing is accurate, but they are similar in usage. Clock has a time limit, said to be 596.5+ hours, the general situation is sufficient to cope. "(Excerpt from Online)"processes such as ntpd may modify system time, resulting in timing errors。 According to the discussion on the Internet,TSC and Hpet interrupt things that could cause the system to wall time fallback。 This should be related to the specific system implementation, in short, the Gettimeofday function does not guarantee the accuracy of the provided, nor guarantee the accurate time of the system,it returns the result "the system's best guess at wall time""(excerpt from online) if possible, try to use Clock_gettime (clock_monotonic), but not all systems implement POSIX realtime, such as Mac OS X" (excerpt from the web) and should now be used:int Clock_gettime (clock_monotonic, struct timespec *tp); clock_monotonic: CLOCK that cannot be set and represents monotonic time since some UNSPECIF IED starting point.
(3)Conversion between seconds, milliseconds, microseconds, nanoseconds 1 seconds = = 1000 msec 1 msec = 1000 microseconds 1 microseconds = 1000 nanoseconds so:1 seconds = 1000,000 microseconds (1 million microseconds) 1 seconds = 1000,000,000 nanoseconds (1 billion nanoseconds)
from seconds to milliseconds, milliseconds to microseconds, microseconds to nanoseconds are 1000 times as many as 3 0 relationships.
another: The microprocessor of a personal computer executes a directive (such as adding two numbers) to approximately 2 to 4 nanoseconds. So the program just has to be accurate to nanosecond enough.
(4)How do we do the time
Formatting and outputIt?
- #include <time.h>
- Char *asctime (const struct TM *TM);
- Char *asctime_r (const struct TM *tm, char *buf);
- Char *ctime (const time_t *TIMEP);
- Char *ctime_r (const time_t *TIMEP, char *buf);
- sizt_t strftime (char *s, size_t max, const char *format, const struct TM *TM);
Obviously, to strictly control the output format of the time, you can only use the Strftime function, but the function can only be used to output the time that the struct TM represents, so it is related to struct timeval, struct timespec, time_ How the time represented by T is converted into a struct TM form. In addition, because the STRUTC TM can only be accurate to the second, I milliseconds, microseconds, nanoseconds can only be output separately. So, the way we take it is:
FirstConvert the struct timeval, struct timespec etc into the number of seconds represented by time_t;
- struct Timeval TV;
- struct TIMESPEC ts;
- time_t SEC_TV = tv.tv_sec;
- time_t sec_ts = ts.ts_sec;
thenConvert time_t into a struct TM using the following functions
- struct TM *gmtime (const time_t *TIMEP);
- struct TM *gmtime_r (const time_t *TIMEP, struct TM *result);
- Or:
- struct TM *localtime (const time_t *TIMEP);
- struct TM *localtime_r (const time_t *TIMEP, struct TM *result);
finallyUse the Strftime function to format and get the last time string. For milliseconds, microseconds, nanoseconds, the output is also used. Example:
- #include <time.h>
- #include <sys/time.h>
- #include <stdio.h>
- int main ()
- {
- struct Timeval TV;
- Char strtime[32];
- Gettimeofday (&TV, NULL);
- struct TM *ptm = gmtime (&tv.tv_sec); Converts seconds into a struct TM form
- Strftime (Strtime, +, "%F%T", PTM);
- printf ("%s", strtime); Output accurate to seconds
- printf ("%ld micorseconds\n", (long) tv.tv_usec); Output microseconds
- return 0;
- }
Output result: 2011-09-14 03:22:42 427880 micorseconds
another:Shaped likethe difference between the gmtime and the shape of the gmtime_t function is that the returned result of the gmtime is present in a static struct TM variable, which may be overwritten by a subsequent gmtime call, and we can provide ourselves a struct if we want to prevent overwriting. A tm-type variable that uses the Gmtime_r function to pass in the address of a variable of our own definition and save the result in it. This will avoid overwriting. AboutCTime andAsctime such functions get the time string, which has the specified shape such as ("Wed June 30 21:49:08 1993
\ n") format, so unfavorable with us cannot be formatted.Note that the format ends with a newline character: ' \ n '.
Structure and related functions representing "Time" in Linux