转载于:http://blog.chinaunix.net/uid-25909722-id-2827364.html
 
 

分类: C/C++

在Linux系统中,表示“时间”概念的结构体有多个,相关的时间处理函数也有很多,给人以很混乱的感觉。导致了当我们真正要使用这些结构体和函数的时候,却不知道到底该用哪个结构体和哪些函数。有必要加以归纳总结一下。通过查看头文件/usr/include/time.h 和 /usr/include/bits/time.h
 
(1)我们可以找到下列四种表示“时间”的结构体
  1. /* Returned by `time'. */
  2. typedef __time_t time_t;
  3. /* A time value that is accurate to the nearest
  4. microsecond but also has a range of years. */
  5. struct timeval
  6. {
  7. __time_t tv_sec;       /* Seconds. */
  8. __suseconds_t tv_usec; /* Microseconds. */
  9. };
  10. struct timespec
  11. {
  12. __time_t tv_sec;        /* Seconds. */
  13. long int tv_nsec;       /* Nanoseconds. */
  14. };
  15. struct tm
  16. {
  17. int tm_sec;            /* Seconds.    [0-60] (1 leap second) */
  18. int tm_min;            /* Minutes.    [0-59] */
  19. int tm_hour;           /* Hours.    [0-23] */
  20. int tm_mday;           /* Day.        [1-31] */
  21. int tm_mon;            /* Month.    [0-11] */
  22. int tm_year;           /* Year    - 1900. */
  23. int tm_wday;           /* Day of week.    [0-6] */
  24. int tm_yday;           /* Days in year.[0-365]    */
  25. int tm_isdst;          /* DST.        [-1/0/1]*/
  26. #ifdef    __USE_BSD
  27. long int tm_gmtoff;        /* Seconds east of UTC. */
  28. __const char *tm_zone;     /* Timezone abbreviation. */
  29. #else
  30. long int __tm_gmtoff;       /* Seconds east of UTC. */
  31. __const char *__tm_zone;    /* Timezone abbreviation. */
  32. #endif
  33. };
其中:
time_t 是一个长整型,用来表示秒数。
struct timeval 结构体是用秒和微妙来表示时间。
struct timespec 结构体是用秒和纳秒来表示时间。
struct tm 直接用秒、分、小时、天、月、年等来表示时间。
很显然它们的精度是各不相同的。位各种不同的需要提供各种不同的选择。
 
(2)在Linux系统中我们如何获取当前的时间
#include
time_t time(time_t *t);
可以获取精确到秒的当前距离1970-01-01 00:00:00 +0000 (UTC)的秒数。
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
可以获取精确到微秒当前距离1970-01-01 00:00:00 +0000 (UTC)的微秒数。
#include
int clock_gettime(clockid_t clk_id, struct timespec *tp);
可以获取精确到纳秒当前距离1970-01-01 00:00:00 +0000 (UTC)的纳秒数。
例子:
  1. #include <time.h>
  2. #include <stdio.h>
  3. int main()
  4. {
  5. struct timespec ts;
  6. clock_gettime(CLOCK_REALTIME, &ts);
  7. printf("%.24s %ld Nanoseconds\n", ctime(&ts.tv_sec), ts.tv_nsec);
  8. return 0;
  9. }
编译:gcc -Wall -lrt -o mytime time.c (注意要加入链接选项 -lrt )
运行:./mytime
结果:Tue Sep 13 18:04:44 2011 740953975 Nanoseconds
总结:我们可以通过上面三个函数获得三种不同精度的当前时间。
注:POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of clock_gettime(2) instead.
并且,有人曾经做过测试,连续两次使用gettimeofday时,会以一种小概率出现"时光倒流"的现象,第二次函数调用得到的时间要小于或说早于第一次调用得到的时间。gettimeofday函数并不是那么稳定,没有times或clock计时准确,但它们用法相似。clock有计时限制,据说是 596.5+小时,一般情况足以应付。”(摘录自网上)
“ntpd之类的进程可能会修改系统时间,导致计时出现误差。据网上的讨论来看,TSC和HPET中断之类的东西,可能导致系统的wall time回退。这个应该和具体的系统实现有关了,总之gettimeofday函数并没有保证提供怎样的精度,也不保证得到系统的准确时间,它返回的结果是"the system's best guess at wall time"(摘录自网上)
有可能的话,尽量使用clock_gettime(CLOCK_MONOTONIC),不过不是所有系统都实现了posix realtime,例如mac os x”(摘录自网上)所以现在应该用:int clock_gettime(CLOCK_MONOTONIC, struct timespec *tp);
CLOCK_MONOTONIC:Clock that cannot be set and represents monotonic time since some unspecified starting point.
(3)秒、毫秒、微秒、纳秒之间的转换
1秒 == 1000毫秒
1毫秒 == 1000微秒
1微秒 == 1000纳秒
so:
1秒 == 1000,000微秒(一百万微秒)
1秒 == 1000,000,000纳秒(十亿纳秒)
从秒到毫秒,毫秒到微秒,微秒到纳秒都是1000的倍关系,也就是多3个0的关系。
另:个人电脑的微处理器执行一道指令(如将两数相加)约需2至4纳秒。所以程序只要精确到纳秒就够了。
 
(4)我们如何对时间进行格式化和输出呢?
  1. #include <time.h>
  2. char *asctime(const struct tm *tm);
  3. char *asctime_r(const struct tm *tm, char *buf);
  4. char *ctime(const time_t *timep);
  5. char *ctime_r(const time_t *timep, char *buf);
  6. sizt_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
很显然,要严格控制时间的输出格式,只能用strftime函数,但是该函数只能对struct tm表示的时间进行输出,所以这里要涉及到struct timeval, struct timespec, time_t等表示的时间如何转换成struct tm的形式。另外因为strutc tm只能精确到秒,所i毫秒、微秒、纳秒只能另外进行输出了。
所以,我们采取的方式是:
首先将struct timeval, struct timespec等转换成time_t表示的秒数;
  1. struct timeval tv;
  2. struct timespec ts;
  3. time_t sec_tv = tv.tv_sec;
  4. time_t sec_ts = ts.ts_sec;
然后利用下列函数将time_t转换成struct tm,
  1. struct tm *gmtime(const time_t *timep);
  2. struct tm *gmtime_r(const time_t *timep, struct tm *result);
  3. 或者:
  4. struct tm *localtime(const time_t *timep);
  5. struct tm *localtime_r(const time_t *timep, struct tm *result);
最后利用strftime函数进行格式化,得到最后的时间字符串。
至于毫秒、微秒、纳秒另外用进行输出。
例子:
  1. #include <time.h>
  2. #include <sys/time.h>
  3. #include <stdio.h>
  4. int main()
  5. {
  6. struct timeval tv;
  7. char strTime[32];
  8. gettimeofday(&tv, NULL);
  9. struct tm *ptm = gmtime(&tv.tv_sec);  //将秒转换成struct tm的形式
  10. strftime(strTime, 32, "%F %T", ptm);
  11. printf("%s ", strTime); //输出精确到秒
  12. printf("%ld Micorseconds\n", (long)tv.tv_usec); //输出微秒
  13. return 0;
  14. }
输出结果:2011-09-14 03:22:42 427880 Micorseconds
另:
形如gmtime和形如gmtime_t函数的区别是,gmtime获得的返回的结果存在于一个static的struct tm型的变量中,可能被后面的gmtime调用覆盖掉,如果要防止覆盖,我们可以自己提供一个struct tm 型的变量,利用gmtime_r函数,将我们自己定义的变量的地址传进去,将结果保存在其中。这样就可以避免覆盖。
 
关于ctime和asctime等函数得到的时间字符串,它具有指定的形如("Wed Jun 30 21:49:08 1993\n")的格式,所以不利与我们不能进行格式化。注意该格式的最后具有换行符:'\n'.

Linux中表示“时间”的结构体和相关函数的更多相关文章

  1. Linux中进程控制块PCB-------task_struct结构体结构

    Linux中task_struct用来控制管理进程,结构如下: struct task_struct { //说明了该进程是否可以执行,还是可中断等信息 volatile long state; // ...

  2. linux块设备驱动---相关结构体(转)

    上回最后面介绍了相关数据结构,下面再详细介绍 块设备对象结构 block_device 内核用结构block_device实例代表一个块设备对象,如:整个硬盘或特定分区.如果该结构代表一个分区,则其成 ...

  3. Linux C 程序 预处理,结构体(13)

    C语言预处理,结构体 C语言预处理命令1.宏定义 1.无参数宏 #define 标识符 字符串 #代表本行是编译预处理命名 习惯上,宏定义大写 代替一个字符串,介绍重复书写某个字符串的工作量 有意义的 ...

  4. 浅析 Linux 中的时间编程和实现原理一—— Linux 应用层的时间编程【转】

    本文转载自:http://www.cnblogs.com/qingchen1984/p/7007631.html 本篇文章主要介绍了"浅析 Linux 中的时间编程和实现原理一—— Linu ...

  5. Objective-C中经常使用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect

    Objective-C中经常使用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect 1   NSRange NSRange 的原型为 typedef struct _N ...

  6. FFMPEG中最要害的结构体之间的关系

    FFMPEG中最关键的结构体之间的关系 http://www.myexception.cn/program/1404591.html FFMPEG中结构体很多.最关键的结构体可以分成以下几类: a)  ...

  7. Linux进程管理之task_struct结构体

    进程是处于执行期的程序以及它所管理的资源(如打开的文件.挂起的信号.进程状态.地址空间等等)的总称.注意,程序并不是进程,实际上两个或多个进程不仅有可能执行同一程序,而且还有可能共享地址空间等资源. ...

  8. Linux串口通信之termios结构体说明

    termios结构体中,该结构体一般包括如下的成员:tcflag_t c_iflag;      tcflag_t c_oflag;      tcflag_t c_cflag;      tcfla ...

  9. Linux进程描述符task_struct结构体详解--Linux进程的管理与调度(一)【转】

    Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息.它定义在include/linux/sched.h文件中. 谈到task_str ...

随机推荐

  1. [VIM] 格式化代码

    快速使用vim格式化代码 在vim的编辑模式i下直接ESC退出道命令模式之后直接敲入如下命令: gg=G        将全部代码格式化 nG=mG    将第n行到第m行的代码格式化 注:如果ESC ...

  2. myeclipse 2014新建maven web 项目步骤

    首先在myeclipse中配置maven的安装路径: 在pom.xml中加上: 1 2 3 4 5 6 7 8 9 10  <plugins>           <plugin&g ...

  3. 深入理解计算机系统家庭作业汇总 20135301&&20135328

    深入理解计算机系统家庭作业 深入理解计算机系统第二章家庭作业 题目2.64 题目要求 判断二进制数偶数位是否有任意一位位为1,有的话返回1,否则返回0 解题过程 int any_even_one(un ...

  4. Ubuntu下eclipse安装svn插件

    好记性不如烂笔头,碰见一个不大不小的问题,记录下. 系统:Ubuntu 12.04 Eclipse:eclipse-jee-kepler-R-linux-gtk.tar.gz subclipse:1. ...

  5. [软件测试]Linux环境中简单清爽的Google Test (GTest)测试环境搭建(初级使用)

    本文将介绍单元测试工具google test(GTEST)在linux操作系统中测试环境的搭建方法.本文属于google test使用的基础教程.在linux中使用google test之前,需要对如 ...

  6. 云计算之路-阿里云上:2014年6月12日14:40出现CPU 100%

    这是今天遇到的第2次故障,主站1台云服务器在14:40:33-14:41:09期间出现CPU突然跑满的状况,而CPU跑满之前,请求量并没有明显增长. 向阿里云提交之后,说晚上会对这台云服务器的虚拟机进 ...

  7. 云计算之路-阿里云上:2014年6月11日17点遇到的CPU 100%状况

    今天下午17:00-17:05之间,在请求量没有明显变化的情况下,SLB中的1台云服务器的CPU突然串到100%(当时SLB中一共有3台云服务器),见下图: 造成的直接后果是请求执行时间变得超长,最长 ...

  8. 前端开发之Chrome插件

    Postman - REST Client Postman是Ajax开发的神器,对于Restful开发方式特别有帮助,可以用来模拟各种请求来测试API的正确性,比如用来模拟Ajax请求.它还支持认证, ...

  9. KMP和扩展KMP

    文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...

  10. [AaronYang]C#人爱学不学[2]

    1. 记事本写C#,脱离vs 新建记事本,名字为 helloworld.cs using System; namespace Hello{ public class HelloWorldSay{ st ...