Gettimeofday ()-Get the current time (stored in the struct timeval)

Source: Internet
Author: User

# Include <stdio. h>
# Include <sys/time. h>
# Include <time. h>

Int main (INT argc, char * argv []) {

Struct timeval TV; // (1)
While (1 ){
Gettimeofday (& TV, null); // (2)
Printf ("time % u: % u \ n", TV. TV _sec, TV. TV _usec );
Sleep (2 );
}
Return 0;

}

(1) struct -- timeval
--------------------------------------------------
Struct timeval {
Time_t TV _sec;/* seconds */
Suseconds_t TV _usec;/* microseconds */
};
Millisecond millisecond
Microsecond microseconds

Timeval indicates a time point, for example:
Timeval. TV _sec = 1 (s)
Timevat. TV _usec = 500 000 (μs)
1: 500 = 1s500000 μs = 1.5 s

(2) gettimeofday ()
--------------------------------------------------
Int gettimeofday (struct timeval * TV, struct timezone * tz );

The functions gettimeofday () and settimeofday () can get and set the time as well as a timezone.
The use of the timezone structure is obsolete; the TZ argument shocould normally be specified as null.

(3) running result:
--------------------------------------------------
Time 1181788367: 991487
Time 1181788369: 991602

The exact duration of 2 seconds of sleep is 2s115 μs.

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.