From 23b69855c0d2ed747a73b15f72f1788804bdb477 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 21 Mar 2024 17:41:26 +0100 Subject: [PATCH] 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.6 6.5 Fixes: QTBUG-123579 Change-Id: Id057128d8a0af9f3a7708d0ee173f854bb1a2a8e Reviewed-by: Thiago Macieira Reviewed-by: Dennis Oberst (cherry picked from commit 3e426182e2e8122d96b208702faaf3177f3a3081) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/time/qdatetimeparser.cpp | 6 ++++-- tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index 7c6ec272fb9..d6ff29cf7f5 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -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(); } } diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index 6c0733686d7..f9c6afc795c 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -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();