From 402f994f12fc524871f2b1d2337ab07ad3d412b8 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sat, 21 Feb 2015 23:31:02 -0800 Subject: [PATCH] Timezones: Fix handling of offset-from-UTC QTimeZones Those QTimeZones failed to convert to other timezones because the data() virtual function was never overridden and reimplemented. That meant all QUtcTimeZonePrivate objects were *really* UTC, with no offset. Task-number: QTBUG-44600 Change-Id: Ia0aac2f09e9245339951ffff13c5294bb783c674 Reviewed-by: Marc Mutz --- src/corelib/tools/qtimezoneprivate.cpp | 9 +++++++++ src/corelib/tools/qtimezoneprivate_p.h | 2 ++ tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp | 12 ++++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/corelib/tools/qtimezoneprivate.cpp b/src/corelib/tools/qtimezoneprivate.cpp index e768905462..4f6067f508 100644 --- a/src/corelib/tools/qtimezoneprivate.cpp +++ b/src/corelib/tools/qtimezoneprivate.cpp @@ -619,6 +619,15 @@ QTimeZonePrivate *QUtcTimeZonePrivate::clone() return new QUtcTimeZonePrivate(*this); } +QTimeZonePrivate::Data QUtcTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const +{ + Data d = invalidData(); + d.abbreviation = m_abbreviation; + d.atMSecsSinceEpoch = forMSecsSinceEpoch; + d.offsetFromUtc = m_offsetFromUtc; + return d; +} + void QUtcTimeZonePrivate::init(const QByteArray &zoneId) { m_id = zoneId; diff --git a/src/corelib/tools/qtimezoneprivate_p.h b/src/corelib/tools/qtimezoneprivate_p.h index 5ba42de560..f1d934abf3 100644 --- a/src/corelib/tools/qtimezoneprivate_p.h +++ b/src/corelib/tools/qtimezoneprivate_p.h @@ -181,6 +181,8 @@ public: QTimeZonePrivate *clone(); + Data data(qint64 forMSecsSinceEpoch) const Q_DECL_OVERRIDE; + QLocale::Country country() const Q_DECL_OVERRIDE; QString comment() const Q_DECL_OVERRIDE; diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index ae812bef0e..75511b5bb7 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -2804,10 +2804,12 @@ void tst_QDateTime::timeZones() const QCOMPARE(invalidDateTime.time(), QTime(0, 0, 0)); QTimeZone nzTz = QTimeZone("Pacific/Auckland"); + QTimeZone nzTzOffset = QTimeZone(12 * 3600); // During Standard Time NZ is +12:00 QDateTime utcStd(QDate(2012, 6, 1), QTime(0, 0, 0), Qt::UTC); QDateTime nzStd(QDate(2012, 6, 1), QTime(12, 0, 0), nzTz); + QDateTime nzStdOffset(QDate(2012, 6, 1), QTime(12, 0, 0), nzTzOffset); QCOMPARE(nzStd.isValid(), true); QCOMPARE(nzStd.timeSpec(), Qt::TimeZone); @@ -2819,6 +2821,16 @@ void tst_QDateTime::timeZones() const QCOMPARE(nzStd.isDaylightTime(), false); QCOMPARE(nzStd.toMSecsSinceEpoch(), utcStd.toMSecsSinceEpoch()); + QCOMPARE(nzStdOffset.isValid(), true); + QCOMPARE(nzStdOffset.timeSpec(), Qt::TimeZone); + QCOMPARE(nzStdOffset.date(), QDate(2012, 6, 1)); + QCOMPARE(nzStdOffset.time(), QTime(12, 0, 0)); + QVERIFY(nzStdOffset.timeZone() == nzTzOffset); + QCOMPARE(nzStdOffset.timeZone().id(), QByteArray("UTC+12:00")); + QCOMPARE(nzStdOffset.offsetFromUtc(), 43200); + QCOMPARE(nzStdOffset.isDaylightTime(), false); + QCOMPARE(nzStdOffset.toMSecsSinceEpoch(), utcStd.toMSecsSinceEpoch()); + // During Daylight Time NZ is +13:00 QDateTime utcDst(QDate(2012, 1, 1), QTime(0, 0, 0), Qt::UTC); QDateTime nzDst(QDate(2012, 1, 1), QTime(13, 0, 0), nzTz);