QT的時區轉換以及使用注意事項

QDateTime localDate = QDateTime::fromString("2018-12-11 10:40:00", "yyyy-MM-dd hh:mm:ss");//輸入的時間
QDateTime utcDate = localDate.toTimeZone(zone);//拿到的是0時區的當前時間
QDateTime currentTime = QDateTime::currentDateTime();//當前時區的當前時間
QDateTime changeLocaldate = QDateTime::fromMSecsSinceEpoch(localDate.toMSecsSinceEpoch(), Qt::UTC);
QDateTime changeutcDate = changeLocaldate.toTimeZone(zone);
QTimeZone currentZone = currentTime.timeZone();
QString currentZoneId = currentZone.id();//當前時間的當前時區
QDateTime currentUtcTime = QDateTime::currentDateTimeUtc();//當前的UTC時間測試

ERROR_TRACE("Local Date: %s", localDate.toString().toUtf8().data());
ERROR_TRACE("utc Date: %s", utcDate.toString().toUtf8().data());
ERROR_TRACE("Current Date: %s", currentTime.toString().toUtf8().data());
ERROR_TRACE("change Current Date: %s", changeLocaldate.toString().toUtf8().data());
ERROR_TRACE("change Current utc Date: %s", changeutcDate.toString().toUtf8().data());
ERROR_TRACE("Current Zone Id: %s", currentZoneId.toUtf8().data());
ERROR_TRACE("Current UTC Date: %s", currentUtcTime.toString().toUtf8().data());ui

[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.676 96466.536706 Test trace test
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.676 96466.536949 Utc zone id UTC
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.677 96466.537928 Local Date: 週二 12月 11 10:40:00 2018
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.677 96466.538217 utc Date: 週二 12月 11 02:40:00 2018 GMT
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.678 96466.538517 Current Date: 週二 12月 11 11:31:33 2018
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.678 96466.538853 change Current Date: 週二 12月 11 02:40:00 2018 GMT
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.678 96466.539169 change Current utc Date: 週二 12月 11 02:40:00 2018 GMT
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.678 96466.539389 Current Zone Id: Asia/Shanghai
[E:uiMeetData+10304:2104] 2018-12-11 11:31:33.679 96466.539554 Current UTC Date: 週二 12月 11 03:31:33 2018 GMT指針

 

上面是demo測試,QTIMEZone和QDateTime在使用對象的時候,都要判斷一下 isValid(),由於在Qt源碼裏面,這兩個類的實現,都包含一個私有的d指針,來保證二進制兼容。在某些狀況下,這個d指針可能爲空,就會致使程序崩潰。對象

相關文章
相關標籤/搜索