summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2023-04-27 14:39:50 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2023-05-11 13:46:42 +0200
commit9f1252da28033fdf46d57112905f2770970ca964 (patch)
tree5e892d8c064adf447e9a8d843c9eddf66645e12b /tests
parentcded6afa3590b7c83664ac6f75d817bfc9b85287 (diff)
downloadqtbase-9f1252da28033fdf46d57112905f2770970ca964.tar.gz
Fix QTimeZone::offsetData() for the case without transitions
A zone without transitions, such as any UTC-based one, would previously return invalid data for the offset data at a given time. The method was documented to be "the equivalent of calling offsetFromUtc(), abbreviation(), etc" but these methods do return sensible data for a zone with no transitions. Furthermore, the backend data() method on which it depends is implemented by all backends, including the UTC one, with no transitions. Fix offsetData() to also return data when no transitions are available. Improve docs. Adapt the checkOffset() test to test offsetData() as well as the various functions to get parts of it. In the process, change that test to use a QTimeZone row instead of its name as a QByteArray, so that we can also have rows for lightweight time representations. Change-Id: I241ecf02a26a228cca972bca5e2db687fe41feb4 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp30
1 files changed, 22 insertions, 8 deletions
diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp
index ea52033723..8b9f5060da 100644
--- a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp
+++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp
@@ -777,17 +777,29 @@ void tst_QTimeZone::transitionEachZone()
void tst_QTimeZone::checkOffset_data()
{
- QTest::addColumn<QByteArray>("zoneName");
+ QTest::addColumn<QTimeZone>("zone");
QTest::addColumn<QDateTime>("when");
QTest::addColumn<int>("netOffset");
QTest::addColumn<int>("stdOffset");
QTest::addColumn<int>("dstOffset");
+ const QTimeZone UTC = QTimeZone::UTC;
+ QTest::addRow("UTC")
+ << UTC << QDate(1970, 1, 1).startOfDay(UTC) << 0 << 0 << 0;
+ const auto east = QTimeZone::fromSecondsAheadOfUtc(28'800); // 8 hours
+ QTest::addRow("UTC+8")
+ << east << QDate(2000, 2, 29).startOfDay(east) << 28'800 << 28'800 << 0;
+ const auto west = QTimeZone::fromDurationAheadOfUtc(std::chrono::hours{-8});
+ QTest::addRow("UTC-8")
+ << west << QDate(2100, 2, 28).startOfDay(west) << -28'800 << -28'800 << 0;
+
struct {
const char *zone, *nick;
int year, month, day, hour, min, sec;
int std, dst;
} table[] = {
+ // Exercise the UTC-backend:
+ { "UTC", "epoch", 1970, 1, 1, 0, 0, 0, 0, 0 },
// Zone with no transitions (QTBUG-74614, QTBUG-74666, when TZ backend uses minimal data)
{ "Etc/UTC", "epoch", 1970, 1, 1, 0, 0, 0, 0, 0 },
{ "Etc/UTC", "pre_int32", 1901, 12, 13, 20, 45, 51, 0, 0 },
@@ -799,38 +811,40 @@ void tst_QTimeZone::checkOffset_data()
{ "Europe/Kiev", "summer", 2017, 10, 27, 12, 0, 0, 2 * 3600, 3600 },
{ "Europe/Kiev", "winter", 2017, 10, 29, 12, 0, 0, 2 * 3600, 0 }
};
- bool lacksRows = true;
for (const auto &entry : table) {
QTimeZone zone(entry.zone);
if (zone.isValid()) {
QTest::addRow("%s@%s", entry.zone, entry.nick)
- << QByteArray(entry.zone)
+ << zone
<< QDateTime(QDate(entry.year, entry.month, entry.day),
QTime(entry.hour, entry.min, entry.sec), zone)
<< entry.dst + entry.std << entry.std << entry.dst;
- lacksRows = false;
} else {
qWarning("Skipping %s@%s test as zone is invalid", entry.zone, entry.nick);
}
}
- if (lacksRows)
- QSKIP("No valid zone info found, skipping test");
}
void tst_QTimeZone::checkOffset()
{
- QFETCH(QByteArray, zoneName);
+ QFETCH(QTimeZone, zone);
QFETCH(QDateTime, when);
QFETCH(int, netOffset);
QFETCH(int, stdOffset);
QFETCH(int, dstOffset);
- QTimeZone zone(zoneName);
QVERIFY(zone.isValid()); // It was when _data() added the row !
QCOMPARE(zone.offsetFromUtc(when), netOffset);
QCOMPARE(zone.standardTimeOffset(when), stdOffset);
QCOMPARE(zone.daylightTimeOffset(when), dstOffset);
QCOMPARE(zone.isDaylightTime(when), dstOffset != 0);
+
+ // Also test offsetData(), which gets all this data in one go:
+ const auto data = zone.offsetData(when);
+ QCOMPARE(data.atUtc, when);
+ QCOMPARE(data.offsetFromUtc, netOffset);
+ QCOMPARE(data.standardTimeOffset, stdOffset);
+ QCOMPARE(data.daylightTimeOffset, dstOffset);
}
void tst_QTimeZone::availableTimeZoneIds()