Time, gettimeofday, clock_gettime, _ ftime

Source: Internet
Author: User

01. Time () provides second-level precision
02.
03.1 header file <time. h>
04.2 function prototype
05. time_t time (time_t * timer)
06. The function returns the number of seconds from TC1970-1-1 0: 0 to the present
07.
08. Use the time () function combined with other functions (such as localtime, gmtime, asctime, and ctime) to obtain the current system time or standard time.
09.
10. # include <time. h>
11. # include <stdio. h>
12. Int main (void)
13 .{
14. time_t T;
15. t = time (null );
16. printf ("the number of seconds since January 1, 1970 is % lD", t );
17.
18. Return 0;
19 .}
20.
21. # include <stdio. h>
22. # include <stddef. h>
23. # include <time. h>
24. Int main (void)
25 .{
26. time_t timer; // time_t is of the long int type.
27. struct TM * tblock;
28. Timer = Time (null); // This sentence can also be changed to time (& timer );
29. tblock = localtime (& timer );
30. printf ("local time is: % s/n", asctime (tblock ));
31.
32. Return 0;
33 .}
Time () provides second-level precision

1. header file <time. h>
2. function prototype
Time_t time (time_t * timer)
Returns the number of seconds from the TC1970-1-1 0: 0 to the present.

Use the time () function combined with other functions (such as localtime, gmtime, asctime, and ctime) to obtain the current system time 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 of the 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;
}
 

View plaincopy to clipboardprint?
01. gettimeofday () provides microsecond-level precision
02.
03.1 header file <time. h>
04.2 function prototype
05. Int gettimeofday (struct timeval * TV, struct timezone * tz );
06.
07. gettimeofday () will return the current time from the structure indicated by TV, and put the information of the local time zone in the structure indicated by TZ (null is available ).
08. parameter description:
09. The timeval structure is defined:
10. struct timeval
11 .{
12. Long TV _sec;/* seconds */
13. Long TV _usec;/* microseconds */
14 .};
15. The timezone structure is defined:
16. struct timezone
17 .{
18. Int tz_minuteswest;/* the time difference between Greenwich and */
19. Int tz_dsttime;/* state of daylight saving time */
20 .};
21. The above two structures are defined in/usr/include/sys/time. h. The status of tz_dsttime is as follows:
22. dst_none/* not used */
23. dst_usa/* USA */
24. dst_aust/* Australia */
25. dst_wet/* Western Europe */
26. dst_met/* Central Europe */
27. dst_eet/* Eastern Europe */
28. dst_can/* Canada */
29. dst_gb/* Great Britain */
30. dst_rum/* Romania */
31. dst_tur/* Turkey */
32. dst_austalt/* Australia (after January 1, 1986 )*/
33.
34. Return Value: 0 is returned for success,-1 is returned for failure, and the error code is stored in errno. Note that the memory space specified by the efault pointer TV and TZ exceeds the access permission.
35.
36. # include <stdio. h>
37. # include <time. h>
38. Int main (void)
39 .{
40. struct timeval TV;
41. struct timezone TZ;
42.
43. gettimeofday (& TV, & Tz );
44.
45. printf ("TV _sec; % d/N", TV,. TV _sec );
46. printf ("TV _usec; % d/N", TV. TV _usec );
47.
48. printf ("tz_minuteswest; % d/N", tz. tz_minuteswest );
49. printf ("tz_dsttime, % d/N", tz. tz_dsttime );
50.
51. Return 0;
52 .}
Gettimeofday () provides microsecond-level precision

1. header file <time. h>
2. function prototype
Int gettimeofday (struct timeval * TV, struct timezone * tz );

Gettimeofday () will return the current time from the structure indicated by TV, and put the information of the local time zone in the structure indicated by TZ (null is available ).
Parameter description:
The timeval structure is defined:
Struct timeval
{
Long TV _sec;/* seconds */
Long TV _usec;/* microseconds */
};
The timezone structure is defined:
Struct timezone
{
Int tz_minuteswest;/* the time difference between Greenwich and */
Int tz_dsttime;/* state of daylight saving time */
};
The above two structures are defined in/usr/include/sys/time. h. The status of tz_dsttime is as follows:
Dst_none/* not used */
Dst_usa/* United States */
Dst_aust/* Australia */
Dst_wet/* Western Europe */
Dst_met/* Central Europe */
Dst_eet/* Eastern Europe */
Dst_can/* Canada */
Dst_gb/* Great Britain */
Dst_rum/* Romania */
Dst_tur/* Turkey */
Dst_austalt/* Australia (after January 1, 1986 )*/
 
Return Value: 0 is returned for success,-1 is returned for failure, and the error code is stored in errno. Note that the memory space specified by the efault 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;
}

View plaincopy to clipboardprint?
01. clock_gettime () provides the precision of nanoseconds
02.
03.1 header file <time. h>
04.2. Compile & link. -LRT must be added during link compilation, because the clock_gettime function is implemented in librt.
05.3 function prototype
06. Int clock_gettime (clockid_t clk_id, struct timespect * TP );
07. parameter description:
08. clockid_t clk_id is used to specify the time clock type. There are four types:
09. clock_realtime: the real-time of the system, which changes with the real-time of the system, that is, the timing starts from 0: 0 of the UTC1970-1-1. If the system time is set to another time by the user, the corresponding time changes accordingly.
10. clock_monotonic: Timing starts from the moment when the system is started, and is not affected by user changes in the system time.
11. clock_process_cputime_id: time spent by the current process to the CPU of the current Code System
12. clock_thread_cputime_id: Time consumed by the current thread to the CPU of the current Code System
13. struct timespect * TP is used to store the current time. Its structure is as follows:
14. struct timespec
15 .{
16. time_t TV _sec;/* seconds */
17. Long TV _nsec;/* nanoseconds */
18 .};
19. Return Value. 0 successful,-1 failed
20.
21. # include <stdio. h>
22. # include <time. h>
23. Int main ()
24 .{
25. struct timespec ts;
26.
27. clock_gettime (clock_realtime, & TS );
28. printf ("clock_realtime: % d, % d", ts. TV _sec, ts. TV _nsec );
29.
30. clock_gettime (clock_monotonic, & TS); // The printed time is the same as the first parameter of CAT/proc/uptime.
31. printf ("clock_monotonic: % d, % d", ts. TV _sec, ts. TV _nsec );
32.
33. clock_gettime (clock_process_cputime_id, & TS );
34. printf ("clock_process_cputime_id: % d, % d", ts. TV _sec, ts. TV _nsec );
35.
36. clock_gettime (clock_thread_cputime_id, & TS );
37. printf ("clock_thread_cputime_id: % d, % d", ts. TV _sec, ts. TV _nsec );
38.
39. printf ("/n % d/N", time (null ));
40.
41. Return 0;
42 .}
43. The two numbers in/proc/uptime indicate:
44. The uptime of the system (seconds), and the amount of time spent in idle process (seconds ).
45. Read the first number, that is, the time since the system was started, measured in seconds.
Clock_gettime () provides the precision in nanoseconds

1. header file <time. h>
2. Compile and link. -LRT must be added during link compilation, because the clock_gettime function is implemented in librt.
3. function prototype
Int clock_gettime (clockid_t clk_id, struct timespect * TP );
Parameter description:
Clockid_t clk_id is used to specify the clock type. There are four types:
Clock_realtime
Clock_monotonic: Timing starts from the moment when the system is started, and is not affected by changes in the system time.
Clock_process_cputime_id: time spent by the current process to the CPU of the current Code System
Clock_thread_cputime_id: time spent by the current thread to the CPU of the current Code System
Struct timespect * TP is used to store the current time. Its structure is as follows:
Struct timespec
{
Time_t TV _sec;/* seconds */
Long TV _nsec;/* nanoseconds */
};
Return Value. 0 successful,-1 failed

# 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); // The printed time is the same as the first parameter of CAT/proc/uptime.
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 in/proc/uptime indicate:
The uptime of the system (seconds), and the amount of time spent in idle process (seconds ).
Read the first number, that is, the time since the system was started, in seconds.

View plaincopy to clipboardprint?
01. _ ftime () provides millisecond-level precision
02.
03.1 header file <sys/types. h> and <sys/timeb. h>
04.2 function prototype
05. Void _ ftime (struct _ timeb * timeptr );
06. parameter description:
07. struct _ timeb
08 .{
09. time_t time;
10. Unsigned short millitm;
11. Short timezone;
12. Short dstflag;
13 .};
14.
15. # include <stdio. h>
16. # include <sys/timeb. h>
17. # include <time. h>
18.
19. Void main (void)
20 .{
21. struct _ timeb timebuffer;
22. char * timeline;
23.
24. _ ftime (& timebuffer );
25. Timeline = ctime (& (timebuffer. Time ));
26.
27. printf ("the time is %. 19 s. % Hu % s", timeline, timebuffer. millitm, & timeline [20]);

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/sunlylorn/archive/2011/04/10/6313278.aspx

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.