Skip to content

Commit

Permalink
Adjust unknown year to leap if it helps get a valid date
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ediosyncratic committed Dec 13, 2024
1 parent 72519ae commit 98ddef2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/corelib/time/qdatetimeparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98ddef2

Please sign in to comment.