summaryrefslogtreecommitdiffstats
path: root/src/corelib/time/qgregoriancalendar.cpp
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2019-08-08 19:40:32 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2019-08-30 10:52:42 +0200
commit20f69beae48d2efb1fdb5754218a8cdc990ba13e (patch)
tree0f79f023ee153875ee4028f609ec6f077ef71234 /src/corelib/time/qgregoriancalendar.cpp
parent108382e236dcdc09a822b89db35cc8bc4509273f (diff)
Optimize QDate by shortcutting some gregorian calendar methods
Add some static methods to QGregorianCalendar, some of which serve to implement its methods, that QDate can use to bypass vtables and exploit fixed truths of the Gregorian calendar in its default handling. Change-Id: Iec191cdf4d52945dbd5679e609180cb4fe59b5fd Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/time/qgregoriancalendar.cpp')
-rw-r--r--src/corelib/time/qgregoriancalendar.cpp39
1 files changed, 38 insertions, 1 deletions
diff --git a/src/corelib/time/qgregoriancalendar.cpp b/src/corelib/time/qgregoriancalendar.cpp
index d3db572aa7..447185d124 100644
--- a/src/corelib/time/qgregoriancalendar.cpp
+++ b/src/corelib/time/qgregoriancalendar.cpp
@@ -78,6 +78,11 @@ QCalendar::System QGregorianCalendar::calendarSystem() const
bool QGregorianCalendar::isLeapYear(int year) const
{
+ return leapTest(year);
+}
+
+bool QGregorianCalendar::leapTest(int year)
+{
if (year == QCalendar::Unspecified)
return false;
@@ -88,10 +93,37 @@ bool QGregorianCalendar::isLeapYear(int year) const
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
+// Duplicating code from QRomanCalendar, but inlining isLeapYear() as leapTest():
+int QGregorianCalendar::monthLength(int month, int year)
+{
+ if (month < 1 || month > 12)
+ return 0;
+
+ if (month == 2)
+ return leapTest(year) ? 29 : 28;
+
+ return 30 | ((month & 1) ^ (month >> 3));
+}
+
+bool QGregorianCalendar::validParts(int year, int month, int day)
+{
+ return year && 0 < day && day <= monthLength(month, year);
+}
+
+int QGregorianCalendar::weekDayOfJulian(qint64 jd)
+{
+ return qMod(jd, 7) + 1;
+}
+
bool QGregorianCalendar::dateToJulianDay(int year, int month, int day, qint64 *jd) const
{
+ return julianFromParts(year, month, day, jd);
+}
+
+bool QGregorianCalendar::julianFromParts(int year, int month, int day, qint64 *jd)
+{
Q_ASSERT(jd);
- if (!isDateValid(year, month, day))
+ if (!validParts(year, month, day))
return false;
if (year < 0)
@@ -112,6 +144,11 @@ bool QGregorianCalendar::dateToJulianDay(int year, int month, int day, qint64 *j
QCalendar::YearMonthDay QGregorianCalendar::julianDayToDate(qint64 jd) const
{
+ return partsFromJulian(jd);
+}
+
+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