A few time functions commonly used under Linux: Time,gettimeofday,clock_gettime,_ftime

Source: Internet
Author: User
Tags function prototype local time

  1. Time () provides a second level of accuracy
  2. 1. header File <time.h>
  3. 2. Function prototype
  4. time_t Time (time_t * timer)
  5. The function returns the number of seconds starting from Tc1970-1-1 0:0:0 to now
  6. Use the time () function with other functions (such as: LocalTime, Gmtime, Asctime, CTime) to obtain the current system or standard time.
  7. #include <time.h>
  8. #include <stdio.h>
  9. int main (void)
  10. {
  11. time_t T;
  12. T = time (NULL);
  13. printf ("The number of seconds since January 1, 1970 is%ld", t);
  14. return 0;
  15. }
  16. #include <stdio.h>
  17. #include <stddef.h>
  18. #include <time.h>
  19. int main (void)
  20. {
  21. time_t timer; //time_t is a long int type
  22. struct TM *tblock;
  23. Timer = time (NULL);  //This sentence can also be changed to time (&timer);
  24. Tblock = LocalTime (&timer);
  25. printf ("Local time is:%s/n", Asctime (Tblock));
  26. return 0;
  27. }


  1. Gettimeofday () provides microsecond-level accuracy
  2. 1. header File <time.h>
  3. 2. Function prototype
  4. int gettimeofday (struct timeval *tv, struct timezone *tz);
  5. 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).
  6. Parameter description:
  7. The TIMEVAL structure is defined as:
  8. struct Timeval
  9. {
  10. long tv_sec; / * sec * /
  11. long tv_usec; / * microseconds * /
  12. };
  13. The timezone structure is defined as:
  14. struct TimeZone
  15. {
  16. int tz_minuteswest; /* and Greenwich TIME DIFFERENCE how many minutes */
  17. int tz_dsttime; / * Daylight Saving Time status * /
  18. };
  19. Both of these structures are defined in/usr/include/sys/time.h. The status represented by Tz_dsttime is as follows
  20. Dst_none/ * Do not use * /
  21. Dst_usa/ * USA * /
  22. Dst_aust/ * Australia * /
  23. Dst_wet/ * Western Europe *
  24. Dst_met/ * Central Europe *
  25. Dst_eet/ * Eastern Europe *
  26. Dst_can/ * Canada * /
  27. DST_GB/ * Britannia * /
  28. Dst_rum/ * Romania * /
  29. Dst_tur/ * Turkey * *
  30. Dst_austalt /* Australia (after 1986) */
  31. 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.
  32. #include <stdio.h>
  33. #include <time.h>
  34. int main (void)
  35. {
  36. struct Timeval TV;
  37. struct timezone tz;
  38. Gettimeofday (&TV, &tz);
  39. printf ("TV_SEC; %d/n ", tv,.tv_sec);
  40. printf ("TV_USEC; %d/n ", tv.tv_usec);
  41. printf ("Tz_minuteswest; %d/n ", tz.tz_minuteswest);
  42. printf ("Tz_dsttime,%d/n", tz.tz_dsttime);
  43. return 0;
  44. }

  1. Clock_gettime () provides nanosecond-level accuracy
  2. 1. header File <time.h>
  3. 2, compile & link. Add-LRT when compiling links, because Clock_gettime functions are implemented in LIBRT
  4. 3. Function prototype
  5. int Clock_gettime (clockid_t clk_id, struct timespect *tp);
  6. Parameter description:
  7. The clockid_t clk_id is used to specify the type of timing clock, which has the following 4 types:
  8. 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
  9. Clock_monotonic: Starting from the moment the system starts, it is not affected by the change of the system time by the user
  10. CLOCK_PROCESS_CPUTIME_ID: The time that this process takes to the current code system CPU
  11. CLOCK_THREAD_CPUTIME_ID: The time that this thread spends on the current code system CPU
  12. The struct Timespect *tp is used to store the current time, and its structure is as follows:
  13. struct Timespec
  14. {
  15. time_t tv_sec; / * seconds * /
  16. long tv_nsec; / * nanoseconds * /
  17. };
  18. The return value. 0 successes,-1 failures
  19. #include <stdio.h>
  20. #include <time.h>
  21. int main ()
  22. {
  23. struct TIMESPEC ts;
  24. Clock_gettime (Clock_realtime, &ts);
  25. printf ("Clock_realtime:%d,%d", ts.tv_sec, ts.tv_nsec);
  26. Clock_gettime (Clock_monotonic, &ts); //Print out the same time as cat/proc/uptime the first parameter
  27. printf ("Clock_monotonic:%d,%d", ts.tv_sec, ts.tv_nsec);
  28. Clock_gettime (clock_process_cputime_id, &ts);
  29. printf ("clock_process_cputime_id:%d,%d", ts.tv_sec, ts.tv_nsec);
  30. Clock_gettime (clock_thread_cputime_id, &ts);
  31. printf ("clock_thread_cputime_id:%d,%d", ts.tv_sec, ts.tv_nsec);
  32. printf ("/n%d/n", Time (NULL));
  33. return 0;
  34. }
  35. The two numbers inside the/proc/uptime indicate:
  36. The uptime of the system (seconds), and the amount of time spent in idle process (seconds).
  37. Read the first number, which is the time since the system started, the unit is seconds

    1. _ftime () provides millisecond-level accuracy
    2. 1. Header Files <sys/types.h> and <sys/timeb.h>
    3. 2. Function prototype
    4. void _ftime (struct _timeb *timeptr);
    5. Parameter description:
    6. struct _TIMEB
    7. {
    8. time_t time;
    9. unsigned short millitm;
    10. Short timezone;
    11. Short Dstflag;
    12. };
    13. #include <stdio.h>
    14. #include <sys/timeb.h>
    15. #include <time.h>
    16. void Main ( void)
    17. {
    18. struct _TIMEB timebuffer;
    19. Char *timeline;
    20. _ftime (&timebuffer);
    21. Timeline = CTime (& (Timebuffer.time));
    22. printf ( "The time is%.19s.%hu%s", Timeline, TIMEBUFFER.MILLITM, &timeline[20]);
    23. }

A few time functions commonly used under Linux: Time,gettimeofday,clock_gettime,_ftime

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.