From 4756062828c30e4b7ed63f8dae4239799333b7d3 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 25 Oct 2023 18:16:14 +0200 Subject: [PATCH] Have QDTP match longest form it can for local zone name Previously, three candidates were tried (there shall soon be more) and the length of the first to match was returned. This could go wrong if tzname[] = { "GMT", "GMT+1" } and our text starts with the latter; we would have only matched it to the former. Change-Id: I8e7b9e6a382bdd527bb98be7c86ea67b11cdc13a Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetimeparser.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index 59ee639644..fb34205012 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -1180,17 +1180,20 @@ static QTime actualTime(QDateTimeParser::Sections known, */ static int startsWithLocalTimeZone(QStringView name, const QDateTime &when) { + // Pick longest match that we might get. + qsizetype longest = 0; // On MS-Win, at least when system zone is UTC, the tzname[]s may be empty. for (int i = 0; i < 2; ++i) { const QString zone(qTzName(i)); - if (!zone.isEmpty() && name.startsWith(zone)) - return zone.size(); + if (zone.size() > longest && name.startsWith(zone)) + longest = zone.size(); } // Mimic what QLocale::toString() would have used, to ensure round-trips work: const QString local = QDateTime(when.date(), when.time()).timeZoneAbbreviation(); - if (name.startsWith(local)) - return local.size(); - return 0; + if (local.size() > longest && name.startsWith(local)) + longest = local.size(); + Q_ASSERT(longest <= INT_MAX); // Timezone names are not that long. + return int(longest); } /*!