From 98ddef287cf01b8830132d828b61f5efc86f7918 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 11 Dec 2024 16:53:19 +0100 Subject: [PATCH] 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 Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetimeparser.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index 9af92941d6b..31e010de74d 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -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