LinuxC时间函数简介

来源:互联网 发布:coc男王升级数据2017 编辑:程序博客网 时间:2024/06/14 04:19

LinuxC时间函数简介

一、综述

许久没有更新过博客,近来项目中有用到Linux下的时间相关的函数,其中也不乏一些使用误区。不禁想做个简单总结,以备后用。

linux下常用的时间函数如下:

                      系统调用                          |        库函数

读取时间:ftime,gettimeofday          |   time
格式转换:                                             |   ctime/ctime_r,localtime/localtime_r, asctime/asctime_r,strftime/strptime,gmtime/gmtime_r,mktime
设置时间:settimeofday                       |   stime

二、常用到的结构体

[cpp] view plain copy
print?
  1. struct timeval   
  2. {  
  3.     time_t       tv_sec;  /* seconds */  
  4.     suseconds_t  tv_usec; /* microseconds */  
  5. };  
  6.   
  7. struct timezone   
  8. {   
  9.     int tv_minuteswest;   /* 格林威治时间往西方的时差 */  
  10.     int tv_dsttime;       /* 时间的修正方式 */  
  11. };  
  12.   
  13. struct timeb    
  14. {    
  15.     time_t           time;        /* 为1970-01-01至今的秒数*/    
  16.     unsigned short   millitm;     /* 千分之一秒即毫秒 */    
  17.     short            timezonel;   /* 为目前时区和Greenwich相差的时间,单位为分钟 */    
  18.     short            dstflag;     /* 为日光节约时间的修正状态,如果为非0代表启用日光节约时间修正 */    
  19. };  
  20.   
  21. struct timespec   
  22. {   
  23.     long int tv_sec;      /* seconds */  
  24.     long int tv_nsec;     /* nano second(10E-9 second) */  
  25. };   
  26.   
  27. struct tm   
  28. {   
  29.     int tm_sec;   /* 代表目前秒数,正常范围为0-59,但允许至61秒 */  
  30.     int tm_min;   /* 代表目前分数,范围0-59 */  
  31.     int tm_hour;  /* 从午夜算起的时数,范围为0-23 */  
  32.     int tm_mday;  /* 目前月份的日数,范围01-31 */  
  33.     int tm_mon;   /* 代表目前月份,从一月算起,范围从0-11 */  
  34.     int tm_year;  /* 从1900 年算起至今的年数 */  
  35.     int tm_wday;  /* 一星期的日数,从星期一算起,范围为0-6 */  
  36.     int tm_yday;  /* 从今年1月1日算起至今的天数,范围为0-365 */  
  37.     int tm_isdst; /* 日光节约时间的旗标 */  
  38. };   


三、读取时间函数

1、time()  取得目前的时间
表头文件:

#include <time.h>
定义函数:time_t time(time_t *t);
函数说明:此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值:成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
范例:

[cpp] view plain copy
print?
  1. #include <time.h>   
  2. int main(void)   
  3. {   
  4.     int seconds = time((time_t*)NULL);   
  5.     printf(“%d\n”,seconds);   
  6.       
  7.     return 0;  
  8. }  

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

★★★特别注意:获取时区tz信息依赖于系统,有的系统无法获取,所以一般调用时该函数第二个参数为NULL。
timeval结构定义为: 

[cpp] view plain copy
print?
  1. struct timeval  
  2. {   
  3.     long tv_sec; /*秒*/   
  4.     long tv_usec; /*微秒*/   
  5. };   
  6. timezone 结构定义为:   
  7. struct timezone  
  8. {   
  9.     int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/   
  10.     int tz_dsttime; /*日光节约时间的状态*/   
  11. };   
上述两个结构都定义在/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所指的内存空间超出存取权限。
范例:

[cpp] view plain copy
print?
  1. #include <sys/time.h>   
  2. #include <unistd.h>   
  3. int main(void)  
  4. {   
  5.     struct timeval tv;   
  6.     struct timezone tz;   
  7.     gettimeofday(&tv , &tz);   
  8.     printf(“tv_sec; %d\n”, tv,.tv_sec) ;   
  9.     printf(“tv_usec; %d\n”,tv.tv_usec);   
  10.     printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);   
  11.     printf(“tz_dsttime, %d\n”,tz.tz_dsttime);   
  12.       
  13.     return 0;  
  14. }  

3、ftime()   函数取得目前的时间和日期
相关函数:time, ctime, gettimeofday

表头文件:

#include <sys/timeb.h>

函数定义:int ftime(struct timeb *tp);
函数说明:ftime()将目前日期由tp所指的结构返回。
返回值 :无论成功或失败都返回0
范例:

[cpp] view plain copy
print?
  1. #include <sys/timeb.h>  
  2. int main(void)  
  3. {  
  4.     struct timeb  tp;  
  5.     ftime(&tp);  
  6.     printf("%d\n", tp.time);  
  7.       
  8.     return 0;  
  9. }  

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

★★★特别注意:ctime函数不是线程安全的。多线程应用里面,应该用ctime_r函数替代ctime函数,因为ctime_r是线程安全的。
范例:

[cpp] view plain copy
print?
  1. #include <time.h>   
  2. int main(void)   
  3. {   
  4.     time_t timep;   
  5.     time(&timep);   
  6.     printf(“%s\n”,ctime(&timep));   
  7.       
  8.     return 0;  
  9. }  

2、localtime_r() localtime()取得当地目前时间和日期
表头文件:
#include <time.h>
定义函数:struct tm *localtime(const time_t * timep);  struct tm* localtime_r( const time_t* timer, struct tm* result );
函数说明:localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回值:返回结构tm代表目前的当地时间。
★★★特别注意:localtime函数可以将时间转换本地时间,但是localtime函数不是线程安全的。多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的。
范例:

[cpp] view plain copy
print?
  1. #include <time.h>   
  2. int main(void)  
  3. {   
  4.     char *wday[] = {“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};   
  5.     time_t timep;   
  6.     struct tm *p;   
  7.     time(&timep);   
  8.     p = localtime(&timep); /*取得当地时间*/   
  9.     printf(“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);   
  10.     printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);   
  11.       
  12.     return 0;  
  13. }  

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

附加说明:返回一字符串表示目前当地的时间日期。

★★★特别注意:asctime函数不是线程安全的。多线程应用里面,应该用asctime_r函数替代asctime函数,因为asctime_r是线程安全的。

范例:

[cpp] view plain copy
print?
  1. #include <time.h>   
  2. int main(void)   
  3. {   
  4.     time_t timep;   
  5.     time(&timep);   
  6.     printf(“%s\n”,asctime(gmtime(&timep)));   
  7.       
  8.     return 0;  
  9. }  

4、strftime(),strptime()  这两个函数都是时间日期的格式控制函数,在功能上看起来正好相反。strftime将一个tm结构格式化为一个字符串,strptime则是将一个字符串格式化为一个tm结构。
表头文件:
#include <time.h>
定义函数: size_t strftime(char *s,size_t maxsize,char *format,const struct tm *timeptr); char *strptime(const char *buf,const char *format,struct tm *timeptr);
函数说明:strftime函数对timeptr指向的tm结构所代表的时间和日期进行格式编排,其结果放在字符串s中。该字符串的长度被设置为(最少)maxsize个字符。格式字符串format用来对写入字符串的字符进行控制,它包含着将被传送到字符串里去的普通字符以及编排时间和日期格式的转换控制符。strptime函数将一个字符串格式化为一个tm结构,功能与strptime相反。。
附加说明:
转换控制符 说明
%a 星期几的简写形式
%A 星期几的全称
%b 月份的简写形式
%B 月份的全称
%c 日期和时间
%d 月份中的日期,0-31
%H 小时,00-23
%I 12进制小时钟点,01-12
%j 年份中的日期,001-366
%m 年份中的月份,01-12
%M 分,00-59
%p 上午或下午
%S 秒,00-60
%u 星期几,1-7
%w 星期几,0-6
%x 当地格式的日期
%X 当地格式的时间
%y 年份中的最后两位数,00-99
%Y
%Z 地理时区名称
返回值:strftime()返回向strDest指向的字符串中放置的字符数;strptime()返回一个指针,指向转换过程处理的最后一个字符后面的那个字符。
范例:

[cpp] view plain copy
print?
  1. #include <time.h>   
  2. #include <stdio.h>   
  3.   
  4. int main()    
  5. {    
  6.     time_t lt      = time(NULL);    
  7.     struct tm* ptr = localtime(&lt);    
  8.     char szBuffer[64] = {0};    
  9.     const char* pFormat = "The time now is %Y-%m-%d %H:%M:%S";    
  10.     strftime(szBuffer, 64, pFormat, ptr);    
  11.     printf("%s\n", szBuffer);    
  12.     
  13.     struct tm tmTemp;    
  14.     char tmBuffer[64] = {0};    
  15.     strptime(szBuffer, pFormat, &tmTemp);    
  16.     strftime(tmBuffer, 64, "The time just was %Y-%m-%d %H:%M:%S", &tmTemp);    
  17.     printf("%s\n", tmBuffer);   
  18.   
  19.     return 0;     
  20. }  

5、gmtime()  gmtime_r() 取得目前时间和日期
表头文件:
#include <time.h>
定义函数:struct tm*gmtime(const time_t*timep); struct tm *gmtime_r(const time_t *timep, struct tm *result);
函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。 
★★★特别注意:(1)gmtime函数不是线程安全的。多线程应用里面,应该用gmtime_r函数替代gmtime函数,因为gmtime_r是线程安全的;(2)此函数返回的时间日期未经时区转换,而是UTC时间。
返回值:返回结构tm代表目前UTC 时间
范例:

[cpp] view plain copy
print?
  1. #include <time.h>   
  2. int main(void)  
  3. {   
  4.     char *wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};   
  5.     time_t timep;   
  6.     struct tm *p;   
  7.     time(&timep);   
  8.     p = gmtime(&timep);   
  9.     printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);   
  10.     printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);   
  11.       
  12.     return 0;  
  13. }  

6、mktime()   将时间结构数据转换成经过的秒数
表头文件:
#include <time.h>
定义函数:time_t mktime(strcut tm * timeptr);
函数说明:mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值:返回经过的秒数。
范例:

[cpp] view plain copy
print?
  1. /* 用time()取得时间(秒数),利用localtime()  
  2. 转换成struct tm 再利用mktime()将struct tm转换成原来的秒数*/   
  3. #include <time.h>   
  4. int main(void)   
  5. {   
  6.     time_t timep;   
  7.     strcut tm *p;   
  8.     time(&timep);   
  9.     printf(“time() : %d \n”,timep);   
  10.     p = localtime(&timep);   
  11.     timep = mktime(p);   
  12.     printf(“time()->localtime()->mktime():%d\n”,timep);   
  13.       
  14.     return 0;  
  15. }  

五、设置时间函数
1、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 时区或某个数据是不正确的,无法正确设置时间。
范例:

[cpp] view plain copy
print?
  1. #include <sys/time.h>   
  2. #include <unistd.h>  
  3.   
  4. int main(void)  
  5. {  
  6.     struct tm * stime;  
  7.     struct timeval tv;  
  8.     time_t timep,timeq;  
  9.     int rec;  
  10.   
  11.     time(&timep);  
  12.     stime = localtime(&timep);  
  13.   
  14.     timeq = mktime(stime);  
  15.     tv.tv_sec = (long)timeq;  
  16.     printf("the second: %ld\n",tv.tv_sec);  
  17.     tv.tv_usec = 0;  
  18.   
  19.     rec = settimeofday(&tv,NULL);  
  20.     if(rec <0 )  
  21.     {  
  22.         printf("settimeofday failed!\n");  
  23.     }  
  24.     else  
  25.     {  
  26.         printf("Set system time ok!\n");  
  27.     }  
  28.       
  29.     return 0;  
  30. }  

2、stime()  设置目前时间,只能精确到秒
表头文件:
#include <time.h>
定义函数:int stime(time_t *t);
函数说明:参数t是以秒为单位的时间值,从GMT1970年1月1日0时0分0秒开始计算。
返回值:成功返回0,错误返回-1,errno错误码,EFAULT表示传递的参数错误,如时间值是无效的值,EPERM表示权限不够,注意只有root用户才有修改系统时间的权限。如果要让普通程序修改系统时间,可以先切换到root用户操作,修改完成后,再切换到普通用户,或者用命令chmod+s给执行文件加上root用户的权限。
范例:

[cpp] view plain copy
print?
  1. #include <stdio.h>  
  2. #include <time.h>  
  3.   
  4. int main(void)  
  5. {  
  6.     time_t t;  
  7.     t = time(NULL);  
  8.     t = t + 120;      
  9.       
  10.     /* 将当前时间调快两分钟*/  
  11.     if (0 == stime(&t))  
  12.     {  
  13.         printf("set system time success!\n");  
  14.     }  
  15.     else  
  16.     {  
  17.         printf("stime failed!\n");  
  18.     }  
  19.       
  20.     return 0;  
  21. }  
六、结语

转载:http://blog.csdn.net/tsx86/article/details/49965171

[c-sharp] view plaincopyprint?
  1. int getSystemTime()   
  2. {   
  3. time_t timer;   
  4. struct tm* t_tm;   
  5. time(&timer);   
  6. t_tm = localtime(&timer);   
  7. printf("today is %4d%02d%02d%02d%02d%02d/n", t_tm.tm_year+1900,   
  8. t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);   
  9. return 0;   
  10. }   
  11.   
  12. /************************************************ 
  13. 设置操作系统时间 
  14. 参数:*dt数据格式为"2006-4-20 20:30:30" 
  15. 调用方法: 
  16.     char *pt="2006-4-20 20:30:30"; 
  17.     SetSystemTime(pt); 
  18. **************************************************/  
  19. int SetSystemTime(char *dt)  
  20. {  
  21.     struct rtc_time tm;  
  22.     struct tm _tm;  
  23.     struct timeval tv;  
  24.     time_t timep;  
  25.     sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year,  
  26.         &tm.tm_mon, &tm.tm_mday,&tm.tm_hour,  
  27.         &tm.tm_min, &tm.tm_sec);  
  28.     _tm.tm_sec = tm.tm_sec;  
  29.     _tm.tm_min = tm.tm_min;  
  30.     _tm.tm_hour = tm.tm_hour;  
  31.     _tm.tm_mday = tm.tm_mday;  
  32.     _tm.tm_mon = tm.tm_mon - 1;  
  33.     _tm.tm_year = tm.tm_year - 1900;  
  34.   
  35.     timep = mktime(&_tm);  
  36.     tv.tv_sec = timep;  
  37.     tv.tv_usec = 0;  
  38.     if(settimeofday (&tv, (struct timezone *) 0) < 0)  
  39.     {  
  40.     printf("Set system datatime error!/n");  
  41.  return -1;  
  42.  
  43.  return 0;  


Linux之通过NTP协议实现服务器时间同步的源码:http://blog.csdn.net/sauphy/article/details/50021833 
意思代码验证有报错,将:struct tim tv,tv1; 改为struct timeval tv,tv1;

 NTP校时源码参考

:

http://blog.csdn.net/rich_baba/article/details/605286
原创粉丝点击