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\t%d\n", 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\n", 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: UNIX

struct timeval和gettimeofday()的更多相关文章

  1. struct timeval和gettimeofday

    struct timeval和gettimeofday() struct timeval结构体在time.h中的定义为: struct timeval { time_t tv_sec; /* Seco ...

  2. gettimeofday(struct timeval *tv, struct timezone *tz)函数

    gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...

  3. struct timespec 和 struct timeval

    time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...

  4. linux高精度struct timespec 和 struct timeval

    一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...

  5. struct timeval结构体 以及 gettimeofday()函数(转)

    struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...

  6. struct timeval 计时问题

    linux编程中,如果用到计时,可以用struct timeval获取系统时间.struct timeval的函数原型如下: struct timeval { __kernel_time_t tv_s ...

  7. struct timeval 和 struct timespec

    struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...

  8. [c++]struct timeval

    struct timeval { time_t tv_sec; // seconds long tv_usec; // microseconds }; re 1. struct timespec 和 ...

  9. Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday

    说明 获取当前的时间的秒数和微秒数本方法需要用到 gettimeofday() 函数,该函数需要引入的头文件是  <sys/time.h>  . 函数说明 int gettimeofday ...

随机推荐

  1. contextmanager 的基本使用

    from contextlib import contextmanager 简化 With 语句: class MyResource:    def query(self):        print ...

  2. JAVA EE配TOMCAT

    纯粹就是吧百度教程上的过程走了一遍发现不行综合各种教程配出来了,四张图代表了四个阶段,以后再要配的话直接来这里看.

  3. Android 支付宝H5 没有回调

    今天测试反馈问题,说,手机上没有安装支付宝的,调用支付宝支付之后,没有回调.不提示成功也不提示失败. 我自己试了半天也都是没有问题 .后来终于可以试出来了. 发现原来是,清单里面注册的Activity ...

  4. 《数据结构与算法分析:C语言描述》复习——第六章“排序”——冒泡排序

    2014.06.17 01:04 简介: 冒泡排序是O(n^2)级别的交换排序算法,原理简单,属于必知必会的基础算法之一. 思路: 排序要进行N轮,每一轮从尾部逐个向前扫描,遇到逆序对就进行交换.确保 ...

  5. 《Cracking the Coding Interview》——第6章:智力题——题目1

    2014-03-19 06:40 题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药.给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 解法:如果药片管 ...

  6. Git——1.简介

    关于版本控制 Git基础 安装Git 初始运行Git前的配置 获取帮助 关于版本控制 版本控制(VCS)是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 本地版本控制系统 大多都 ...

  7. springboot相关链接

    springboot的三种启动方式 https://blog.csdn.net/my__Sun_/article/details/72866329 springboot学历历程 https://www ...

  8. Linq语法和C#6.0

    一. linq 1.简介: 能用linq实现的基本都可以用扩展方法实现: 举例: 查询ID>1的狗有如下两种写法 (1)var  r1=dogs.where(d=>d.id>1) ( ...

  9. Python全栈工程师(集合、函数)

     ParisGabriel     感谢 大家的支持  你们的阅读评价就是我最好的动力  我会坚持把排版内容以及偶尔的错误做的越来越好        每天坚持 一天一篇 点个订阅吧  灰常感谢    ...

  10. IPMITool driver

    官网链接: https://docs.openstack.org/ironic/latest/admin/drivers/ipmitool.html IPMITool driver 概述IPMI(In ...