C++ 字符串转时间 与 时间转转字符串[通俗易懂]

C++ 字符串转时间 与 时间转转字符串[通俗易懂]1、常用的时间存储方式1)time_t类型,这本质上是一个长整数,表示从1970-01-0100:00:00到目前计时时间的秒数,如果需要更精确一点的,可以使用timeval精确到毫秒。2)tm结构,这本质上是一个结构体,里面包含了各时间字段structtm{inttm_sec;/*secondsafterthe…

大家好,又见面了,我是你们的朋友全栈君。

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。  
  
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); //获取目前秒时间  
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_; //秒时间  
}  

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/137035.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea 配置SVN_idea的svn插件

    idea 配置SVN_idea的svn插件如何解决idea中配置svn不能用的问题Can’tuseSubversioncommandlineclient:svnThepathtotheSubversionexecutableisprobablywrong.Fixit.mac自带svnsvn–versionbrewlistsubversion然后idea配置文件更改如果默认这样有问题更改为…

    2022年9月6日
    3
  • python从linux下载文件_python gzip

    python从linux下载文件_python gzip解决python调用OpenCV保存视频时使用”avc1″格式出现#Couldnotfindencoderforcodecid27:Encodernotfound的错误(此错误不能保存视频文件),以及使用”mpeg”格式出现的#OpenCV:FFMPEG:tag0x6765706d/’mpeg’isnotsupportedwithcodecid2a…

    2022年9月25日
    0
  • Centos部署禅道项目管理软件

    Centos部署禅道项目管理软件

    2021年6月2日
    122
  • JS中height、clientHeight、scrollHeight、offsetHeight区别

    JS中height、clientHeight、scrollHeight、offsetHeight区别我们来实现test中的onclick事件  functionjustAtest()    {       vartest= document.getElementById(“test”);       vartest2=document.getElementById(“test2”)       vartest3=document.getElementB

    2022年7月23日
    10
  • PLC编程从入门到精通视频教程【副业学习会】

    PLC编程从入门到精通视频教程【副业学习会】PLC编程视频教程共73课,从入门到精通。从基础讲起,一步步提高PLC编程技巧。本套教程分为:电工基础教程、PLC入门教程、PLC高级教程、PLC经验与技巧、触摸屏(人机)编程教学。此视频通俗易懂,而且很实用。![在这里插入图片描述](https://img-blog.csdnimg.cn/20210715203622364.png)课程目录:第1章电工基础教程01电工基础的简介.mp402工厂用电.mp403看懂基本电路.mp404自锁、正反转电路.m…

    2022年9月4日
    2
  • 常用的go代理

    常用的go代理GOPROXY,目前国内常用的go代理goproxy.iohttps://goproxy.io,direct七牛云https://goproxy.cn阿里云https://mirrors.aliyun.com/goproxy/

    2022年7月26日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号