- gettimeofday()
gettimeofday()
可于tv
指向的缓冲区中返回自Epoch(UTC 1970.1.1 00:00:00)以来的秒数,日期存储于time_t
类型的变量,这是SUSv3定义的整数类型)
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone*tz);
Returns 0 on success, or -1 on error
参数tv是指向如下数据结构的一个指针:
struct timeval{
time_t tv_sec; /*Seconds since 00:00:00, 1 Jan 1970 UTC*/
suseconds_t tv_sec; /*Addtional microseconds (long int)*/
}
参数tz已经废弃,使用时传入NULL即可。
2.time()
time()
函数同样返回自Epoch以来的秒数,这是由于历史原因,gettimeofday()
更为精确,后来time()
便实现为对gettimeofday()
的调用。
#include <time.h>
time_t time(time_t *timep);
Returns number of seconds since the Epoch or (time_t)-1 on error
time()
以两种方式返回秒数,参数timep或者返回值,但是为了避免出错,通常简单地这样调用:
time_t t = time(NULL);
参考资料:《Linuc/UNIX系统编程手册》