侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

time_t tm timeval 和 时间字符串的转换方法

技术  /  管理员 发布于 7年前   271

1、常用的时间存储方式  

1)time_t类型,这本质上是一个长整数,表示从1970-01-01 00:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。  

2)tm结构,这本质上是一个结构体,里面包含了各时间字段 

struct tm {     int tm_sec;   /* seconds after the minute - [0,59] */     int tm_min;   /* minutes after the hour - [0,59] */     int tm_hour;  /* hours since midnight - [0,23] */     int tm_mday;  /* day of the month - [1,31] */     int tm_mon;   /* months since January - [0,11] */     int tm_year;  /* years since 1900 */     int tm_wday;  /* days since Sunday - [0,6] */     int tm_yday;  /* days since January 1 - [0,365] */     int tm_isdst;  /* daylight savings time flag */     }; 

 其中tm_year表示从1900年到目前计时时间间隔多少年,如果是手动设置值的话,tm_isdst通常取值-1。 

3)struct timeval结构体在time.h中的定义为

struct timeval {     time_t    tv_sec;   /* seconds */     suseconds_t  tv_usec; /* microseconds */ };

2、常用的时间函数 

time_t time(time_t *t); //取得从1970年1月1日至今的秒数 char *asctime(const struct tm *tm); //将结构中的信息转换为真实世界的时间,以字符串的形式显示 char *ctime(const time_t *timep); //将timep转换为真是世界的时间,以字符串显示,它和asctime不同就在于传入的参数形式不一样 struct tm *gmtime(const time_t *timep); //将time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针  struct tm *localtime(const time_t *timep); //和gmtime类似,但是它是经过时区转换的时间。 time_t mktime(struct tm *tm); //将struct tm 结构的时间转换为从1970年至今的秒数 int gettimeofday(struct timeval *tv, struct timezone *tz); //返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用 double difftime(time_t time1, time_t time2); //返回两个时间相差的秒数 

3、时间与字符串的转换  

需要包含的头文件如下  

#include <iostream> 
#include <time.h> 
#include <stdlib.h> 
#include <string.h>  

1)unix/windows下时间转字符串参考代码 

time_t t; //秒时间 tm* local; //本地时间  tm* gmt;  //格林威治时间 char buf[128]= {0};  t = time(NULL); //或者time(&t);//获取目前秒时间 local = localtime(&t); //转为本地时间 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", local); std::cout << buf << std::endl;  gmt = gmtime(&t);//转为格林威治时间 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", gmt); std::cout << buf << std::endl; 

2)unix字符串转时间参考代码  

 tm tm_; time_t t_; char buf[128]= {0};  strcpy(buf, "2012-01-01 14:00:00"); strptime(buf, "%Y-%m-%d %H:%M:%S", &tm_); //将字符串转换为tm时间 tm_.tm_isdst = -1; t_ = mktime(&tm_); //将tm时间转换为秒时间 t_ += 3600; //秒数加3600  tm_ = *localtime(&t_);//输出时间 strftime(buf, 64, "%Y-%m-%d %H:%M:%S", &tm_); std::cout << buf << std::endl;  

3)由于windows下没有strptime函数,所以可以使用scanf来格式化  

time_t StringToDatetime(char *str) {   tm tm_;   int year, month, day, hour, minute,second;   sscanf(str,"%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);   tm_.tm_year = year-1900;   tm_.tm_mon  = month-1;   tm_.tm_mday = day;   tm_.tm_hour = hour;   tm_.tm_min  = minute;   tm_.tm_sec  = second;   tm_.tm_isdst = 0;    time_t t_ = mktime(&tm_); //已经减了8个时区   return t_; //秒时间 } 

4)timeval获取时间示例:

struct timeval start_time, over_time, consume_time;gettimeofday(&over_time, NULL);//get the current timestart_time = over_time; do something.....gettimeofday(&over_time, NULL);timeval_subtract(&consume_time, &start_time, &over_time);//计算时间差104./**    * 计算两个时间的间隔,得到时间差    * @param struct timeval* resule 返回计算出来的时间    * @param struct timeval* x 需要计算的前一个时间    * @param struct timeval* y 需要计算的后一个时间    * return -1 failure ,0 success  **/ timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y)  {     if ( x->tv_sec>y->tv_sec )          return -1;       if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) )          return -1;       result->tv_sec = ( y->tv_sec-x->tv_sec );     result->tv_usec = ( y->tv_usec-x->tv_usec );       if (result->tv_usec<0)     {          result->tv_sec--;          result->tv_usec+=1000000;     }       return 0;  }

4、关于localtime与localtime_r的区别

struct tm *localtime(const time_t *timep);

这个函数在返回的时候,返回的是一个指针,实际的内存是localtime内部通过static申请的静态内存,所以通过localtime调用后的返回值不及时使用的话,很有可能被其他线程localtime调用所覆盖掉

struct tm *localtime_r(const time_t *timep, struct tm *result);

localtime_r则是由调用者在第二个参数传入一个struct tm result指针,该函数会把结果填充到这个传入的指针所指内存里面;成功的返回值指针也就是struct tm result。

多线程应用里面,应该用localtime_r函数替代localtime函数,因为localtime_r是线程安全的。

其他的时间函数,如asctime,asctime_r;ctime,ctime_r;gmtime,gmtime_r都是类似的,所以,<strong>时间函数的 _r 版本都是线程安全的。</strong>
</span>

以上这篇time_t tm timeval 和 时间字符串的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    IO复用之select poll epoll的总结(推荐)
    下一条:
    cloudera manager 设置开机自启的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客