C++ tm 和 time_t 有关总结

来源:互联网 发布:福岛核电站 知乎 编辑:程序博客网 时间:2024/05/04 18:12
 

C++ tm 和 time_t 有关总结

标签: tmtime_tdifftimemktimelocaltime
 515人阅读 评论(0) 收藏 举报
 分类:

【分解时间 & 日历时间】

在标准C/C++中,我们可通过tm结构来获得日期和时间,struct tm结构体在time.h中的定义如下

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef_TM_DEFINED    
  2. structtm {    
  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代表一月) - 取值区间为[0,11] */    
  8. int tm_year; /* 年份,其值等于实际年份减去1900 */    
  9. int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */    
  10. int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */    
  11. int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/    
  12. };    
  13. #define_TM_DEFINED    
  14. #endif    

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


而日历时间(Calendar Time)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。在time.h中,我们也可以看到time_t是一个长整型数:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #ifndef _TIME_T_DEFINED  
  2. typedef long time_t;    /* 时间值 */  
  3. #define _TIME_T_DEFINED  
  4. #endif  
        那么问题来了:既然time_t实际上是长整型,到未来的某一天,从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间)超出了长整形所能表示的数的范围怎么办?对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。

        为了能够表示更久远的时间,一些编译器厂商引入了64位甚至更长的整形数来保存日历时间。比如微软在Visual C++中采用了__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间(而不是通过使用32位字的time()函数),这样就可以通过该数据类型保存3001年1月1日0时0分0秒(不包括该时间点)之前的时间。


【常用函数】
在time.h头文件中,我们还可以看到一些函数,它们都是以time_t为参数类型或返回值类型的函数:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. double difftime(time_t time1, time_t time0);  
  2. time_t mktime(struct tm * timeptr);  
  3. time_t time(time_t * timer);  
  4. char * asctime(const struct tm * timeptr);  
  5. char * ctime(const time_t *timer);  


此外,time.h还提供了两种不同的函数,将日历时间转换为我们平时看到的时间格式(年月日时分秒分开显示的) tm:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. struct tm * gmtime(const time_t *timer);  
  2. struct tm * localtime(const time_t * timer);  

其中gmtime()是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间。而localtime()是将日历时间转化为本地时间。比如:用gmtime()函数获得的世界标准时间是2016年7月30日7点18分20秒,那么用localtime()函数在中国地区获得的本地时间会比时间标准时间晚8个小时,即2016年7月30日15点18分20秒。示例代码如下:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "time.h"  
  2. #include "stdio.h"  
  3.   
  4. int main(void)  
  5. {  
  6.     time_t t = time(NULL);//参数为NULL,返回现在的日历时间  
  7.     struct tm *local = localtime(&t);  
  8.     printf("Local hour is: %d\n",local->tm_hour);  
  9.       
  10.     local = gmtime(&t);  
  11.     printf("UTC hour is: %d\n",local->tm_hour);  
  12.   
  13.     return 0;  
  14. }  

运行结果:

Local hour is: 15
UTC hour is: 7


【固定的时间格式】

可以通过asctime()函数和ctime()函数将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。返回的时间格式为:
星期几 月份 日期 时:分:秒 年\n\0
例如:Wed Jan 02 02:03:55 1980\n\0

函数原型如下:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. char * asctime(const struct tm * timeptr);  
  2. char * ctime(const time_t *timer);  
其中asctime()函数,是通过tm结构来生成具有固定格式的保存时间信息的字符串,而ctime()是通过日历时间来生成时间字符串。这样的话,asctime()函数只是把tm结构对象中的各个域填到时间字符串的相应位置就行了,而ctime()函数需要先参照本地的时间设置,把日历时间转化为本地时间,然后再生成格式化后的字符串。示例代码如下:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "time.h"  
  2. #include "stdio.h"  
  3.   
  4. int main(void)  
  5. {  
  6.     time_t tt = time(NULL);  
  7.     struct tm *ptr = gmtime(&tt);  
  8.     printf(asctime(ptr));  
  9.     printf(ctime(&tt));  
  10.   
  11.     return 0;  
  12. }  

运行结果:

Tue Aug 16 07:21:59 2016
Tue Aug 16 15:21:59 2016


自定义时间格式

我们可以使用strftime()函数将时间格式化为我们想要的格式。它的原型如下:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. size_t strftime(  
  2.                 char *strDest,  
  3.                 size_t maxsize,  
  4.                 const char *format,  
  5.                 const struct tm *timeptr   
  6.                 );  

可以根据format指向字符串中格式命令,把timeptr中保存的时间信息放在strDest指向的字符串中,最多向strDest中存放maxsize个字符。该函数返回向strDest指向的字符串中放置的字符个数。格式命令列如下,它们是区分大小写的!!

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. %a 星期几的简写   
  2. %A 星期几的全称   
  3. %b 月分的简写   
  4. %B 月份的全称   
  5. %c 标准的日期的时间串   
  6. %C 年份的后两位数字   
  7. %d 十进制表示的每月的第几天   
  8. %D 月/天/年   
  9. %e 在两字符域中,十进制表示的每月的第几天   
  10. %F 年-月-日   
  11. %g 年份的后两位数字,使用基于周的年   
  12. %G 年分,使用基于周的年   
  13. %h 简写的月份名   
  14. %H 24小时制的小时   
  15. %I 12小时制的小时  
  16. %j 十进制表示的每年的第几天   
  17. %m 十进制表示的月份   
  18. %M 十时制表示的分钟数   
  19. %n 新行符   
  20. %p 本地的AM或PM的等价显示   
  21. %r 12小时的时间   
  22. %R 显示小时和分钟:hh:mm   
  23. %S 十进制的秒数   
  24. %t 水平制表符   
  25. %T 显示时分秒:hh:mm:ss   
  26. %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)  
  27. %U 第年的第几周,把星期日做为第一天(值从0到53)  
  28. %V 每年的第几周,使用基于周的年   
  29. %w 十进制表示的星期几(值从0到6,星期天为0)  
  30. %W 每年的第几周,把星期一做为第一天(值从0到53)   
  31. %x 标准的日期串   
  32. %X 标准的时间串   
  33. %y 不带世纪的十进制年份(值从0到99)  
  34. %Y 带世纪部分的十制年份   
  35. %z,%Z 时区名称,如果不能得到时区名称则返回空字符。  
  36. %% 百分号  


strftime 的示例代码:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include "time.h"  
  2. #include "stdio.h"  
  3.   
  4. int main(void)  
  5. {  
  6.     //显示现在是几点了,并以12小时制显示  
  7.     char str[80];  
  8.     time_t _t = time(NULL);  
  9.     struct tm *ptm = localtime(&_t);  
  10.     strftime(str, 100, "It is now %I %p\n",ptm);  
  11.     printf(str);  
  12.   
  13.     //显示当前的完整日期  
  14.     char tmpbuf[128];     
  15.     time_t It = time( NULL );  
  16.     struct tm *newtime = localtime(&It);  
  17.     strftime(tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime);  
  18.     printf(tmpbuf);  
  19.   
  20.     return 0;  
  21. }  
输出结果:

It is now 03 PM
Today is Tuesday, day 16 of August in the year 2016.


【应用举例】

使用tm和time_t,计算两个日期之间的间隔天数:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <time.h>  
  3. #include <cstdlib>  
  4.   
  5. class DaysCounter  
  6. {  
  7. public:  
  8.     DaysCounter(){}  
  9.     ~DaysCounter(){}  
  10.   
  11.     static int get_date_span(int date1, int date2)  
  12.     {  
  13.         time_t t_of_day1 = time_maker(date1);  
  14.         time_t t_of_day2 = time_maker(date2);  
  15.         double diff = difftime(t_of_day1, t_of_day2);  
  16.         if(diff < 0)  
  17.             diff = 0-diff;  
  18.   
  19.         int count_gap_day = (int)diff/86400;//60*60*24=86400    
  20.         return count_gap_day;  
  21.     }  
  22.   
  23.     static time_t time_maker(int date)  
  24.     {  
  25.         /*struct*/ tm _tm;  
  26.         _tm.tm_year = date/10000-1900;  
  27.         _tm.tm_mon = (date-(_tm.tm_year+1900)*10000)/100-1;  
  28.         _tm.tm_mday = date-(_tm.tm_year+1900)*10000-(_tm.tm_mon+1)*100;  
  29.         _tm.tm_hour = 0;  
  30.         _tm.tm_min = 0;  
  31.         _tm.tm_sec = 1;  
  32.         _tm.tm_isdst = 0;  
  33.   
  34.         return mktime(&_tm);  
  35.     }  
  36.   
  37.     int current_time(time_t& current_time)  
  38.     {  
  39.         time_t _t = time(NULL);  
  40.         struct tm * timeinfo = localtime(&_t);  
  41.         int year = timeinfo->tm_year + 1900;  
  42.         int month = timeinfo->tm_mon + 1;  
  43.         int day = timeinfo->tm_mday;  
  44.         int date_current = year*10000 + month*100 + day;  
  45.         return date_current;  
  46.     }     
  47. };  
  48.   
  49.  int main()  
  50.  {  
  51.     int days = DaysCounter::get_date_span(20150810, 20150815);  
  52.     printf("days : %d\n", days);  
  53.    
  54.     system("pause");  
  55.     return 0;  
  56.  }  
0 0
原创粉丝点击