Linux时间函数

http://blog.csdn.net/water_cow/article/details/7521567



系统环境:ubuntu10.04


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

1、Linux下常用时间类型
Linux下常用时间类型有四种:time_tstruct tmstruct timevalstruct timespec

1.1 time_t时间类型
time_t类型在time.h中定义:
  1. #ifndef __TIME_T  
  2. #define __TIME_T  
  3. typedef  long  time_t;  
  4. #endif  
可见,time_t实际是一个长整型。其值表示为从UTC(coordinated universal time)时间197011000000(也称为Linux系统的Epoch时间)到当前时刻的秒数。由于time_t类型长度的限制,它所表示的时间不能晚于2038119031407秒(UTC)。为了能够表示更久远的时间,可用64或更长的整形数来保存日历时间,这里不作详述。
使用time()函数获取当前时间的time_t值,使用ctime()函数将time_t转为当地时间字符串。

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

1.2 struct tm时间类型
tm结构在time.h中定义:
  1. #ifndef _TM_DEFINED  
  2. struct tm{  
  3.     int tm_sec; /*秒 - 取值区间为[0, 59]*/  
  4.     int tm_min; /*分 - 取值区间为[0, 59]*/  
  5.     int tm_hour; /*时 - 取值区间为[0, 23]*/  
  6.     int tm_mday; /*日 - 取值区间为[1, 31]*/  
  7.     int tm_mon; /*月份 - 取值区间为[0, 11]*/  
  8.     int tm_year; /*年份 - 其值为1900年至今年数*/  
  9.     int tm_wday; /*星期 - 取值区间[0, 6],0代表星期天,1代表星期1,以此类推*/  
  10.     int tm_yday; /*从每年的1月1日开始的天数-取值区间为[0, 365],0代表1月1日*/  
  11.     int tm_isdst; /*夏令时标识符,使用夏令时,tm_isdst为正,不使用夏令时,tm_isdst为0,不了解情况时,tm_isdst为负*/  
  12. };  
  13. #define _TM_DEFINED  
  14. #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中定义:
  1. Struct tmieval{  
  2.     time_t tv_sec; /*秒s*/  
  3.     suseconds_t tv_usec; /*微秒us*/  
  4. };  
设置时间函数settimeofday( )与获取时间函数gettimeofday( )均使用该事件类型作为传参。
 
1.4 struct timespec时间类型
timespec结构体在time.h定义:
  1. struct timespec{  
  2.     time_t tv_sec; /*秒s*/  
  3.     long tv_nsec; /*纳秒ns*/  
  4. };  
 
2、Linux下常用时间函数
Linux下常用时间函数有:time( )ctime( )gmtime( )localtime( )mktime( )asctime( )difftime( )gettimeofday( )settimeofday( )

2.1 time( )函数
头文件:#include <time.h>
函数定义:time_t time(time_t *timer)
功能描述:该函数返回从197011000000秒至今所经过的秒数。如果time_t *timer非空指针,函数也会将返回值存到timer指针指向的内存。
返回值:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
例:
  1. time_t seconds;  
  2. seconds = time((time_t *)NULL);  

2.2 ctime( )函数
头文件:#include <time.h>
函数定义:char *ctime(const time_t *timep);
功能描述:ctime( )将参数timep指向的time_t时间信息转换成实际所使用的时间日期表示方法,并以字符串形式返回。字符串格式为:"Wed Jun 20 21:00:00 2012\n"
例:
  1. time_t timep;  
  2. tmep = time(NULL);  
  3. printf("%s\n", ctime(&timep));  

2.3 gmtime( )函数
头文件:#include <time.h>
函数定义:struct tm *gmtime(const time_t *timep)
功能描述:gmtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的GMT时间信息,并以struct tm*指针返回。
GMTGMT是中央时区, 北京 在东8,相差8个小时,所以北京时间=GMT时间+8小时
例:
  1. int main(void)  
  2. {  
  3.     char *wday[] = {"Sun""Mon""Tue""Wed""Thu""Fri""Sat"};  
  4.     time_t timep;  
  5.     struct tm *p_tm;  
  6.     timep = time(NULL);  
  7.     p_tm = gmtime(&timep); /*获取GMT时间*/  
  8.     printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday);  
  9.     printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);  
  10. }  

2.4 localtime( )函数
头文件:#include <time.h>
函数定义:struct tm *localtime(const time_t *timep);
功能描述:localtime( )将参数timep指向的time_t时间信息转换成以tm结构体表示的本地时区时间(如北京时间= GMT+小时)

/*
Name: localtime
Prototype: struct tm * localtime (const time_t *time)
Description:
The localtime function converts the simple time pointed to by time to broken-down time 
 representation, expressed relative to the user's specified time zone. 

The return value is a pointer to a static broken-down time structure, which might be overwritten by 
 subsequent calls to ctime, gmtime, or localtime. (But no other library function overwrites the contents 
 of this object.) 

The return value is the null pointer if time cannot be represented as a broken-down time; typically 
 this is because the year cannot fit into an int. 

Calling localtime has one other effect: it sets the variable tzname with information about the current 
 time zone. . 
Header files:
time.h*/
例:
  1. int main(void)  
  2. {  
  3.     char *wday[] = {"Sun""Mon""Tue""Wed""Thu""Fri""Sat"};  
  4.     time_t timep;  
  5.     struct tm *p_tm;  
  6.     timep = time(NULL);  
  7.     p_tm = localtime(&timep); /*获取本地时区时间*/  
  8.     printf("%d-%d-%d ", (p_tm->tm_year+1900), (p_tm->mon+1), p_tm->tm_mday);  
  9.     printf("%s %d:%d:%d\n", wday[p_tm->tm_wday], p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec);  
  10.     return 0;  


2.5 mktime( )函数
头文件:#include <time.h>
函数定义:time_t mktime(struct tm *p_tm);
功能描述:mktime( )将参数p_tm指向的tm结构体数据转换成从197011000000秒至今的GMT时间经过的秒数。
例:
  1. int main(void)  
  2. {  
  3.     time_t timep:  
  4.     struct tm *p_tm;  
  5.     timep = time(NULL);  
  6.     pintf("time( ):%d\n", timep);  
  7.     p_tm = local(&timep);  
  8.     timep = mktime(p_tm);  
  9.     printf("time( )->localtime( )->mktime( ):%d\n", timep);  
  10.     return 0;  
  11. }  

2.6 asctime( )函数
头文件:#include <time.h>
函数定义:char *asctime(const struct tm *p_tm);
功能描述:asctime( )将参数p_tm指向的tm结构体数据转换成实际使用的时间日期表示方法,并以字符串形式返回(ctime函数相同)。字符串格式为:"Wed Jun 20 21:00:00 2012\n"
例:
  1. int main(void)  
  2. {  
  3.     time_t timep;  
  4.     timep = time(NULL);  
  5.     printf("%s\n", asctime(gmtime(&timep)));  
  6.     return 0;  
  7. }  

2.7 difftime( )函数
头文件:#include <time.h>
函数定义:double difftime(time_t timep1, time_t timep2);
功能描述:difftime( )比较参数timep1timep2时间是否相同,并返回之间相差秒数。
例:
  1. int main(void)  
  2. {  
  3.     time_t timep1, timep2;  
  4.     timep1 = time(NULL);  
  5.     sleep(2);  
  6.     timep2 = time(NULL);  
  7.     printf("the difference is %f seconds\n", difftime(timep1, timep2));  
  8.     return 0;  
  9. }  

2.8 gettimeofday( )函数
头文件:#include <sys/time.h>
        #include <unistd.h>
函数定义:int gettimeofday(struct timeval *tv, struct timezone *tz);
功能描述:gettimeofday( )把目前的时间信息存入tv指向的结构体,当地时区信息则放到tz指向的结构体。
struct timezone原型:
  1. struct timezone{  
  2.     int tz_minuteswest; /*miniutes west of Greenwich*/  
  3.     int tz_dsttime; /*type of DST correction*/  
  4. };  
例:
  1. struct timeval tv;  
  2. struct timeval tz;  
  3. gettimeofday(&tv, &tz);  

附:
使用time函数族获取时间并输出指定格式字符串例子(strftime( )函数):
  1. int main(void)  
  2. {  
  3.     char strtime[20] = {0};  
  4.     time_t timep;  
  5.     struct tm *p_tm;  
  6.     timep = time(NULL);  
  7.     p_tm = localtime(&timep);  
  8.     strftime(strtime, sizeof(strtime), "%Y-%m-%d %H:%M:%S", p_tm);  
  9.     return 0;  
  10. }  

2.9 settimeofday( )函数
头文件:#include <sys/time.h>
        #include <unistd.h>
函数定义:int settimeofday(const struct timeval *tv, const struct timezone *gz);
功能描述:settimeofday( )把当前时间设成由tv指向的结构体数据。当前地区信息则设成tz指向的结构体数据。
例:
  1. int main(void)  
  2. {  
  3.     char t_string[] = "2012-04-28 22:30:00";  
  4.     struct tm time_tm;  
  5.     struct timeval time_tv;  
  6.     time_t timep;  
  7.     int ret = 0;  
  8.   
  9.     sscanf(t_string, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);  
  10.     time_tm.tm_year -= 1900;  
  11.     time_tm.tm_mon -= 1;  
  12.     time_tm.tm_wday = 0;  
  13.     time_tm.tm_yday = 0;  
  14.     time_tm.tm_isdst = 0;  
  15.   
  16.     timep = mktime(&time_tm);  
  17.     time_tv.tv_sec = timep;  
  18.     time_tv.tv_usec = 0;  
  19.   
  20.     ret = settimeofday(&time_tv, NULL);  
  21.     if(ret != 0)  
  22.     {  
  23.         fprintf(stderr, "settimeofday failed\n");  
  24.         return -1;  
  25.     }  
  26.     return 0;  





相关函数 time,ctime,gmtime,localtime

//--------------------------------------------------------------------------------------------------------------------------------------------------------//

asctime(将时间和日期以字符串格式表示)

#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(将时间和日期以字符串格式表示)

表头文件

#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(取得目前的时间)

表头文件

#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; /*和Greenwich 时间差了多少分钟*/
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 /*澳洲(1986年以后)*/

返回值

成功则返回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(取得目前时间和日期)

表头文件

#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(取得当地目前时间和日期)

表头文件

#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(将时间结构数据转换成经过的秒数)

表头文件

#include<time.h>

定义函数

time_t mktime(strcut tm * timeptr);

函数说明

mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。

返回值

返回经过的秒数。

范例

/* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/
#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(设置目前时间)

表头文件

#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(取得目前的时间)

表头文件

#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);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值