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>
bb10
Edward Welbourne 2023-01-23 19:57:01 +01:00
parent 1cc4ca3e2c
commit 5a91b734f2
1 changed files with 22 additions and 26 deletions

View File

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