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

Linux与时间相关的结构体及相关用法

2012-09-11 15:20 387 查看
1. Linux下与时间有关的结构体

struct timeval

{

int tv_sec;

int tv_usec;

};

其中tv_sec是由凌晨开始算起的秒数,tv_usec则是微秒(10E-6 second)。

struct timezone

{

int tv_minuteswest;

int tv_dsttime;

};

tv_minuteswest是格林威治时间往西方的时差,tv_dsttime则是时间的修正方式。

struct timespec

{

long int tv_sec;

long int tv_nsec;

};

tv_nsec是nano second(10E-9 second)。

struct tm

{

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

};

tm_sec表「秒」数,在[0,61]之间,多出来的两秒是用来处理跳秒问题用的。

tm_min表「分」数,在[0,59]之间。

tm_hour表「时」数,在[0,23]之间。

tm_mday表「本月第几日」,在[1,31]之间。

tm_mon表「本年第几月」,在[0,11]之间。

tm_year要加1900表示那一年。

tm_wday表「本第几日」,在[0,6]之间。

tm_yday表「本年第几日」,在[0,365]之间,闰年有366日。

tm_isdst表是否为「日光节约时间」。

struct itimerval

{

struct timeval it_interval;

struct timeval it_value;

};

it_interval成员表示间隔计数器的初始值,而it_value成员表示间隔计数器的当前值。

2.获得当前时间

在所有的UNIX下,都有个time()的函数

time_t time(time_t *t);

这个函数会传回从epoch开始计算起的秒数,如果t是non-null,它将会把时间值填入t中。

对某些需要较高精准度的需求,Linux提供了gettimeofday()。

int gettimeofday(struct timeval * tv,struct timezone *tz);

int settimeofday(const struct timeval * tv,const struct timezone *tz);

struct tm格式时间函数

struct tm * gmtime(const time_t * t);

转换成格林威治时间。有时称为GMT或UTC。

struct tm * localtime(const time_t *t);

转换成本地时间。它可以透过修改TZ环境变数来在一台机器中,不同使用者表示不同时间。

time_t mktime(struct tm *tp);

转换tm成为time_t格式,使用本地时间。

tme_t timegm(strut tm *tp);

转换tm成为time_t格式,使用UTC时间。

double difftime(time_t t2,time_t t1);

计算秒差。

3.文字时间格式函数

char * asctime(struct tm *tp);

char * ctime(struct tm *tp);

这两个函数都转换时间格式为标准UNIX时间格式。

Mon May 3 08:23:35 1999

ctime一率使用当地时间,asctime则用tm结构内的timezone资讯来表示。

size_t strftime(char *str,size_t max,char *fmt,struct tm *tp);

strftime有点像sprintf,其格式由fmt来指定。

%a : 本第几天名称,缩写。

%A : 本第几天名称,全称。

%b : 月份名称,缩写。

%B : 月份名称,全称。

%c : 与ctime/asctime格式相同。

%d : 本月第几日名称,由零算起。

%H : 当天第几个小时,24小时制,由零算起。

%I : 当天第几个小时,12小时制,由零算起。

%j : 当年第几天,由零算起。

%m : 当年第几月,由零算起。

%M : 该小时的第几分,由零算起。

%p : AM或PM。

%S : 该分钟的第几秒,由零算起。

%U : 当年第几,由第一个日开始计算。

%W : 当年第几,由第一个一开始计算。

%w : 当第几日,由零算起。

%x : 当地日期。

%X : 当地时间。

%y : 两位数的年份。

%Y : 四位数的年份。

%Z : 时区名称的缩写。

%% : %符号。

char * strptime(char *s,char *fmt,struct tm *tp);

如同scanf一样,解译字串成为tm格式。

%h : 与%b及%B同。

%c : 读取%x及%X格式。

%C : 读取%C格式。

%e : 与%d同。

%D : 读取%m/%d/%y格式。

%k : 与%H同。

%l : 与%I同。

%r : 读取"%I:%M:%S %p"格式。

%R : 读取"%H:%M"格式。

%T : 读取"%H:%M:%S"格式。

%y : 读取两位数年份。

%Y : 读取四位数年份。

下面举一个小例子,说明如何获得系统当前时间:

time_t now;

struct tm *timenow;

char strtemp[255];



time(&now);

timenow = localtime(&now);

printf("recent time is : %s \n", asctime(timenow));

asctime(将时间和日期以字符串格式表示)
相关函数
time,ctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
char * asctime(const struct tm * timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993\n”
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
范例
#include <time.h>

main()

{

time_t timep;

time (&timep);

printf(“%s”,asctime(gmtime(&timep)));

}
执行
Sat Oct 28 02:10:06 2000
ctime(将时间和日期以字符串格式表示)
相关函数
time,asctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
char *ctime(const time_t *timep);
函数说明
ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值
返回一字符串表示目前当地的时间日期。
范例
#include<time.h>

main()

{

time_t timep;

time (&timep);

printf(“%s”,ctime(&timep));

}
执行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的时间)
相关函数
time,ctime,ftime,settimeofday
表头文件
#include <sys/time.h>

#include <unistd.h>
定义函数
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。

timeval结构定义为:

struct timeval{

long tv_sec;

long tv_usec;

};

timezone 结构定义为:

struct timezone{

int tz_minuteswest;

int tz_dsttime;

};

上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下

DST_NONE

DST_USA

DST_AUST

DST_WET

DST_MET

DST_EET

DST_CAN

DST_GB

DST_RUM

DST_TUR

DST_AUSTALT
返回值
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例
#include<sys/time.h>

#include<unistd.h>

main(){

struct timeval tv;

struct timezone tz;

gettimeofday (&tv , &tz);

printf(“tv_sec; %d\n”, tv,.tv_sec) ;

printf(“tv_usec; %d\n”,tv.tv_usec);

printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);

printf(“tz_dsttime, %d\n”,tz.tz_dsttime);

}
执行
tv_sec: 974857339

tv_usec:136996

tz_minuteswest:-540

tz_dsttime:0
gmtime(取得目前时间和日期)
相关函数
time,asctime,ctime,localtime
表头文件
#include<time.h>
定义函数
struct tm*gmtime(const time_t*timep);
函数说明
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。

结构tm的定义为

struct tm

{

int tm_sec;

int tm_min;

int tm_hour;

int tm_mday;

int tm_mon;

int tm_year;

int tm_wday;

int tm_yday;

int tm_isdst;

};

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 日光节约时间的旗标

此函数返回的时间日期未经时区转换,而是UTC时间。
返回值
返回结构tm代表目前UTC 时间
范例
#include <time.h>

main(){

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

time_t timep;

struct tm *p;

time(&timep);

p=gmtime(&timep);

printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);

printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);

}
执行
2000/10/28 Sat 8:15:38
localtime(取得当地目前时间和日期)
相关函数
time, asctime, ctime, gmtime
表头文件
#include<time.h>
定义函数
struct tm *localtime(const time_t * timep);
函数说明
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回值
返回结构tm代表目前的当地时间。
范例
#include<time.h>

main(){

char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};

time_t timep;

struct tm *p;

time(&timep);

p=localtime(&timep);

printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);

printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);

}
执行
2000/10/28 Sat 11:12:22
mktime(将时间结构数据转换成经过的秒数)
相关函数
time,asctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
time_t mktime(strcut tm * timeptr);
函数说明
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值
返回经过的秒数。
范例
#include<time.h>

main()

{

time_t timep;

strcut tm *p;

time(&timep);

printf(“time() : %d \n”,timep);

p=localtime(&timep);

timep = mktime(p);

printf(“time()->localtime()->mktime():%d\n”,timep);

}
执行
time():974943297

time()->localtime()->mktime():974943297
settimeofday(设置目前时间)
相关函数
time,ctime,ftime,gettimeofday
表头文件
#include<sys/time.h>

#include<unistd.h>
定义函数
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函数说明
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值
成功则返回0,失败返回-1,错误代码存于errno。
错误代码
EPERM 并非由root权限调用settimeofday(),权限不够。

EINVAL 时区或某个数据是不正确的,无法正确设置时间。
time(取得目前的时间)
相关函数
ctime,ftime,gettimeofday
表头文件
#include<time.h>
定义函数
time_t time(time_t *t);
函数说明
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
范例
#include<time.h>

mian()

{

int seconds= time((time_t*)NULL);

printf(“%d\n”,seconds);

}
#include <stdio.h>

#include <time.h>

int

main ()

{

struct tm tm, tm2;

char buf[255];

time_t t1, t2;

strptime ("Thu Feb 5 13:12:18 EST 2009", "%a %b %d %H:%M:%S EST %Y", &tm);

strptime ("Fri Feb 6 13:11:18 EST 2009", "%a %b %d %H:%M:%S EST %Y", &tm2);

t1 = mktime(&tm);

t2 = mktime(&tm2);

printf("TIME: %ld, %ld, %ld\n", t2, t1, t2-t1);

return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: