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

相关推荐
qq_37215423几秒前
如何配置表中某列的排序权重_全文索引配置与权重分配
jvm·数据库·python
CoderCodingNo1 分钟前
【信奥业余科普】C++ 的奇妙之旅 | 14:程序的分叉路口——逻辑判断与 if-else 语句
开发语言·c++
The Chosen One9859 分钟前
a进制转b进制的转换总结
开发语言·c++
2501_9142459311 分钟前
CSS如何使用-nth-of-type精确选择列表项_通过元素类型限制提升样式健壮性
jvm·数据库·python
吕源林18 分钟前
Golang如何做本地缓存加速_Golang本地缓存教程【核心】
jvm·数据库·python
tankeven21 分钟前
C++ 学习杂记05:std::map
c++
Magic@27 分钟前
Redis学习[1] ——基本概念和数据类型
linux·开发语言·数据库·c++·redis·学习
黑不溜秋的28 分钟前
C++ STL reduce 用法
开发语言·c++
你觉得脆皮鸡好吃吗36 分钟前
SQL注入 基础防御
数据库·sql
池佳齐37 分钟前
软考高级系统架构设计师备考(十九):数据库系统—数据库设计
数据库·系统架构