time_t和struct tm之间的转化
time_t和struct tm结构体
1:ubuntu man文档对time_t的解释
从UTC 1970年1月1日0时0分0秒开始经过的描述。例如time_t tt = 2;可以认为是从UTC 1970年1月1日0时0分0秒开始经过了2秒的时间。
2:ubuntu man文档对Struct tm的解释,
保存着年月日时分秒等具体时间点信息,需要注意的是,年份是从1900年开始的,例如119指的是2019年,月份是从零开始的,0指的是1月(January)。
3:Linux常用时间函数
struct tm *localtime(const time_t *timep);
struct tm localtime_r(const time_t timep, struct tm result);
/**函数将time_t转换为用年月日时分秒表示的struct tm结构,结果为本地时间。例如给定time_t为83600(从UTC 1970-01-01:00:00:00开始的8小时),当本地电脑时区为上海时区(东八区)时,转换结果为1970-01-01:00:00:00,当本地电脑为中央时区(0时区)时,转换结果为1970-01-01:08:00:00
localtime_r是线程安全的/
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
/asctime 将时间以换为字符串字符串格式返回,asctime_r为线程安全/
time_t mktime(struct tm *tm);
/函数将struct tm结构的本地时间转换成time_t,本地电脑时区如果为上海时区(东八区),1970-01-01:00:00:00将转换成-28800(东八区时间比格林威治时间快了28800秒),电脑时区为中央时区,则会转化为0/
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
/gmtime是把本地日期和时间转换为格林威治(GMT)时间的函数。如果本地电脑时区为上海时区(东八区),那么28800将会被转换成1970-01-01:00:00:00/
Time_t timelocal(struct tm *tm);
Time_t timegm(struct tm *tm);
/函数将年月日时分秒表示的struct tm格式的时间转换为time_t格式,timelocal将参数传入的时间按照本地时间处理,而timegm将参数传入的时间按照GMT时间处理。于是,timelocal返回的结果跟本地电脑时区有关,上海时区(东八区)的情况下将1970-01-01:08:00:00转换为0.timegm将1970-01-01:08:00:00转化为28800/
/*根据文档,这两个函数不是posix标准,可以使用的替代方案参考
https://stackoverflow.com/questions/283166/easy-way-to-convert-a-struct-tm-expressed-in-utc-to-time-t-type **/
4:相关代码及其运行结果
#include <time.h>
#include <stdio.h>
void mktime_test()
{
/*1970-01-01:00:00:00*/
struct tm tm_;
tm_.tm_year = 70;/* Year - 1900 */
tm_.tm_mon= 0;/* Month (0-11) */
tm_.tm_mday=1;/* Day of the month (1-31) */
tm_.tm_hour=0;/* Hours (0-23) */
tm_.tm_min=0;/* Minutes (0-59) */
tm_.tm_sec=0;/* Seconds (0-60) */
tm_.tm_isdst=0;/* Daylight saving time */
time_t tt = mktime(&tm_);
printf("###mktime_test###\n");
printf("struct tm[1970-01-01:00:00:00] -> time_t[%ld]\n\n", tt);
}
void gmtime_test()
{
struct tm *ptm_;
/*1970-01-01:00:00:00*/
time_t tt = 28800;
ptm_ = gmtime(&tt);/*to gmt time*/
printf("###gmtime_test###\n");
printf("time_t[28800] -> struct tm[%s\n", asctime(ptm_));
}
void gmt_tm_to_gmt_time_t()
{
/*1970-01-01:00:00:00*/
struct tm tm_;
tm_.tm_year = 119;/* Year - 1900 */
tm_.tm_mon= 5;/* Month (0-11) */
tm_.tm_mday=13;/* Day of the month (1-31) */
tm_.tm_hour=8;/* Hours (0-23) */
tm_.tm_min=22;/* Minutes (0-59) */
tm_.tm_sec=0;/* Seconds (0-60) */
tm_.tm_isdst=0;/* Daylight saving time */
//time_t tt = mktime(&tm_) + timezone;
time_t tt = timegm(&tm_);
printf("###gmt_tm_to_gmt_time_t###\n");
printf("struct tm[2019-06-13:08:22:00] -> time_t[%ld]\n\n", tt);
}
void local_tm_to_local_time_t()
{
/*1970-01-01:00:00:00*/
struct tm tm_;
tm_.tm_year = 119;/* Year - 1900 */
tm_.tm_mon= 5;/* Month (0-11) */
tm_.tm_mday=13;/* Day of the month (1-31) */
tm_.tm_hour=8;/* Hours (0-23) */
tm_.tm_min=22;/* Minutes (0-59) */
tm_.tm_sec=0;/* Seconds (0-60) */
tm_.tm_isdst=0;/* Daylight saving time */
time_t tt = timelocal(&tm_);
printf("###local_tm_to_local_time_t###\n");
printf("struct tm[2019-06-13:08:22:00] -> time_t[%ld]\n", tt);
}
void main()
{
mktime_test();
gmtime_test();
gmt_tm_to_gmt_time_t();
local_tm_to_local_time_t();
}
运行结果:
参考资料:
linux几种时间函数总结: https://www.cnblogs.com/wenqiang/p/5678451.html
GMT时间和UTC时间介绍: https://www.cnblogs.com/tosee/p/5538007.html
C++ 时间类型及相互转换详解 time_t与tm: https://blog.csdn.net/ffcjjhv/article/details/83376767
c++ 时间类型详解 time_t: https://blog.csdn.net/love_gaohz/article/details/6637625