tst_QDateTime constructor: adapt the year check to vary year

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 <oystein.heskestad@qt.io>
Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>
Reviewed-by: Konrad Kujawa <konrad.kujawa@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2023-03-24 13:50:56 +01:00
parent 9aa87643d7
commit 91fedd50f0

View File

@ -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