Fix QDateTimeParser's handling of 't' format to match serialization

This amends commit 68f19fb630dc02463c2d61fc511de7407687795e to only
consume one 't' from the format string, to match qlocale.cpp's
serialization of time-zone specifiers, which only consumes one, so
will repeat the time-zone specifier as many times as unquoted t
appears in the format. It's hard to imagine why anyone would want this
behavior, but it's what our serialization has always done and parsing
should match serialization.

Add test-cases for double time-zone specifier.
Delete a lie in the process.

Task-number: QTBUG-95966
Change-Id: I574896040a74085dee89a4fefd8384be44ad827b
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2021-08-20 14:02:42 +02:00
parent a5d501000e
commit 7943480f0f
2 changed files with 10 additions and 2 deletions

View File

@ -600,7 +600,10 @@ bool QDateTimeParser::parseFormat(QStringView newFormat)
break;
case 't':
if (parserType == QMetaType::QDateTime) {
const SectionNode sn = { TimeZoneSection, i - add, countRepeat(newFormat, i, 4), 0 };
// TODO (in qlocale.cpp's serialization, too) QTBUG-95966:
// decide what different lengths of 't' format should do,
// instead of repetition !
const SectionNode sn = { TimeZoneSection, i - add, 1, 0 };
newSectionNodes.append(sn);
appendSeparator(&newSeparators, newFormat, index, i - index, lastQuote);
i += sn.count - 1;

View File

@ -212,7 +212,6 @@ tst_QDateTime::tst_QDateTime()
&& QDate(1990, 7, 1).startOfDay().toSecsSinceEpoch() == 646783200
&& QDate(1990, 1, 1).startOfDay().toSecsSinceEpoch() == 631148400
&& QDate(1979, 1, 1).startOfDay().toSecsSinceEpoch() == 283993200
// .toSecsSinceEpoch() returns -1 for everything before this:
&& QDateTime(QDate(1970, 1, 1), QTime(1, 0)).toSecsSinceEpoch() == 0);
// Use .toMSecsSinceEpoch() if you really need to test anything earlier.
@ -1084,6 +1083,8 @@ void tst_QDateTime::toString_strformat()
QCOMPARE(testDate.toString("yyyy-MM-dd"), QString("2013-01-01"));
QCOMPARE(testTime.toString("hh:mm:ss"), QString("01:02:03"));
QCOMPARE(testDateTime.toString("yyyy-MM-dd hh:mm:ss t"), QString("2013-01-01 01:02:03 UTC"));
// TODO QTBUG-95966: find better ways to use repeated 't'
QCOMPARE(testDateTime.toString("yyyy-MM-dd hh:mm:ss tt"), QString("2013-01-01 01:02:03 UTCUTC"));
}
#endif // datestring
@ -2908,6 +2909,10 @@ void tst_QDateTime::fromStringStringFormat_localTimeZone_data()
QTest::newRow("local-timezone-with-offset:Etc/GMT+3") << QByteArrayLiteral("GMT")
<< QString("2008-10-13 Etc/GMT+3 11.50") << QString("yyyy-MM-dd t hh.mm")
<< QDateTime(QDate(2008, 10, 13), QTime(11, 50), etcGmtWithOffset);
// TODO QTBUG-95966: find better ways to use repeated 't'
QTest::newRow("double-timezone-with-offset:Etc/GMT+3") << QByteArrayLiteral("GMT")
<< QString("2008-10-13 Etc/GMT+3Etc/GMT+3 11.50") << QString("yyyy-MM-dd tt hh.mm")
<< QDateTime(QDate(2008, 10, 13), QTime(11, 50), etcGmtWithOffset);
}
QTimeZone gmtWithOffset("GMT-2");
if (gmtWithOffset.isValid()) {