Gcc 中的 gettimeofday 函数使用

来源:互联网 发布:男朋友宠我知乎 编辑:程序博客网 时间:2024/06/14 04:16
gettimeofday
表头文件:#include <sys/time.h>
函数原型:int gettimeofday(struct timeval *tv,struct timezone *tz)
函数说明:把目前的时间按tv所指的结构返回,当时地区的信息则放到tz所指的结构中
struct timeval
{
   long tv_sec; //秒
   long tv_usec;//微妙
}
struct timezone
{
   int tz_minuteswest;//和格林威治时间差了多少分钟
   int tz_dsttime;    //日光节约时间的状态
}

学习代码
#include <stdio.h>#include <sys/time.h>int main(int argc, char** argv){    timeval tv;    gettimeofday(&tv, NULL);    double cl = tv.tv_sec + (double)tv.tv_usec / 1000000;    getchar(); //暂停等待    gettimeofday(&tv, NULL);    cl = (tv.tv_sec + (double)tv.tv_usec / 1000000) - cl;    printf("暂停时间:%f\n", cl);    struct timezone tz;    gettimeofday(&tv, &tz);    printf("tv.tv_sec:%d\n", tv.tv_sec);    printf("tv.tv_usec:%d\n", tv.tv_usec);    printf("tz.tz_minuteswest:%d\n", tz.tz_minuteswest);    printf("tv.tz_dsttime:%d\n", tz.tz_dsttime);    return 0;}