# 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.