Special-case parsing of Zulu time-zone in date-times

When parsing a date-time's zone, a stray Z denotes UTC (a.k.a. Zulu
time), despite not being a valid name for the zone.  Clients parsing
such date strings had to treat the Z as a literal, rather than a
zone-ID, but then they got back a LocalTime instead of the UTC the
string actually described.  So teach QTimeZoneParser to handle this
special case and adapt an existing test (that used a time ending in Z,
but had to treat it as a local time) to check this works.

[ChangeLog][QtCore][QDateTime] When parsing a time-zone, "Z" is now
recognized as an alias for UTC.

Change-Id: Ib6aa2d8ea2dc6b2da526b39aec74dbc007f90fd8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2017-11-09 21:13:43 +01:00
parent 1456a7b78a
commit 285596ee19
2 changed files with 8 additions and 3 deletions

View File

@ -1155,7 +1155,8 @@ QDateTimeParser::scanString(const QDateTime &defaultValue,
// Synchronize with what findTimeZone() found:
QStringRef zoneName = input->midRef(pos, sect.used);
Q_ASSERT(!zoneName.isEmpty()); // sect.used > 0
const QByteArray latinZone(zoneName.toLatin1());
const QByteArray latinZone(zoneName == QLatin1String("Z")
? QByteArray("UTC") : zoneName.toLatin1());
timeZone = QTimeZone(latinZone);
tspec = timeZone.isValid()
? (QTimeZone::isTimeZoneIdAvailable(latinZone)
@ -1595,6 +1596,10 @@ QDateTimeParser::findTimeZone(QStringRef str, const QDateTime &when,
while (index > 0) {
str.truncate(index);
if (str == QLatin1String("Z")) {
offset = 0; // "Zulu" time - a.k.a. UTC
break;
}
QTimeZone zone(str.toLatin1());
if (zone.isValid()) {
offset = zone.offsetFromUtc(when);

View File

@ -2396,8 +2396,8 @@ void tst_QDateTime::fromStringStringFormat_data()
QTest::newRow("data14") << QString("32.01.2004") << QString("dd.MM.yyyy") << invalidDateTime();
QTest::newRow("data15") << QString("Thu January 2004") << QString("ddd MMMM yyyy") << QDateTime(QDate(2004, 1, 1), QTime());
QTest::newRow("data16") << QString("2005-06-28T07:57:30.001Z")
<< QString("yyyy-MM-ddThh:mm:ss.zZ")
<< QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 1));
<< QString("yyyy-MM-ddThh:mm:ss.zt")
<< QDateTime(QDate(2005, 06, 28), QTime(07, 57, 30, 1), Qt::UTC);
}
void tst_QDateTime::fromStringStringFormat()