c++ QT 十八位时间戳转换

先说一下UTC: 它是协调世界时间,又称世界统一时间、世界标准时间、国际协调时间,简称UTC

UTC时间与本地时间关系:UTC +时间差=本地时间

如果UTC时间是 2015-05-01 00:00:00

那么北京时间就是 2015-05-01 08:00:00

解释:

116444736000000000

是从1601年1月1日0:0:0:000到1970年1月1日0:0:0:000的时间(单位100ns)

  1. 解析一串 133395047197220000 数字转成UTC时间和本地时间(两种方式)
cpp 复制代码
	   /*QT方法解析*/
       long long currentMSecs = 133395047197220000;
       long long milliseconds =( long long )( currentMSecs- 116444736000000000) / 10000;
       
       QDateTime f = QDateTime::fromMSecsSinceEpoch(milliseconds);
       qDebug() << f.toUTC().toString("yyyy-MM-dd hh:mm:ss:zzz");//UTC的
       qDebug() << f.toString("yyyy-MM-dd hh:mm:ss:zzz");//本地的时间

结果:

"2023-09-18 09:58:39:722"

"2023-09-18 17:58:39:722"

cpp 复制代码
		/*C/C++方法解析*/
		long long currentMSecs = 133395047197220000;
		long long milliseconds =( long long )( currentMSecs- 116444736000000000) / 10000;
		std::time_t current_time = milliseconds / 1000;
       // 转换成本地时间
        std::tm* local_time = std::localtime(&current_time);

        // 输出年月日时分秒毫秒
        std::cout<< "Local date and time: "<<std::endl ;
        std::cout<< local_time->tm_year + 1900 << "-" << local_time->tm_mon + 1 << "-" << local_time->tm_mday << " "<<local_time->tm_hour << ":" << local_time->tm_min << ":" << local_time->tm_sec << "." <<(milliseconds % 1000) <<std::endl ;

        std::cout << "UTC date and time: "<<std::endl  ;
        std::cout<< local_time->tm_year + 1900 << "-" << local_time->tm_mon + 1 << "-" << local_time->tm_mday << " "<<local_time->tm_hour - 8 << ":" << local_time->tm_min << ":" << local_time->tm_sec << "." <<(milliseconds % 1000)<<std::endl  ;

结果:

Local date and time:

2023-9-18 17:58:39.722

UTC date and time:

2023-9-18 9:58:39.722

  1. 生成当前时间 UTC 十八位时间戳
cpp 复制代码
    /*QT生成方法*/
    QDateTime t = QDateTime::currentDateTimeUtc();
    qDebug() << t.toString("yyyy-MM-dd hh:mm:ss:zzz");
    long long currentMSecs = t.currentMSecsSinceEpoch();
    // 获取从1970-01-01 00:00:00到现在的秒数
    currentMSecs =(currentMSecs * 10000 )+ 116444736000000000;
 
    qDebug()<<currentMSecs;

结果:

"2023-09-18 09:58:39:719"

133395047197220000

相关推荐
Want59510 分钟前
C/C++跳动的爱心①
c语言·开发语言·c++
lingggggaaaa20 分钟前
免杀对抗——C2远控篇&C&C++&DLL注入&过内存核晶&镂空新增&白加黑链&签名程序劫持
c语言·c++·学习·安全·网络安全·免杀对抗
phdsky32 分钟前
【设计模式】建造者模式
c++·设计模式·建造者模式
H_-H34 分钟前
关于const应用与const中的c++陷阱
c++
coderxiaohan39 分钟前
【C++】多态
开发语言·c++
gfdhy1 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
m***56721 小时前
Win10下安装 Redis
数据库·redis·缓存
Warren981 小时前
Python自动化测试全栈面试
服务器·网络·数据库·mysql·ubuntu·面试·职场和发展
leon_zeng01 小时前
Qt Modern OpenGL 入门:从零开始绘制彩色图形
开发语言·qt·opengl
会飞的胖达喵1 小时前
Qt CMake 项目构建配置详解
开发语言·qt