C++ /qt 获取当前时间的几种方法

方法一:std标准函数方式

cpp 复制代码
#include <iostream>
#include <ctime>
#include <chrono>
#include <iomanip>

int getCurFormatTime(char* fmtTime, bool convertTwsj=false)
{
    // 使用high_resolution_clock获取当前时间点
    auto now = std::chrono::high_resolution_clock::now();
    // 将时间点转换为time_t以便转换为本地时间
    std::time_t now_time = std::chrono::high_resolution_clock::to_time_t(now);
    if (convertTwsj) {
        now_time = now_time - 3600*8;
    }
    // 转换为本地时间并格式化
    std::tm* local_time = std::localtime(&now_time);
    // 输出格式化时间
    std::_Put_time<char> a = std::put_time(local_time, "%Y-%m-%d %H:%M:%S");
    sprintf(fmtTime, "%d-%02d-%02d %02d:%02d:%02d", a._M_tmb->tm_year+1900,a._M_tmb->tm_mon+1,
            a._M_tmb->tm_mday, a._M_tmb->tm_hour, a._M_tmb->tm_min, a._M_tmb->tm_sec);
    //qDebug()<< "当前系统时间:" << fmtTime;
    return 0;
}

方式二,qt

cpp 复制代码
qt 获取当前时间
获取当前时间可以使用Qt的QDateTime类来实现。以下是几种不同的代码实现方法:

方法一:使用静态函数QDateTime::currentDateTime()获取当前时间。
#include <QDateTime>
 
QDateTime currentTime = QDateTime::currentDateTime();
qDebug() << "Current Time: " << currentTime;

方法二:使用QDateTime类的构造函数和QDateTime::currentDateTime()函数
#include <QDateTime>
 
QDateTime currentTime(QDateTime::currentDateTime());
qDebug() << "Current Time: " << currentTime;

方法三:使用QDateTime::currentDateTimeUtc()获取当前协调世界时间(UTC)。
#include <QDateTime>
 
QDateTime currentUtcTime = QDateTime::currentDateTimeUtc();
qDebug() << "Current UTC Time: " << currentUtcTime.toLocalTime();

方法四:使用QDateTime::currentMSecsSinceEpoch()获取当前时间的毫秒数。
#include <QDateTime>
 
qint64 currentTimeMs = QDateTime::currentMSecsSinceEpoch();
qDebug() << "Current Time (in milliseconds): " << currentTimeMs;

字符串时间转本地时间

cpp 复制代码
#include <QDateTime>
#include <QString>
#include <QDebug>
 
int main() {
    // 假设我们有一个字符串格式的时间
    QString timeString = "2023-04-01 12:00:00";
 
    // 将字符串转换为QDateTime对象
    QDateTime dateTime = QDateTime::fromString(timeString, "yyyy-MM-dd hh:mm:ss");
 
    // 如果需要转换为本地时间,可以使用以下代码
    QDateTime localDateTime = dateTime.toLocalTime();
 
    // 打印转换后的时间
    qDebug() << "Local Time: " << localDateTime.toString("yyyy-MM-dd hh:mm:ss");
 
    return 0;
}

字符串时间转UTC时间

cpp 复制代码
#include <QDateTime>
#include <QTimeZone>
#include <QString>
#include <QDebug>
 
int main(int argc, char *argv[])
{
    // 假设我们有一个字符串表示的本地时间
    QString localTimeString = "2023-04-01 12:00:00";
 
    // 使用QDateTime将字符串转换为本地时间的QDateTime对象
    QDateTime localDateTime = QDateTime::fromString(localTimeString, "yyyy-MM-dd hh:mm:ss");
 
    // 获取当前系统时区
    QTimeZone systemTimeZone = QTimeZone::systemTimeZone();
 
    // 转换为UTC时间
    QDateTime utcDateTime = localDateTime.toUTC(systemTimeZone);
 
    // 输出UTC时间
    qDebug() << "UTC Time: " << utcDateTime.toString("yyyy-MM-dd hh:mm:ss");
 
    return 0;
}
相关推荐
小坏坏的大世界1 小时前
C++ STL常用容器总结(vector, deque, list, map, set)
c++·算法
我命由我123452 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
liulilittle2 小时前
C++ TAP(基于任务的异步编程模式)
服务器·开发语言·网络·c++·分布式·任务·tap
励志要当大牛的小白菜4 小时前
ART配对软件使用
开发语言·c++·qt·算法
武子康4 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
PAK向日葵5 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
灵感__idea5 小时前
JavaScript高级程序设计(第5版):好的编程就是掌控感
前端·javascript·程序员
烛阴6 小时前
Mix
前端·webgl
代码续发6 小时前
前端组件梳理
前端
试图让你心动7 小时前
原生input添加删除图标类似vue里面移入显示删除[jquery]
前端·vue.js·jquery