Advertisement

Qt本地时间与UTC时间转换

阅读量:

1,主要用到下面这个函数:

void QDateTime::setTimeSpec(Qt::TimeSpec spec)

Sets the time specification used in this datetime to spec. The datetime will refer to a different point in time.
If spec is Qt::OffsetFromUTC then the timeSpec() will be set to Qt::UTC, i.e. an effective offset of 0.
If spec is Qt::TimeZone then the spec will be set to Qt::LocalTime, i.e. the current system time zone.

复制代码
     QString strTime = "2020-10-30 08:42:58";

    
     QString fromat = "yyyy-MM-dd hh:mm:ss";
    
     QDateTime time = QDateTime::fromString(strTime, fromat);
    
  
    
     //如果,strTime是utc时间,转换为本地时间
    
     time.setTimeSpec(Qt::UTC);
    
     QDateTime localTime = time.toLocalTime();
    
  
    
     //如果,strTime是本地时间,转换为utc时间
    
     time.setTimeSpec(Qt::LocalTime);
    
     QDateTime utcTime = time.toUTC();
    
  
    
     qDebug()<<"time:      "<<time.toString(fromat);
    
     qDebug()<<"localTime: "<<localTime.toString(fromat);
    
     qDebug()<<"utcTime:   "<<utcTime.toString(fromat);

运行结果:

复制代码
 time:       "2020-10-30 08:42:58"

    
 localTime:  "2020-10-30 16:42:58"
    
 utcTime:    "2020-10-30 00:42:58"

全部评论 (0)

还没有任何评论哟~