gettimeofday()函数的使用方法 |
您所在的位置:网站首页 › c语言获取时区 › gettimeofday()函数的使用方法 |
1.简介: 在C语言中可以使用函数gettimeofday()函数来得到精确时间。它的精度可以达到微妙,是C标准库的函数。 2.函数原型: #include int gettimeofday(struct timeval*tv,struct timezone *tz ) 3.说明:gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中 4.结构体:1. timeval 结构体定义: struct timeval{ long tv_sec; /*秒*/ long tv_usec; /*微妙*/ };2. timezone 结构定义: struct timezone{ int tz_minuteswest;/*和greenwich 时间差了多少分钟*/ int tz_dsttime; /*type of DST correction*/ }3>在gettimeofday()函数中tv或者tz都可以为空。如果为空则就不返回其对应的结构体。 4>函数执行成功后返回0,失败后返回-1,错误代码存于errno中。 5.程序实例: #include #include #include void hello_world(void) { printf("Hello world!!!!\r\n"); } int main(void) { struct timeval tv_begin,tv_end; gettimeofday(&tv_begin,NULL); hello_world(); gettimeofday(&tv_end,NULL); printf(“tv_begin_sec:%d\n”,tv_begin.tv_sec); printf(“tv_begin_usec:%d\n”,tv_begin.tv_usec); printf(“tv_end_sec:%d\n”,tv_end.tv_sec); printf(“tv_end_usec:%d\n”,tv_end.tv_usec); return 0; }说明:在使用gettimeofday()函数时,第二个参数一般都为空,因为我们一般都只是为了获得当前时间,而不用获得timezone的数值。
|
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |