一、struct timeval结构体
struct timeval结构体在time.h中的定义为:
- struct timeval
- {
- __time_t tv_sec; /* Seconds. */
- __suseconds_t tv_usec; /* Microseconds. */
- };
- #include <sys/time.h>
- #include <stdio.h>
-
- int main(void)
- {
- int i;
- struct timeval tv;
-
- for(i = 0; i < 4; i++){
- gettimeofday(&tv, NULL);
- printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
- sleep(1);
- }
-
- return 0;
- }
- 329612 1314851429
- 329782 1314851430
- 329911 1314851431
- 330036 1314851432
二、gettimeofday()函数
原型:
- /* Get the current time of day and timezone information,
- putting it into *TV and *TZ. If TZ is NULL, *TZ is not filled.
- Returns 0 on success, -1 on errors.
- NOTE: This form of timezone information is obsolete.
- Use the functions and variables declared in <time.h> instead. */
- extern int gettimeofday (struct timeval *__restrict __tv,
- __timezone_ptr_t __tz) __THROW __nonnull ((1));
gettimeofday()功能是得到当前时间和时区,分别写到tv和tz中,如果tz为NULL则不向tz写入。