time_t和struct tm之间的转化

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)。
![配置框](https://img-blog.csdnimg.cn/20190612195219519.png

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为8
3600(从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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值