Adjust unknown year to leap if it helps get a valid date

Where QDateTimeParser doesn't know the year and the day of month is
out of range for the known month and uncertain year, try adjusting to
a leap year to see if that solves the problem.

Change-Id: Ic1c126fba9f0901447503fda46c9830834c30038
Reviewed-by: Magdalena Stojek <magdalena.stojek@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2024-12-11 16:53:19 +01:00
parent 72519aeb23
commit 98ddef287c

View File

@ -1037,6 +1037,18 @@ static QDate actualDate(QDateTimeParser::Sections known, QCalendar calendar, int
month = 12;
known &= ~QDateTimeParser::MonthSection;
}
if (!actual.isValid() && !known.testAnyFlag(QDateTimeParser::YearSectionMask)
&& known.testFlags(QDateTimeParser::DaySection | QDateTimeParser::MonthSection)
&& !calendar.isLeapYear(year) && day > calendar.daysInMonth(month, year)) {
// See if a leap year works better:
int leap = year + 1, stop = year + 47;
// (Sweden's 1700 plan (abandoned part way through) for Julian-Gregorian
// transition implied no leap year after 1697 until 1744.)
while (!calendar.isLeapYear(leap) && leap < stop)
++leap;
if (day <= calendar.daysInMonth(month, leap))
year = leap;
}
QDate first(year, month, 1, calendar);
int last = known & QDateTimeParser::MonthSection