From fa9244700e016403b162932211023c65f4bb0d6b Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 26 Mar 2023 20:40:35 +0200 Subject: [PATCH] QDate: use more constexpr vars instead of plain numbers Change-Id: I95580c199f868d632324f7f1fcbd56fa4dc85958 Reviewed-by: Edward Welbourne --- src/corelib/time/qdatetime.cpp | 3 +++ src/corelib/time/qdatetime_p.h | 3 +++ tests/auto/corelib/time/qdate/tst_qdate.cpp | 18 ++++++++++-------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index d69101a0d1..b484196350 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -435,6 +435,9 @@ static int fromOffsetString(QStringView offsetString, bool *valid) noexcept QDate::QDate(int y, int m, int d) { + static_assert(QDate::maxJd() == JulianDayMax); + static_assert(QDate::minJd() == JulianDayMin); + if (!QGregorianCalendar::julianFromParts(y, m, d, &jd)) jd = nullJd(); } diff --git a/src/corelib/time/qdatetime_p.h b/src/corelib/time/qdatetime_p.h index ab91ec3333..bd4e1e33d0 100644 --- a/src/corelib/time/qdatetime_p.h +++ b/src/corelib/time/qdatetime_p.h @@ -122,6 +122,9 @@ constexpr qint64 MSECS_PER_HOUR = SECS_PER_HOUR * MSECS_PER_SEC; constexpr qint64 MSECS_PER_DAY = SECS_PER_DAY * MSECS_PER_SEC; constexpr qint64 JULIAN_DAY_FOR_EPOCH = 2440588; // result of QDate(1970, 1, 1).toJulianDay() + +constexpr qint64 JulianDayMax = Q_INT64_C( 784354017364); +constexpr qint64 JulianDayMin = Q_INT64_C(-784350574879); } } diff --git a/tests/auto/corelib/time/qdate/tst_qdate.cpp b/tests/auto/corelib/time/qdate/tst_qdate.cpp index 302f58d00a..32edc20c18 100644 --- a/tests/auto/corelib/time/qdate/tst_qdate.cpp +++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp @@ -12,6 +12,8 @@ #include // for the icu feature test #include +using namespace QtPrivate::DateTimeConstants; + class tst_QDate : public QObject { Q_OBJECT @@ -98,8 +100,8 @@ void tst_QDate::isNull_data() QTest::addColumn("jd"); QTest::addColumn("null"); - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); + qint64 minJd = JulianDayMin; + qint64 maxJd = JulianDayMax; QTest::newRow("qint64 min") << std::numeric_limits::min() << true; QTest::newRow("minJd - 1") << minJd - 1 << true; @@ -693,8 +695,8 @@ void tst_QDate::julianDaysLimits() { qint64 min = std::numeric_limits::min(); qint64 max = std::numeric_limits::max(); - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); + qint64 minJd = JulianDayMin; + qint64 maxJd = JulianDayMax; QDate maxDate = QDate::fromJulianDay(maxJd); QDate minDate = QDate::fromJulianDay(minJd); @@ -917,8 +919,8 @@ void tst_QDate::addYears_data() void tst_QDate::daysTo() { - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); + qint64 minJd = JulianDayMin; + qint64 maxJd = JulianDayMax; QDate dt1(2000, 1, 1); QDate dt2(2000, 1, 5); @@ -1682,8 +1684,8 @@ void tst_QDate::roundtrip() const loopDate = loopDate.addDays(1); } - qint64 minJd = Q_INT64_C(-784350574879); - qint64 maxJd = Q_INT64_C( 784354017364); + qint64 minJd = JulianDayMin; + qint64 maxJd = JulianDayMax; // Test Gregorian round trip at top end of conversion range loopDate = QDate::fromJulianDay(maxJd);