summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2023-01-23 20:05:57 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2023-01-31 17:35:13 +0100
commit0e653246b163adb58d0fd955f45aaefc8a506443 (patch)
tree86bbe132b7c982242d459fccb04fe92cfe08f632
parent5a91b734f2ac42df694c8052eaaf39c03c7cfab0 (diff)
Shift the base date for the Gregorian calendar's calculations
Internally it was using the start of March 4801 BCE and then adjusting the year by 4800; instead, use the start of March 1 BCE and save those adjustments. This incidentally aligns better with the Milankovic calendar's calculations. Change-Id: Ic64b4e1d4f65bbf1973a77f830e68f05f7949f4d Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/time/qgregoriancalendar.cpp12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/corelib/time/qgregoriancalendar.cpp b/src/corelib/time/qgregoriancalendar.cpp
index 846a1154466..904b23434cc 100644
--- a/src/corelib/time/qgregoriancalendar.cpp
+++ b/src/corelib/time/qgregoriancalendar.cpp
@@ -162,6 +162,10 @@ int QGregorianCalendar::yearSharingWeekDays(QDate date)
* 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).
+ *
+ * The source given uses 4801 BCE as base date; the following adjusts that by
+ * 4800 years to simplify part of the arithmetic (and match more closely what we
+ * do for Milankovic).
*/
bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *jd)
@@ -174,16 +178,16 @@ bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *j
++year;
int a = month < 3 ? 1 : 0;
- qint64 y = qint64(year) + 4800 - a;
+ qint64 y = qint64(year) - a;
int m = month + 12 * a - 3;
- *jd = day + qDiv<5>(153 * m + 2) - 32045
+ *jd = day + qDiv<5>(153 * m + 2) + 1721119
+ 365 * y + qDiv<4>(y) - qDiv<100>(y) + qDiv<400>(y);
return true;
}
QCalendar::YearMonthDay QGregorianCalendar::partsFromJulian(qint64 jd)
{
- qint64 a = jd + 32044;
+ qint64 a = jd - 1721120;
qint64 b = qDiv<146097>(4 * a + 3);
int c = a - qDiv<4>(146097 * b);
@@ -191,7 +195,7 @@ QCalendar::YearMonthDay QGregorianCalendar::partsFromJulian(qint64 jd)
int e = c - qDiv<4>(1461 * d);
int m = qDiv<153>(5 * e + 2);
- int y = 100 * b + d - 4800 + qDiv<10>(m);
+ int y = 100 * b + d + qDiv<10>(m);
// Adjust for no year 0
int year = y > 0 ? y : y - 1;