unix环境C编程之日期时间转换

1、理清概念

1.1、日历时间:
  含义:国际标准时间1970年1月1日00:00:00以来经过的秒数。
  数据类型:time_t。实际上是long的别名。
1.2、tm结构时间:
  含义:结构化存放时间的数据结构,方便查看。ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)
  数据类型:
    struct tm
    {
          int  tm_sec;       /* 秒数,范围[0,60] */
          int  tm_min;       /* 分钟,范围[0,59] */
          int  tm_hour;      /* 小时,范围[0,23] */
          int  tm_mday;    /* 日,范围[1,31] */
          int  tm_mon;      /* 月,范围[0,11] */
          int  tm_year;      /* 年,从1900年开始的年数,为实际年份-1900 */
          int  tm_wday;     /* 星期,周日为0,范围[0,6] */
          int  tm_yday;     /* 日,从每年1月1日开始的天数,范围[0,365] */
          int  tm_isdst;      /* 夏令时标识符,实行夏令时的时候,tm_isdst为正,不实行是为0,不了解情况时为负 */
   }
 
2、时间转换
 
2.1、时间转换函数
以下时间转换函数在time.h中定义 :
   (1) time_t time(time_t *calptr);
        功能:返回当前日期和时间。
        参数:若calptr不为空,则返回的时间值也存放在calptr指向的单元内。
        返回值:若成功则返回时间值,不成功则返回-1。
   (2) struct tm * gmtime(const time_t *calptr);
        功能:将日历时间转成国际标准时间的年、月、日、时、分、秒、周日。
        参数:日历时间。
        返回值:指向struct tm时间的指针。
   (3) struct tm * localtime(const time_t *calptr);
        功能:将日历时间转成本地时间(考虑到本地时区和夏时制标志)。
        参数:日历时间。
        返回值:指向struct tm时间的指针。
   (4) time_t mktime(struct tm *tmptr);
        功能:将时间结构数据转换成经过的秒数。
        参数:tm结构时间。
        返回值:日历时间。
   (5) char *asctime(const struct tm *tmptr);
        功能:将时间结构数据转换为相应的字符串(英文简写形式)。
        参数:tm结构数据。
        返回值:时间字符串。
   (6) char *ctime(const time_t *calptr);
         功能:将日历时间转成字符串。
         参数:日历时间。     
         返回值:时间字符串。
   (7) size_t strftime(char *restrict but, size_t maxsize,
                             const char *restrict format,
                             const struct tm *restrict tmptr);
         功能:将时间结构数据转成格式化字符串。
         参数:buf存放格式化的结果。maxsize为buf的长度(该长度需要保证能够存放字符串以及一个null终止符)。
                  format为格式化参数,类似printf的格式化。tmptr指向一个tm结构的时间数据。
         返回值:若buf能够保存结构则返回存入数组的字符数(不包括null终止符),否则返回0。
 
2.2、转换关系图为:
(图中以虚线表示的四个函数localtime、mktime、ctime和strftime都受到环境变量TZ的影响)
 
3、strftime的转换说明

格式

说明

实例

%a

缩写的星期名

Thu

%A

全星期名

Thursday

%b

缩写的月名

Aug

%B

全月名

August

%c

日期和时间

Thu Aug 23 14:55:02 2001

%C

年/100 [00,99]

20

%d

十进制表示的每月的第几天 [01,31]

16

%D

月/天/年 [MM/DD/YY]

06/16/12

%e

十进制表示的每月的第几天 ,一位数前加空格[1,31]

10

%F

年-月-日 [YYYY-MM-DD]

2012-06-16

%g

ISO 8601使用基于周的年的后两位数字 

12

%G

ISO 8601使用基于周的年

2012

%h

简写的月份名 ,与%b相同

Aug

%H

24小时制的小时 [00,23]

14

%I

12小时制的小时 [01,12]

02

%j

每年的第几天 [001,366]

235

%m

十进制的月 [01,12]

08

%M

分钟 [00,59]

55

%n

换行符

 

%p

AM/PM 

PM

%r

本地时间:(12时制)

11:01:23 AM

%R

与“%H:%M”相同

11:01

%S

秒 [00,60]

02

%t

水平制表符

 

%T

与“%H:%M:%S”相同

11:01:23

%u

ISO 8601的星期,星期一为1,[1,7]

2

%U

周数 ,以周日为一周开始 [00,53]

33

%V

ISO 8601周数 [01,53]

07

%w

星期,星期天为0. [0,6]

4

%W

周数,以星期一为一周开始 [00,53]

34

%x

标准日期

06/16/12

%X

标准时间

14:55:02

%y

年份的后两位数字 [00,99]

12

%Y

2012

%z

ISO 8601格式的UTC偏移量

-0500

%Z

时区名

EST

%%

百分号

%

4、举例
 
代码:
[cpp] view plaincopy
 
  1. #include<stdio.h>  
  2. #include<time.h>  
  3.   
  4. int main()  
  5. {  
  6.   //获取当前日历时间  
  7.   time_t t;  
  8.   t = time(&t);  
  9.   printf("the current time of seconds:[%d], string is:[%s] ",t, ctime(&t));  
  10.   
  11.  //日历时间转成本地tm结构时间  
  12.   struct tm* tm_t = localtime(&t);  
  13.   char buf[100];  
  14.   strftime(buf, 100,"%F %T",tm_t);   
  15.   printf("get struct tm tm_t from seconds is:[%s] ", buf);  
  16.   
  17.   //tm结构时间转成日历时间  
  18.   time_t t1 = mktime(tm_t);  
  19.   printf("get seconds from struct tm:[%d] ",t);  
  20.   
  21.   //自定义tm结构时间  
  22.   struct tm tm_t1;  
  23.   tm_t1.tm_year=2012-1900;  
  24.   tm_t1.tm_mon=0;  
  25.   tm_t1.tm_mday=16;  
  26.   tm_t1.tm_hour=0;  
  27.   tm_t1.tm_sec=0;  
  28.   tm_t1.tm_min=0;  
  29.   strftime(buf, 100,"%F %T",&tm_t1);   
  30.   printf("the struct tm tm_t1 is:[%s] ", buf);  
  31.   
  32. }  


结果:
the current time of seconds:[1339819819], string is:[Fri Jun 15 21:10:19 2012
]
get struct tm tm_t from seconds is:[2012-06-15 21:10:19]
get seconds from struct tm:[1339819819]
the struct tm tm_t1 is:[2012-01-16 00:00:00]
 
说明:
1、ctime得到的字符串有换行符
2、tm结构的tm_year为从1900开始的年数
2、tm结构的tm_mon为0时表示1月
原文地址:https://www.cnblogs.com/sankye/p/4240869.html