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为微秒数,即秒后面的零头。比如当前我写博文时的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秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
#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
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()的更多相关文章
- struct timeval和gettimeofday
struct timeval和gettimeofday() struct timeval结构体在time.h中的定义为: struct timeval { time_t tv_sec; /* Seco ...
- gettimeofday(struct timeval *tv, struct timezone *tz)函数
gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- linux高精度struct timespec 和 struct timeval
一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...
- struct timeval结构体 以及 gettimeofday()函数(转)
struct timeval结构体 转载地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html 该结构体是Linux系统中定义,struct ...
- struct timeval 计时问题
linux编程中,如果用到计时,可以用struct timeval获取系统时间.struct timeval的函数原型如下: struct timeval { __kernel_time_t tv_s ...
- struct timeval 和 struct timespec
struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...
- [c++]struct timeval
struct timeval { time_t tv_sec; // seconds long tv_usec; // microseconds }; re 1. struct timespec 和 ...
- Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday
说明 获取当前的时间的秒数和微秒数本方法需要用到 gettimeofday() 函数,该函数需要引入的头文件是 <sys/time.h> . 函数说明 int gettimeofday ...
随机推荐
- 第三章 文件 I/O
3.1 引言 先说明可用的文件 I/O 函数:open.read.write.close,然后说明不同缓冲区长度对read和write函数的影响. 本章所说的函数经常被称为不带缓冲的 I/O (unb ...
- POJ :3614-Sunscreen
传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- python-8错误调试测试
1-错误处理 import logging try: print('try.......') r = 10/0 except ValueError as e: print('result:', e) ...
- 笔记-python-元类metaclass
笔记-python-元类metaclass 1. 元类 1.1. 类也是对象 class Person(object): pass 上面的代码会在内存中创建一个类,它也是对象, 可以将 ...
- java实时监听日志写入kafka
目的 实时监听某目录下的日志文件,如有新文件切换到新文件,并同步写入kafka,同时记录日志文件的行位置,以应对进程异常退出,能从上次的文件位置开始读取(考虑到效率,这里是每100条记一次,可调整) ...
- 一、MySQL数据库之简介和安装
一.基础部分 1.数据库是简介 之前所学,数据要永久保存,比如用户注册的用户信息,都是保存于文件中,而文件只能存在于某一台机器上. 如果我们不考虑从文件中读取数据的效率问题,并且假设我们的程序 ...
- 批量上传图片(jQuery-File-Upload使用)
jQuery-File-Upload jQuery-File-Upload是一个jquery下的ajax文件上传插件,支持批量上传,github地址:https://github.com/blueim ...
- Java课程目录
Java课程目录 1 年前 大家收藏一下这篇文章,这是所有课程的目录. 第一周: 第一课:进制及计算机表示 第二课:进制习题课,进一步介绍进制的知识 第三节课:Java语言的品味(一),通过stati ...
- Spring进阶—如何用Java代码实现邮件发送(一)
相关文章: <Spring进阶—如何用Java代码实现邮件发送(二)> 在一些项目里面如进销存系统,对一些库存不足发出预警提示消息,招聘网站注册用户验证email地址等都需要用到邮件发送技 ...
- linux_shell基础-变量、数组、运算符
#!/bin/bash# echo 'hello world'# name='http://www.baidu.com'# for file in $(ls ./); do# echo "t ...