Details
-
Bug
-
Resolution: Done
-
P2: Important
-
5.2.1, 5.4.1
-
None
-
Linux Ubuntu 14.04 x86_64
Description
It is impossible to get correct timestamp from QDateTime if an instance is constructed in specific timezone and QTime is either provided to the constructor or set via setTime(). However if QDateTime is constructed using fromTime_t in explicit timezone, all fields seem to be correct.
The following example demonstrates the issue
#include <QDateTime> #include <QTimeZone> #include <QDebug> int main() { QTimeZone tz6("UTC+06:00"); time_t timestamp20150415 = 1429034400; QDateTime timePureFromTime_t = QDateTime::fromTime_t(timestamp20150415, tz6); qDebug() << "pure fromTime_t: Hours (expected 0):" << timePureFromTime_t.time().hour(); qDebug() << "timestamp preserved:" << (timestamp20150415 == timePureFromTime_t.toTime_t()); time_t randomTimestamp = 1429072085; // setTime results in wrong timestamp QDateTime timeSetTime = QDateTime::fromTime_t(randomTimestamp, tz6); timeSetTime.setTime(QTime(0, 0, 0)); QDateTime timeSetTimeFromTime_t = QDateTime::fromTime_t(timeSetTime.toTime_t(), tz6); qDebug() << "setTime(QTime(0, 0, 0)): Hours (expected 0):" << timeSetTimeFromTime_t.time().hour(); // incorrect timestamp for specified date and time QDateTime timeConstruct = QDateTime(QDate(2015, 4, 15), QTime(0, 0, 0), tz6); QDateTime timeConstructFromTime_t = QDateTime::fromTime_t(timeConstruct.toTime_t(), tz6); qDebug() << "QDateTime(QDate, QTime, QTimeZone): Hours (expected 0):" << timeConstructFromTime_t.time().hour(); return 0; }
Output:
pure fromTime_t: Hours (expected 0): 0 timestamp preserved: true setTime(QTime(0, 0, 0)): Hours (expected 0): 6 QDateTime(QDate, QTime, QTimeZone): Hours (expected 0): 6
I do not expect 6 hours since 0 hours is passed to QTime constructor.
The origin of the bug may be similar to QTBUG-39734