From 91fedd50f062b1f67b54344e31d540a05b15c922 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 24 Mar 2023 13:50:56 +0100 Subject: [PATCH] tst_QDateTime constructor: adapt the year check to vary year MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We check three sample dates, in different years, so don't compare their year() to 1970, but to their respective actual years. In the process, package the arrays iterated for these checks into a constexpr struct array and reverse them so that, instead of reverse-iterating and indexing, we can use a ranged-for loop. Change-Id: I214685346c637875a4ea31125c324851eb4308db Reviewed-by: Øystein Heskestad Reviewed-by: Ievgenii Meshcheriakov Reviewed-by: Konrad Kujawa Reviewed-by: Thiago Macieira --- .../corelib/time/qdatetime/tst_qdatetime.cpp | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index dc3d3e0760..7567f6b545 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -197,7 +197,7 @@ tst_QDateTime::tst_QDateTime() might not be properly handled by our work-arounds for the MS backend and 32-bit time_t; so don't probe them here. */ - const uint day = 24 * 3600; // in seconds + constexpr uint day = 24 * 3600; // in seconds zoneIsCET = (QDateTime(QDate(2038, 1, 19), QTime(4, 14, 7)).toSecsSinceEpoch() == 0x7fffffff // Entries a year apart robustly differ by multiples of day. && QDate(2015, 7, 1).startOfDay().toSecsSinceEpoch() == 1435701600 @@ -235,15 +235,17 @@ tst_QDateTime::tst_QDateTime() our TZ data, so we can't helpfully be exhaustive. Instead, scan a sample of years' starts and middles. */ - const int sampled = 3; - // UTC starts of months in 2004, 2038 and 1970: - qint64 jans[sampled] = { 12418 * day, 24837 * day, 0 }; - qint64 juls[sampled] = { 12600 * day, 25018 * day, 181 * day }; + // UTC starts of January and July in the given years: + constexpr struct { int year; qint64 jan; qint64 jul; } dates[] = { + { 1970, 0, 181 * day }, + { 2038, 24837 * day, 25018 * day }, + { 2004, 12418 * day, 12600 * day }, + }; localTimeType = LocalTimeIsUtc; - for (int i = sampled; i-- > 0; ) { - QDateTime jan = QDateTime::fromSecsSinceEpoch(jans[i]); - QDateTime jul = QDateTime::fromSecsSinceEpoch(juls[i]); - if (jan.date().year() < 1970 || jul.date().month() < 7) { + for (const auto &date : dates) { + QDateTime jan = QDateTime::fromSecsSinceEpoch(date.jan); + QDateTime jul = QDateTime::fromSecsSinceEpoch(date.jul); + if (jan.date().year() < date.year || jul.date().month() < 7) { localTimeType = LocalTimeBehindUtc; break; } else if (jan.time().hour() > 0 || jul.time().hour() > 0