From 4fa8dfee5dd31433d22fdb449c1783e256931c8f Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 15 Jun 2018 11:35:00 +0200 Subject: [PATCH] Leave m_id clear if the JNI didn't give us a time-zone QTimeZonePrivate::isValid() just checks m_id is non-empty; so we have to leave m_id clear if we don't get a valid time-zone back when we ask the JNI for one. Unfortunately, JNI gives us a "valid" default zone if it doesn't recognize the given name; so check the known names of this zone (or of zones with its offset); if the given ianaId isn't one of them, assume this is a bogus zone. Task-number: QTBUG-68842 Change-Id: I6245db18c59c4261ed5fcd4d948dd773365ce61d Reviewed-by: Thiago Macieira --- .../tools/qtimezoneprivate_android.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qtimezoneprivate_android.cpp b/src/corelib/tools/qtimezoneprivate_android.cpp index c3f8c3e0d9..b60093617f 100644 --- a/src/corelib/tools/qtimezoneprivate_android.cpp +++ b/src/corelib/tools/qtimezoneprivate_android.cpp @@ -82,7 +82,24 @@ void QAndroidTimeZonePrivate::init(const QByteArray &ianaId) QJNIObjectPrivate jo_ianaId = QJNIObjectPrivate::fromString( QString::fromUtf8(ianaId) ); androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod( "java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;", static_cast(jo_ianaId.object()) ); - if (ianaId.isEmpty()) + // Painfully, JNI gives us back a default zone object if it doesn't + // recognize the name; so check for whether ianaId is a recognized name of + // the zone object we got and ignore the zone if not. + bool found = false; + // Try checking ianaId against getID(), getDisplayName(): + QJNIObjectPrivate jname = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;"); + found = (jname.toString().toUtf8() == ianaId); + for (int style = 1; !found && style-- > 0;) { + for (int dst = 1; !found && dst-- > 0;) { + jname = androidTimeZone.callObjectMethod("getDisplayName", "(ZI;)Ljava/lang/String;", + bool(dst), style); + found = (jname.toString().toUtf8() == ianaId); + } + } + + if (!found) + m_id.clear(); + else if (ianaId.isEmpty()) m_id = systemTimeZoneId(); else m_id = ianaId;