Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
5.12.4
-
timezone: CEST
-
-
21
-
3f44e0fc5f1d55498ce7fb93a7841198a7583a08 (qt/qtdeclarative/5.14)
-
Foundation PM Staging
Description
The problem
When formatting a QTime object in QML, the hour is wrong (due to DST inconsistencies):
// main.cpp ... QQmlEngine *pQmlEngine = new QQmlEngine; pQmlEngine->rootContext()->setContextProperty("myCESTDate", QTime(18,0)); ...
// main.qml ... console.log(myCESTDate); // prints "Mon Jun 8 17:00:00 1925 GMT+0200" ...
The reason
When converting a QTime object from C++ to qml daylight saving time (DST) is determined using localtime_r (call hierarchy: Heap::DateObject::init > UTC > DaylightSavingTA), which returns DST is true.
However, the qml (js) Date object is converted back to a QDateTime object using QDateTime::toTimeSpec, which internally calls epochMSecsToLocalTime, which does never sets DST for dates before 1970-01-01:
static bool epochMSecsToLocalTime(qint64 msecs, QDate *localDate, QTime *localTime, QDateTimePrivate::DaylightStatus *daylightStatus = 0) { if (msecs < 0) { // Docs state any LocalTime before 1970-01-01 will *not* have any Daylight Time applied // Instead just use the standard offset from UTC to convert to UTC time qt_tzset(); msecsToTime(msecs - qt_timezone() * 1000, localDate, localTime); if (daylightStatus) *daylightStatus = QDateTimePrivate::StandardTime; return true; } ... }
On Windows, this does not seem to be a problem, but on macOS and iOS the above (wrong) behaviour is observed.
The solution
A solution may be to choose a date after 1970-01-01 (instead of 1925-6-8) when converting a QTime object to a qml (js) Date object (see function Heap::DateObject::init, including the implementation comment inside this function).
Work around
A work around (without changing Qt's source code) would be to explicitly specify a (random or current) date after 1970-01-01, when passing a QTime object to qml:
...
pQmlEngine->rootContext()->setContextProperty("myCESTDate", QDateTime(QDate::currentDate(), QTime(18,0)));
...
Attachments
Issue Links
- relates to
-
QTBUG-92466 Recommend that instead of passing a QTime directly from C++ to QML, that it is passed as a QDateTime instead so that the timezone can be explicitly set to UTC to avoid any problems with conversion
- Closed
Gerrit Reviews
For Gerrit Dashboard: QTBUG-78706 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
275000,4 | Convert QTime to V4 Date on a date with no DST in force | 5.14 | qt/qtdeclarative | Status: MERGED | +2 | 0 |