From 285596ee199ae1767fc48a0d0c2d12b178c0234d Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 9 Nov 2017 21:13:43 +0100 Subject: 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 --- src/corelib/tools/qdatetimeparser.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/corelib/tools/qdatetimeparser.cpp') diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp index dd277f7753..4b3777407c 100644 --- a/src/corelib/tools/qdatetimeparser.cpp +++ b/src/corelib/tools/qdatetimeparser.cpp @@ -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); -- cgit v1.2.3