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());
[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指针可能为空,就会导致程序崩溃。