Fix script selection in locales for TZ names.

Fix how we pass the locale ID to ICU4C. Previously we incorrectly passed
the result of Locale.toString() (which is a hybrid, legacy-based value)
instead of Locale.toLanguageTag() (which is well-specified by BCP 47 and
what ICU4C actually expects).

The only known case where this actually leads to an observable
difference is when the Locale includes an explicit script identifier.

For example the (BCP 47) locale id sr-Latn-RS would be represented by
Locale.toString as "sr_RS_#Latn". When parsing that string in ICU4C, the
last part will be ignored, as it's not well-formed.

Bug: 30527513
Tested: libcore.java.text.SimpleDateFormatTest
    libcore.java.util.TimeZoneTest libcore.icu.TimeZoneNamesTest

(cherry picked from commit 7d60ae13c1300a8bcfb3aa41c773a9eb2e92c310)

Change-Id: I71a2fc8cd9842e9a8b11bd7d9e15716b17bfe8cf
diff --git a/luni/src/main/java/libcore/icu/TimeZoneNames.java b/luni/src/main/java/libcore/icu/TimeZoneNames.java
index daa915e..917d9ce 100644
--- a/luni/src/main/java/libcore/icu/TimeZoneNames.java
+++ b/luni/src/main/java/libcore/icu/TimeZoneNames.java
@@ -68,7 +68,7 @@
             }
 
             long nativeStart = System.nanoTime();
-            fillZoneStrings(locale.toString(), result);
+            fillZoneStrings(locale.toLanguageTag(), result);
             long nativeEnd = System.nanoTime();
 
             internStrings(result);
diff --git a/luni/src/test/java/libcore/java/util/TimeZoneTest.java b/luni/src/test/java/libcore/java/util/TimeZoneTest.java
index 5a6fa7f..d0342e2 100644
--- a/luni/src/test/java/libcore/java/util/TimeZoneTest.java
+++ b/luni/src/test/java/libcore/java/util/TimeZoneTest.java
@@ -261,6 +261,31 @@
         assertEquals("", failures.toString());
     }
 
+    // http://b/30527513
+    public void testDisplayNamesWithScript() throws Exception {
+        Locale latinLocale = Locale.forLanguageTag("sr-Latn-RS");
+        Locale cyrillicLocale = Locale.forLanguageTag("sr-Cyrl-RS");
+        Locale noScriptLocale = Locale.forLanguageTag("sr-RS");
+        TimeZone tz = TimeZone.getTimeZone("Europe/London");
+
+        final String latinName = "Srednje vreme po Griniču";
+        final String cyrillicName = "Средње време по Гриничу";
+
+        // Check java.util.TimeZone
+        assertEquals(latinName, tz.getDisplayName(latinLocale));
+        assertEquals(cyrillicName, tz.getDisplayName(cyrillicLocale));
+        assertEquals(cyrillicName, tz.getDisplayName(noScriptLocale));
+
+        // Check ICU TimeZoneNames
+        // The one-argument getDisplayName() override uses LONG_GENERIC style which is different
+        // from what java.util.TimeZone uses. Force the LONG style to get equivalent results.
+        final int style = android.icu.util.TimeZone.LONG;
+        android.icu.util.TimeZone utz = android.icu.util.TimeZone.getTimeZone(tz.getID());
+        assertEquals(latinName, utz.getDisplayName(false, style, latinLocale));
+        assertEquals(cyrillicName, utz.getDisplayName(false, style, cyrillicLocale));
+        assertEquals(cyrillicName, utz.getDisplayName(false, style, noScriptLocale));
+    }
+
     // http://b/7955614
     public void testApia() throws Exception {
         TimeZone tz = TimeZone.getTimeZone("Pacific/Apia");