unix环境C编程之日期时间转换
2013-08-22 15:25
253 查看
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 buf, 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、转换关系图为:
![](http://my.csdn.net/uploads/201206/16/1339821455_4538.png)
![](file:///C:/Users/wangyuan/AppData/Local/Temp/enhtmlclip/Image.png)
(图中以虚线表示的四个函数localtime、mktime、ctime和strftime都受到环境变量TZ的影响)
3、strftime的转换说明
4、举例
代码:
结果:
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]
[align=left]说明:[/align]
1、ctime得到的字符串有换行符
2、tm结构的tm_year为从1900开始的年数
2、tm结构的tm_mon为0时表示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 buf, 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、转换关系图为:
![](http://my.csdn.net/uploads/201206/16/1339821455_4538.png)
![](file:///C:/Users/wangyuan/AppData/Local/Temp/enhtmlclip/Image.png)
(图中以虚线表示的四个函数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 |
%% | 百分号 | % |
代码:
#include<stdio.h> #include<time.h> int main() { //获取当前日历时间 time_t t; t = time(&t); printf("the current time of seconds:[%d], string is:[%s]\n",t, ctime(&t)); //日历时间转成本地tm结构时间 struct tm* tm_t = localtime(&t); char buf[100]; strftime(buf, 100,"%F %T",tm_t); printf("get struct tm tm_t from seconds is:[%s]\n", buf); //tm结构时间转成日历时间 time_t t1 = mktime(tm_t); printf("get seconds from struct tm:[%d]\n",t); //自定义tm结构时间 struct tm tm_t1; tm_t1.tm_year=2012-1900; tm_t1.tm_mon=0; tm_t1.tm_mday=16; tm_t1.tm_hour=0; tm_t1.tm_sec=0; tm_t1.tm_min=0; strftime(buf, 100,"%F %T",&tm_t1); printf("the struct tm tm_t1 is:[%s]\n", buf); }
结果:
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]
[align=left]说明:[/align]
1、ctime得到的字符串有换行符
2、tm结构的tm_year为从1900开始的年数
2、tm结构的tm_mon为0时表示1月
相关文章推荐
- unix环境C编程之日期时间转换
- UNIX环境高级编程——时间和日期
- Unix/Linux环境C编程入门教程(28) 日期时间那些事儿
- Unix/Linux环境C编程入门教程(28) 日期时间那些事儿
- Unix环境编程-时间和日期
- UNIX环境高级编程——时间和日期
- UNIX环境高级编程——时间和日期
- Java将Unix时间戳转换成指定格式日期
- UNIX时间戳及日期的转换与计算
- UNIX环境编程学习笔记(11)——文件I/O之文件时间以及 utime 函数
- Mysql日期时间类型(DATE,DATETIME,TIMESTAMP)和函数及与linux系统时间之间的转换(UNIX_TIMESTAMP、FROM_UNIXTIME)
- oracle中日期类型与unix 时间戳的转换
- Java将Unix时间戳转换成指定格式日期
- UNIX环境编程学习笔记(11)——文件I/O之文件时间以及 utime 函数
- 【转】UNIX时间戳与.net日期类的转换
- unix时间戳转换为日期格式
- Excel将Unix时间戳转换为日期
- PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)
- oracle中日期类型与unix 时间戳的转换
- oracle中日期类型与unix 时间戳的转换