xszf

导航

Qt下设置Linux系统时间

include <sys/time.h>

bool SysTime(int nYear,int nMonth,int nDay,int nHours,int nMin,int nSecond) {
QDateTime dateTime;
dateTime.setDate(QDate(nYear, nMonth, nDay));
dateTime.setTime(QTime(nHours, nMin, nSecond));

         // 转换为从1970-01-01 00:00:00开始的秒数(时间戳)
        qint64 secondsSinceEpoch = dateTime.toSecsSinceEpoch();

struct timeval tv;
tv.tv_sec = secondsSinceEpoch;  // 设置为一个新的时间,例如 1970-08-02 15:30:00
tv.tv_usec = 0;

if (settimeofday(&tv, NULL) == -1) {
     return false;
 }

return true;
}

posted on 2025-10-15 08:04  xszf  阅读(0)  评论(0)    收藏  举报