From 5a91b734f2ac42df694c8052eaaf39c03c7cfab0 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 23 Jan 2023 19:57:01 +0100 Subject: [PATCH] Shuffle QGregorianCalendar's parts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before I embark on some cleanup in the calendrical calculations, move julianFromParts() to alongside partsFromJulian() - they're the static methods used to bypass virtuals internally - and pull out a duplicated comment from them to put before both. Move julianDayToDate() to sit with its fellow virtual shim, each calling one of the above, dateToJulianDay(). Change-Id: I581c08bae4c921c4a5cd48eebb66d46035d7d046 Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim --- src/corelib/time/qgregoriancalendar.cpp | 48 ++++++++++++------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/src/corelib/time/qgregoriancalendar.cpp b/src/corelib/time/qgregoriancalendar.cpp index db0bdb2e45..846a115446 100644 --- a/src/corelib/time/qgregoriancalendar.cpp +++ b/src/corelib/time/qgregoriancalendar.cpp @@ -100,26 +100,9 @@ bool QGregorianCalendar::dateToJulianDay(int year, int month, int day, qint64 *j return julianFromParts(year, month, day, jd); } -bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *jd) +QCalendar::YearMonthDay QGregorianCalendar::julianDayToDate(qint64 jd) const { - Q_ASSERT(jd); - if (!validParts(year, month, day)) - return false; - - if (year < 0) - ++year; - - /* - * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php - * This formula is correct for all julian days, when using mathematical integer - * division (round to negative infinity), not c++11 integer division (round to zero) - */ - int a = month < 3 ? 1 : 0; - qint64 y = qint64(year) + 4800 - a; - int m = month + 12 * a - 3; - *jd = day + qDiv<5>(153 * m + 2) - 32045 - + 365 * y + qDiv<4>(y) - qDiv<100>(y) + qDiv<400>(y); - return true; + return partsFromJulian(jd); } int QGregorianCalendar::yearStartWeekDay(int year) @@ -175,18 +158,31 @@ int QGregorianCalendar::yearSharingWeekDays(QDate date) return res; } -QCalendar::YearMonthDay QGregorianCalendar::julianDayToDate(qint64 jd) const +/* + * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php + * This formula is correct for all julian days, when using mathematical integer + * division (round to negative infinity), not c++11 integer division (round to zero). + */ + +bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *jd) { - return partsFromJulian(jd); + Q_ASSERT(jd); + if (!validParts(year, month, day)) + return false; + + if (year < 0) + ++year; + + int a = month < 3 ? 1 : 0; + qint64 y = qint64(year) + 4800 - a; + int m = month + 12 * a - 3; + *jd = day + qDiv<5>(153 * m + 2) - 32045 + + 365 * y + qDiv<4>(y) - qDiv<100>(y) + qDiv<400>(y); + return true; } QCalendar::YearMonthDay QGregorianCalendar::partsFromJulian(qint64 jd) { - /* - * Math from The Calendar FAQ at http://www.tondering.dk/claus/cal/julperiod.php - * This formula is correct for all julian days, when using mathematical integer - * division (round to negative infinity), not c++11 integer division (round to zero) - */ qint64 a = jd + 32044; qint64 b = qDiv<146097>(4 * a + 3); int c = a - qDiv<4>(146097 * b);