gettimeofday() 函数原型如下:
[C++] 纯文本查看 复制代码 #include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
该函数把当前的时间返回到由参数 tv 所指向的介机构中,当地的失去信息则放在 tz 所指向的结构中。
timeval 结构定义如下:
[C++] 纯文本查看 复制代码 struct timeval {
time_t tv_sec; /* 秒 */
suseconds_t tv_usec; /* 毫秒 */
};
timezone 结构定义如下:
[C++] 纯文本查看 复制代码 struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
tz_dsttime 所表示的状态有: ST_NONE /* 不使用 */
DST_USA /* 美国 */
DST_AUST /* 澳洲 */
DST_WET /* 西欧 */
DST_MET /* 中欧 */
DST_EET /* 东欧 */
DST_CAN /* 加拿大 */
DST_GB /* 大不列颠 */
DST_RUM /* 罗马尼亚*/
DST_TUR /* 土耳其*/
DST_AUSTALT /* 澳洲(1986年后) */ 函数成功时返回 0 ,失败返回 -1, 可以通过错误码 error 查看发生了什么错误,如 EFAULT 表示 tv 或 tz 指向的内存空间超出了访问权限。
测试代码:
[C++] 纯文本查看 复制代码 #include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
int main()
{
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;
}
运行输出:./gettimeofday
tv_sec: 1324369876
tv_usec: 279337
tz_minuteswest: 0
tz_dsttime: 0 |