您的位置:首页 > 运维架构 > Linux

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

2016-06-14 18:24 411 查看
转载于:http://blog.chinaunix.net/uid-25909722-id-2827364.html

Linux中表示“时间”的结构体和相关函数 2011-09-13 17:01:13

分类: C/C++

在Linux系统中,表示“时间”概念的结构体有多个,相关的时间处理函数也有很多,给人以很混乱的感觉。导致了当我们真正要使用这些结构体和函数的时候,却不知道到底该用哪个结构体和哪些函数。有必要加以归纳总结一下。通过查看头文件/usr/include/time.h 和 /usr/include/bits/time.h

(1)我们可以找到下列四种表示“时间”的结构体

/* Returned by `time'. */

typedef __time_t time_t;

/* A time value that is accurate to the nearest

microsecond but also has a range of years. */

struct timeval

{

__time_t tv_sec; /* Seconds. */

__suseconds_t tv_usec; /* Microseconds. */

};

struct timespec

{

__time_t tv_sec; /* Seconds. */

long int tv_nsec; /* Nanoseconds. */

};

struct tm

{

int tm_sec; /* Seconds. [0-60] (1 leap second) */

int tm_min; /* Minutes. [0-59] */

int tm_hour; /* Hours. [0-23] */

int tm_mday; /* Day. [1-31] */

int tm_mon; /* Month. [0-11] */

int tm_year; /* Year - 1900. */

int tm_wday; /* Day of week. [0-6] */

int tm_yday; /* Days in year.[0-365] */

int tm_isdst; /* DST. [-1/0/1]*/

#ifdef __USE_BSD

long int tm_gmtoff; /* Seconds east of UTC. */

__const char *tm_zone; /* Timezone abbreviation. */

#else

long int __tm_gmtoff; /* Seconds east of UTC. */

__const char *__tm_zone; /* Timezone abbreviation. */

#endif

};

其中:
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)的纳秒数。
例子:

#include <time.h>

#include <stdio.h>

int main()

{

struct timespec ts;

clock_gettime(CLOCK_REALTIME, &ts);

printf("%.24s %ld Nanoseconds\n", ctime(&ts.tv_sec), ts.tv_nsec);

return 0;

}

编译: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)我们如何对时间进行格式化和输出呢?

#include <time.h>

char *asctime(const struct tm *tm);

char *asctime_r(const struct tm *tm, char *buf);

char *ctime(const time_t *timep);

char *ctime_r(const time_t *timep, char *buf);

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表示的秒数;

struct timeval tv;

struct timespec ts;

time_t sec_tv = tv.tv_sec;

time_t sec_ts = ts.ts_sec;

然后利用下列函数将time_t转换成struct tm,

struct tm *gmtime(const time_t *timep);

struct tm *gmtime_r(const time_t *timep, struct tm *result);

或者:

struct tm *localtime(const time_t *timep);

struct tm *localtime_r(const time_t *timep, struct tm *result);

最后利用strftime函数进行格式化,得到最后的时间字符串。
至于毫秒、微秒、纳秒另外用进行输出。
例子:

#include <time.h>

#include <sys/time.h>

#include <stdio.h>

int main()

{

struct timeval tv;

char strTime[32];

gettimeofday(&tv, NULL);

struct tm *ptm = gmtime(&tv.tv_sec); //将秒转换成struct tm的形式

strftime(strTime, 32, "%F %T", ptm);

printf("%s ", strTime); //输出精确到秒

printf("%ld Micorseconds\n", (long)tv.tv_usec); //输出微秒

return 0;

}

输出结果: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'.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: