Fix end-of-parse fixup of two-digit year in QDateTimeParser

When a date string contains day of the week, day of the month, month
and two-digit year, it was still possible to get a conflicting result
in the default century instead of a consistent result in the next (in
fact present) century.

The actual logic needed to get the right year has to take into account
all date fields. This is all worked out already in actualDate(), so
delegate to it instead of trying to make do with just the year info.

Pick-to: 6.7 6.6 6.5
Fixes: QTBUG-123579
Change-Id: Id057128d8a0af9f3a7708d0ee173f854bb1a2a8e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>
bb10
Edward Welbourne 2024-03-21 17:41:26 +01:00
parent b87db4296c
commit 3e426182e2
2 changed files with 7 additions and 2 deletions

View File

@ -1360,13 +1360,15 @@ QDateTimeParser::scanString(const QDateTime &defaultValue, bool fixup) const
if (parserType != QMetaType::QTime) {
if (year % 100 != year2digits && (isSet & YearSection2Digits)) {
const QDate date = actualDate(isSet, calendar, defaultCenturyStart,
year, year2digits, month, day, dayofweek);
if (!(isSet & YearSection)) {
year = yearInCenturyFrom(year2digits, defaultCenturyStart);
year = date.year();
} else {
conflicts = true;
const SectionNode &sn = sectionNode(currentSectionIndex);
if (sn.type == YearSection2Digits)
year = yearInCenturyFrom(year2digits, defaultCenturyStart);
year = date.year();
}
}

View File

@ -3117,6 +3117,9 @@ void tst_QDateTime::fromStringStringFormat_data()
<< u"Thu January 2004"_s << u"ddd MMMM yyyy"_s << 1900
<< QDate(2004, 1, 1).startOfDay();
}
QTest::newRow("yy=24/Mar/20") // QTBUG-123579
<< u"Wed, 20 Mar 24 16:17:00"_s << u"ddd, dd MMM yy HH:mm:ss"_s << 1900
<< QDateTime(QDate(2024, 3, 20), QTime(16, 17));
QTest::newRow("secs-conflict") << u"1020"_s << u"sss"_s << 1900 << QDateTime();
QTest::newRow("secs-split-conflict")
<< u"10hello20"_s << u"ss'hello'ss"_s << 1900 << QDateTime();