UNIX环境高级编程——时间和日期

来源:互联网 发布:京东秒杀软件安卓版 编辑:程序博客网 时间:2024/05/01 16:47
由UNIX内核提供的基本时间服务是计算自国际标准时间公元1970年1月1日00:00:00以来经过的秒数。这种秒数是以数据类型time_t表示。   

1.     time函数返回当前时间和日期:

time_t time(time_t *calptr);
时间值总是作为函数返回。如果参数不为空,则时间值也存放在由calptr指向的单元内。

2.     与time函数相比,gettimeofday提供了更高的分辨率(最高位微秒级)。

int gettimeofday(struct timeval* tp,void* tzp);

tzp的唯一合法值是NULL,其他值则将产生不确定的结果。

gettimeofday函数将当前时间存放在tp指向的timeval结构中,而该结构存储秒和微妙。
struct timeval{
    long int tv_sec; // 秒数
    long int tv_usec; // 微秒数
}
 
 
3.     两个函数localtime和gmtime将日历时间转换成以年、月、日、时、分、秒、周日表示的时间,并将这些存放在一个tm结构中。

struct tm
{
int tm_sec;//代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min;//代表目前分数,范围0-59
int tm_hour;//从午夜算起的时数,范围为0-23
int tm_mday;//目前月份的日数,范围01-31
int tm_mon;//代表目前月份,从一月算起,范围从0-11
int tm_year;//从1900 年算起至今的年数
int tm_wday;//一星期的日数,从星期一算起,范围为0-6
int tm_yday;//从今年1月1日算起至今的天数,范围为0-365
int tm_isdst;//夏令时当前是否生效
};

struct tm *localtime(const time_t*clock);
struct tm *gmtime(const time_t *clock);

localtime和gmtime之间的区别是:localtime将日历时间转换成本地时间(考虑到本地时区和夏时制标志),而gmtime则将日历时 间转换成国际标准时间的年、月、日、时、分、秒、周日。
 
 
4.     函数mktime以本地时间的年、月、日等作为参数,将其转换成time_t值。
time_t mktime(struct tm* tmptr);
     

5.     asctime和ctime函数产生大家熟悉的26字节的字符串,这与date(1)命令的系统默认输出类似,例如:

huangcheng@ubuntu:~$ date
20130705日 星期五 18:25:52 CST


char* asctime(conststruct tm* tmptr);
char* ctime(const time_t calptr);

sctime的参数是指向年、月、日等字符串的指针,而ctime的参数则是指向日历时间的指针。


 

Linux时间函数


简介 本文旨在为了解Linux各种时间类型与时间函数提供技术文档。


1、Linux下常用时间类型

Linux下常用时间类型有四种:time_t、struct tm、struct timeval、struct timespec

 
1.1 time_t时间类型

time_t类型在time.h中定义:

#ifndef __TIME_T  

#define __TIME_T  

typedef   long   time_t;  

#endif  

可见,time_t实际是一个长整型。其值表示为从UTC(coordinated universal time)时间1970年1月1日00时00分00秒(也称为Linux系统的Epoch时间)到当前时刻的秒数。

由于time_t类型长度的限制,它所表示的时间不能晚于2038119031407秒(UTC)。为了能够表示更久远的时间,可用64位或更长的整形数来保存日历时间,这里不作详述。

使用time()函数获取当前时间的time_t值,使用ctime()函数将time_t转为当地时间字符串。

备注:UTC时间有时也称为GMT时间,其实UTC和GMT两者几乎是同一概念。它们都是指格林尼治标准时间,只不过UTC的称呼更为正式一点。两者区别在于前者是天文上的概念,而后者是基于一个原子钟。

 
1.2 struct tm时间类型

tm结构在time.h中定义:

#ifndef _TM_DEFINED  

struct tm{  

    int tm_sec; /*秒 - 取值区间为[0, 59]*/  

    int tm_min; /*分 - 取值区间为[0, 59]*/  

    int tm_hour; /*时 - 取值区间为[0, 23]*/  

    int tm_mday; /*日 - 取值区间为[1, 31]*/  

    int tm_mon; /*月份 - 取值区间为[0, 11]*/  

    int tm_year; /*年份 - 其值为1900年至今年数*/  

    int tm_wday; /*星期 - 取值区间[0, 6],0代表星期天,1代表星期1,以此类推*/  

    int tm_yday; /*从每年的1月1日开始的天数-取值区间为[0, 365],0代表1月1日*/  

    int tm_isdst; /*夏令时标识符,使用夏令时,tm_isdst为正,不使用夏令时,tm_isdst为0,不了解情况时,tm_isdst为负*/  

};  

#define _TM_DEFINED  

#endif  

ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。

使用gmtime( )和localtime( )可将time_t时间类型转换为tm结构体;

使用mktime( )将tm结构体转换为time_t时间类型;

使用asctime( )将struct tm转换为字符串形式。



1.3 struct timeval时间类型

timeval结构体在time.h中定义:

Struct tmieval{  

    time_t tv_sec; /*秒s*/  

    suseconds_t tv_usec; /*微秒us*/  

};  

设置时间函数settimeofday( )与获取时间函数gettimeofday( )均使用该事件类型作为传参。



1.4 struct timespec时间类型

timespec结构体在time.h定义:

struct timespec{  

    time_t tv_sec; /*秒s*/  

    long tv_nsec; /*纳秒ns*/  

};   

2、Linux下常用时间函数



参考资料:

http://blog.csdn.net/c395565746c/article/details/6621153

http://fanqiang.chinaunix.net/a4/b8/20010527/201001267.html

http://qgjie456.blog.163.com/blog/static/35451367200844102949365/

http://hi.baidu.com/wangzhongli/blog/item/e260b3a1f388278746106461.html

http://www.eefocus.com/xuefu2009/blog/10-03/187348_f456a.html