这篇文章给大家分享的是有关linux中gettimeofday()函数怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
gettimeofday() 获取当前时间
函数原型如下:
1 #include 2
3 int gettimeofday(struct timeval *tv, struct timezone *tz);
4
5 struct timeval {
6 time_t tv_sec; /* seconds (秒)*/
7 suseconds_t tv_usec; /* microseconds(微秒) */
8 };
9 struct timezone {
10 int tz_minuteswest; /* minutes west of Greenwich */
11 int tz_dsttime; /* type of DST correction */
12 };
13 //gettimeofday函数获取当前时间存于tv结构体中,相应的时区信息则存于tz结构体中
14 //需要注意的是tz是依赖于系统,不同的系统可能存在获取不到的可能,因此通常设置为NULL
eg:
1 #include 2 #include 3 #include 4
5 int main()
6 {
7 struct timeval tv;
8
9 gettimeofday(&tv, NULL);
10
11 printf("tv_sec: %d\n", tv.tv_sec);
12 printf("tv_usec: %d\n", tv.tv_usec);
13
14 return 0;
15 }
感谢各位的阅读!关于“linux中gettimeofday()函数怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!