Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1754033
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: C/C++

2011-09-13 17:01:13

在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'.
阅读(13780) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~