Allow lower-case for the T and Z in ISO 8601 date format

Cite RFC 3339 as basis for allowing a space in place of the T, too.
The RFC mentions that ISO 8601 accepts t and z for T and Z, so test
for them case-insensitively. Add a test for this.

Change-Id: Iba700c8d74d485df154d27300aab7b1958e1ccef
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2019-12-03 11:17:31 +01:00
parent 653c1aab18
commit 6b5f848ebd
2 changed files with 8 additions and 4 deletions

View File

@ -5220,10 +5220,12 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
QStringRef isoString(&string);
isoString = isoString.mid(10); // trim "yyyy-MM-dd"
// Must be left with T and at least one digit for the hour:
// Must be left with T (or space) and at least one digit for the hour:
if (isoString.size() < 2
|| !(isoString.startsWith(QLatin1Char('T'))
// FIXME: QSql relies on QVariant::toDateTime() accepting a space here:
|| !(isoString.startsWith(QLatin1Char('T'), Qt::CaseInsensitive)
// RFC 3339 (section 5.6) allows a space here. (It actually
// allows any separator one considers more readable, merely
// giving space as an example - but let's not go wild !)
|| isoString.startsWith(QLatin1Char(' ')))) {
return QDateTime();
}
@ -5231,7 +5233,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format)
int offset = 0;
// Check end of string for Time Zone definition, either Z for UTC or [+-]HH:mm for Offset
if (isoString.endsWith(QLatin1Char('Z'))) {
if (isoString.endsWith(QLatin1Char('Z'), Qt::CaseInsensitive)) {
spec = Qt::UTC;
isoString.chop(1); // trim 'Z'
} else {

View File

@ -2237,6 +2237,8 @@ void tst_QDateTime::fromStringDateFormat_data()
<< Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9), Qt::UTC);
QTest::newRow("ISO zzz-3") << QString::fromLatin1("2014-12-15T12:37:09.745-3")
<< Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9, 745), Qt::UTC);
QTest::newRow("ISO lower-case") << QString::fromLatin1("2005-06-28T07:57:30.002z")
<< Qt::ISODate << QDateTime(QDate(2005, 6, 28), QTime(7, 57, 30, 2), Qt::UTC);
// No time specified - defaults to Qt::LocalTime.
QTest::newRow("ISO data3") << QString::fromLatin1("2002-10-01")
<< Qt::ISODate << QDateTime(QDate(2002, 10, 1), QTime(0, 0, 0, 0), Qt::LocalTime);