- Time () provides a second level of accuracy
- 1. header File <time.h>
- 2. Function prototype
- time_t Time (time_t * timer)
- The function returns the number of seconds starting from Tc1970-1-1 0:0:0 to now
- Use the time () function with other functions (such as: LocalTime, Gmtime, Asctime, CTime) to obtain the current system or standard time.
- #include <time.h>
- #include <stdio.h>
- int main (void)
- {
- time_t T;
- T = time (NULL);
- printf ("The number of seconds since January 1, 1970 is%ld", t);
- return 0;
- }
- #include <stdio.h>
- #include <stddef.h>
- #include <time.h>
- int main (void)
- {
- time_t timer; //time_t is a long int type
- struct TM *tblock;
- Timer = time (NULL); //This sentence can also be changed to time (&timer);
- Tblock = LocalTime (&timer);
- printf ("Local time is:%s/n", Asctime (Tblock));
- return 0;
- }
- Gettimeofday () provides microsecond-level accuracy
- 1. header File <time.h>
- 2. Function prototype
- int gettimeofday (struct timeval *tv, struct timezone *tz);
- Gettimeofday () returns the current time from the structure that the TV refers to, and the local time zone information is placed in the structure that TZ refers to (null is available).
- Parameter description:
- The TIMEVAL structure is defined as:
- struct Timeval
- {
- long tv_sec; / * sec * /
- long tv_usec; / * microseconds * /
- };
- The timezone structure is defined as:
- struct TimeZone
- {
- int tz_minuteswest; /* and Greenwich TIME DIFFERENCE how many minutes */
- int tz_dsttime; / * Daylight Saving Time status * /
- };
- Both of these structures are defined in/usr/include/sys/time.h. The status represented by Tz_dsttime is as follows
- Dst_none/ * Do not use * /
- Dst_usa/ * USA * /
- Dst_aust/ * Australia * /
- Dst_wet/ * Western Europe *
- Dst_met/ * Central Europe *
- Dst_eet/ * Eastern Europe *
- Dst_can/ * Canada * /
- DST_GB/ * Britannia * /
- Dst_rum/ * Romania * /
- Dst_tur/ * Turkey * *
- Dst_austalt /* Australia (after 1986) */
- Return value: Success returns 0, failure returns-1, error code is stored in errno. Additional instructions efault the memory space referred to by the pointer TV and TZ exceeds the access permission.
- #include <stdio.h>
- #include <time.h>
- int main (void)
- {
- struct Timeval TV;
- struct timezone tz;
- Gettimeofday (&TV, &tz);
- printf ("TV_SEC; %d/n ", tv,.tv_sec);
- printf ("TV_USEC; %d/n ", tv.tv_usec);
- printf ("Tz_minuteswest; %d/n ", tz.tz_minuteswest);
- printf ("Tz_dsttime,%d/n", tz.tz_dsttime);
- return 0;
- }
- Clock_gettime () provides nanosecond-level accuracy
- 1. header File <time.h>
- 2, compile & link. Add-LRT when compiling links, because Clock_gettime functions are implemented in LIBRT
- 3. Function prototype
- int Clock_gettime (clockid_t clk_id, struct timespect *tp);
- Parameter description:
- The clockid_t clk_id is used to specify the type of timing clock, which has the following 4 types:
- Clock_realtime: System real-time, with the system changes in real time, that is, from the utc1970-1-1 0:0:0 start timing, intermediate time if the system time by the user to other, the corresponding time corresponding change
- Clock_monotonic: Starting from the moment the system starts, it is not affected by the change of the system time by the user
- CLOCK_PROCESS_CPUTIME_ID: The time that this process takes to the current code system CPU
- CLOCK_THREAD_CPUTIME_ID: The time that this thread spends on the current code system CPU
- The struct Timespect *tp is used to store the current time, and its structure is as follows:
- struct Timespec
- {
- time_t tv_sec; / * seconds * /
- long tv_nsec; / * nanoseconds * /
- };
- The return value. 0 successes,-1 failures
- #include <stdio.h>
- #include <time.h>
- int main ()
- {
- struct TIMESPEC ts;
- Clock_gettime (Clock_realtime, &ts);
- printf ("Clock_realtime:%d,%d", ts.tv_sec, ts.tv_nsec);
- Clock_gettime (Clock_monotonic, &ts); //Print out the same time as cat/proc/uptime the first parameter
- printf ("Clock_monotonic:%d,%d", ts.tv_sec, ts.tv_nsec);
- Clock_gettime (clock_process_cputime_id, &ts);
- printf ("clock_process_cputime_id:%d,%d", ts.tv_sec, ts.tv_nsec);
- Clock_gettime (clock_thread_cputime_id, &ts);
- printf ("clock_thread_cputime_id:%d,%d", ts.tv_sec, ts.tv_nsec);
- printf ("/n%d/n", Time (NULL));
- return 0;
- }
- The two numbers inside the/proc/uptime indicate:
- The uptime of the system (seconds), and the amount of time spent in idle process (seconds).
- Read the first number, which is the time since the system started, the unit is seconds
- _ftime () provides millisecond-level accuracy
- 1. Header Files <sys/types.h> and <sys/timeb.h>
- 2. Function prototype
- void _ftime (struct _timeb *timeptr);
- Parameter description:
- struct _TIMEB
- {
- time_t time;
- unsigned short millitm;
- Short timezone;
- Short Dstflag;
- };
- #include <stdio.h>
- #include <sys/timeb.h>
- #include <time.h>
- void Main ( void)
- {
- struct _TIMEB timebuffer;
- Char *timeline;
- _ftime (&timebuffer);
- Timeline = CTime (& (Timebuffer.time));
- printf ( "The time is%.19s.%hu%s", Timeline, TIMEBUFFER.MILLITM, &timeline[20]);
- }
A few time functions commonly used under Linux: Time,gettimeofday,clock_gettime,_ftime