zoukankan      html  css  js  c++  java
  • struct timeval和gettimeofday()

    http://www.cppblog.com/lynch/archive/2011/08/05/152520.html

    struct timeval结构体在time.h中的定义为: struct timeval { time_t tv_sec; /* Seconds. */ suseconds_t tv_usec; /* Microseconds. */ }; 其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒

    struct timeval结构体在time.h中的定义为:
    struct timeval
    {
    time_t tv_sec; /* Seconds. */
    suseconds_t tv_usec; /* Microseconds. */
    };
    其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
    int i;
    for (i = 0; i < 4; ++i)
    {
    gettimeofday(&tv, NULL);
    printf("%d %d ", tv.tv_usec, tv.tv_sec);
    sleep(1);
    }
    442388 1244770435
    443119 1244770436
    443543 1244770437
    444153 1244770438
    前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。

    gettimeofday() -- 获取当前时间(保存在结构体timeval中) 
    #include <stdio.h>
    #include <sys/time.h>
    #include <time.h>

    int main(int argc, char * argv[]){

    struct timeval tv; //(1)
    while(1){
    gettimeofday(&tv, NULL); //(2)
    printf("time %u:%u ", tv.tv_sec, tv.tv_usec);
    sleep(2);
    }
    return 0;

    }

    (1)
     struct--timeval
    --------------------------------------------------
    struct timeval {
    time_t tv_sec; /* seconds */
    suseconds_t tv_usec; /* microseconds */
    };
    millisecond 毫秒
    microsecond 微秒

    timeval表示一个时间点,比如:
    timeval.tv_sec = 1 (s)
    timevat.tv_usec = 500 000 (
    μs)
    1:500 = 1s500000
    μs = 1.5s

    (2) gettimeofday()
    --------------------------------------------------
    int gettimeofday(struct timeval *tv, struct timezone *tz);

    The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone. 
    The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL.

    (3) 运行结果:
    --------------------------------------------------
    time 1181788367:991487
    time 1181788369:991602

    表示睡眠2秒经过的精确时间为: 2s115
    μs
     
     
    http://www.6san.com/575/
     

    UNIX时间:新纪元时间(Epoch Time)

    UNIX时间又称POSIX时间/新纪元时间(Epoch Time):从协调世界时1970年1月1日0时0分0秒起到现在的总秒数,不包括闰秒。正值表示1970以後,负值则表示1970年以前。

    Unix 2038 bug(Jason hatchet bug)

    说到UNIX时间不得不提Unix 2038 bug(Jason hatchet bug):2038年1月19日3时14分07秒,32位元系统的UNIX时间将会被重置。

    32位的UNIX系统会以32位二进制数字表示时间,它们最多只能表示至协调世界时间2038年1月19日3时14分07秒(二进制:01111111 11111111 11111111 11111111),在下一秒二进制数字会是10000000 00000000 00000000 00000000,这是负数,因此各系统会把时间误解作1901年12月13日20时45分52秒(亦有说回归到1970年)。这时可能会令软件发生问题,导致系统瘫痪。

    目前解决方案是把系统由32位转为64位系统。在64位系统下,此时间最多可以表示到292,277,026,596年12月4日15时30分08秒。

    wireshark显示时间戳/新纪元时间(Epoch Time)

    依次选择菜单中的“View–>Time Display Fromat–>Seconds Since Epoch (1970-01-01): 1234567890.123456” 即可将wireshark显示的时间格式设置为时间戳、新纪元时间(Epoch Time),自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数。

    转载请注明出处:6san.com 
    原文地址: http://www.6san.com/575/

     

    Tags: 

  • 相关阅读:
    解决RobotFramework的关键字不能高亮的问题
    使用Python遇到:'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte 问题
    通过Jekins执行bat脚本始终无法完成
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
    [转]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    HDU 2686 MCMF
    HDU 4278 卡特兰,区间DP
    POJ 2985 名次树
    POJ 2531 深搜剪枝
    Uva 10061 进制问题
  • 原文地址:https://www.cnblogs.com/xuejinhui/p/4340263.html
Copyright © 2011-2022 走看看