diff options
| author | Edward Welbourne <edward.welbourne@qt.io> | 2023-01-23 19:57:01 +0100 |
|---|---|---|
| committer | Edward Welbourne <edward.welbourne@qt.io> | 2023-01-31 17:35:13 +0100 |
| commit | 5a91b734f2ac42df694c8052eaaf39c03c7cfab0 (patch) | |
| tree | ae9892d39d34da5a673cd5da22c5dcd980afd2b8 /src | |
| parent | 1cc4ca3e2c91201a4b223d78daf2481f67e402fb (diff) | |
Shuffle QGregorianCalendar's parts
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 <thiago.macieira@intel.com>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/corelib/time/qgregoriancalendar.cpp | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/src/corelib/time/qgregoriancalendar.cpp b/src/corelib/time/qgregoriancalendar.cpp index db0bdb2e45f..846a1154466 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); |
