时间函数(gettimeofday)
时间函数gettimeofday
gettimeofday 函数是一个用于获取当前时间的UNIX系统调用,它返回自1970年1月1日以来的秒数和微秒数。它的原型如下:
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
参数
tv:一个指向 struct timeval 结构的指针,用于存储当前时间。
tz:一个指向 struct timezone 结构的指针,用于存储时区信息。在现代UNIX系统中,通常将该参数设置为 NULL,因为不再使用时区信息。
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
gettimeofday 函数返回当前时间的秒数和微秒数,分别存储在 tv_sec 和 tv_usec 字段中。
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
int main()
{
struct timeval tv;
gettimeofday(&tv, NULL);
printf("sec = %ld\tusec = %ld\n",tv.tv_sec, tv.tv_usec);
return 0;
}

本文详细介绍了gettimeofday函数,它是UNIX系统中用于获取当前时间的API,返回自1970年1月1日以来的秒数和微秒数。通过C语言示例展示了如何使用这个函数获取并打印时间信息。

194

被折叠的 条评论
为什么被折叠?



