8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string
authorrpatil
Thu, 11 Jul 2019 03:28:43 +0530
changeset 57478 5d7cb195ea1c
parent 55761 afe8584ac8d9
child 57479 d999a1a11485
8154520: java.time: appendLocalizedOffset() should return the localized "GMT" string Summary: Changes to produce/consume the locale-specific equivalent corresponding to the english string "GMT". Reviewed-by: naoto, rriggs Contributed-by: thejasvi.v.voniadka@oracle.com
src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java
test/jdk/java/time/tck/java/time/format/TCKOffsetPrinterParser.java
test/jdk/java/time/test/java/time/format/TestLocalizedOffsetPrinterParser.java
--- a/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java	Mon Jul 22 16:10:25 2019 -0700
+++ b/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java	Thu Jul 11 03:28:43 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -85,7 +85,6 @@
 import java.time.ZoneId;
 import java.time.ZoneOffset;
 import java.time.chrono.ChronoLocalDate;
-import java.time.chrono.ChronoLocalDateTime;
 import java.time.chrono.Chronology;
 import java.time.chrono.Era;
 import java.time.chrono.IsoChronology;
@@ -122,7 +121,6 @@
 import sun.text.spi.JavaTimeDateTimePatternProvider;
 import sun.util.locale.provider.CalendarDataUtility;
 import sun.util.locale.provider.LocaleProviderAdapter;
-import sun.util.locale.provider.LocaleResources;
 import sun.util.locale.provider.TimeZoneNameUtility;
 
 /**
@@ -3871,7 +3869,11 @@
             if (offsetSecs == null) {
                 return false;
             }
-            String gmtText = "GMT";  // TODO: get localized version of 'GMT'
+            String key = "timezone.gmtZeroFormat";
+            String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale());
+            if (gmtText == null) {
+                gmtText = "GMT";  // Default to "GMT"
+            }
             buf.append(gmtText);
             int totalSecs = Math.toIntExact(offsetSecs);
             if (totalSecs != 0) {
@@ -3917,7 +3919,11 @@
         public int parse(DateTimeParseContext context, CharSequence text, int position) {
             int pos = position;
             int end = text.length();
-            String gmtText = "GMT";  // TODO: get localized version of 'GMT'
+            String key = "timezone.gmtZeroFormat";
+            String gmtText = DateTimeTextProvider.getLocalizedResource(key, context.getLocale());
+            if (gmtText == null) {
+                gmtText = "GMT";  // Default to "GMT"
+            }
             if (!context.subSequenceEquals(text, pos, gmtText, 0, gmtText.length())) {
                     return ~position;
                 }
--- a/test/jdk/java/time/tck/java/time/format/TCKOffsetPrinterParser.java	Mon Jul 22 16:10:25 2019 -0700
+++ b/test/jdk/java/time/tck/java/time/format/TCKOffsetPrinterParser.java	Thu Jul 11 03:28:43 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -69,6 +69,7 @@
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
 import java.time.format.TextStyle;
+import java.util.Locale;
 
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.DataProvider;
@@ -557,20 +558,20 @@
         ZonedDateTime zdt = ldt.atZone(offset);
 
         DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style)
-                                                            .toFormatter();
+                                                            .toFormatter(Locale.US);
         assertEquals(f.format(odt), expected);
         assertEquals(f.format(zdt), expected);
         assertEquals(f.parse(expected, ZoneOffset::from), offset);
 
         if (style == TextStyle.FULL) {
             f = new DateTimeFormatterBuilder().appendPattern("ZZZZ")
-                                              .toFormatter();
+                                              .toFormatter(Locale.US);
             assertEquals(f.format(odt), expected);
             assertEquals(f.format(zdt), expected);
             assertEquals(f.parse(expected, ZoneOffset::from), offset);
 
             f = new DateTimeFormatterBuilder().appendPattern("OOOO")
-                                              .toFormatter();
+                                              .toFormatter(Locale.US);
             assertEquals(f.format(odt), expected);
             assertEquals(f.format(zdt), expected);
             assertEquals(f.parse(expected, ZoneOffset::from), offset);
@@ -578,7 +579,7 @@
 
         if (style == TextStyle.SHORT) {
             f = new DateTimeFormatterBuilder().appendPattern("O")
-                                              .toFormatter();
+                                              .toFormatter(Locale.US);
             assertEquals(f.format(odt), expected);
             assertEquals(f.format(zdt), expected);
             assertEquals(f.parse(expected, ZoneOffset::from), offset);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/time/test/java/time/format/TestLocalizedOffsetPrinterParser.java	Thu Jul 11 03:28:43 2019 +0530
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8154520
+ * @summary This test verifies that the localized text for "GMT" from CLDR is
+ * applied/recognized during printing/parsing timestamps. For example, the
+ * localized text for "GMT" on some particular locale may be "UTC", and the
+ * resulting formatted string should have UTC+<offset> (instead of GMT+<offset>).
+ * Since the test relies on CLDR data, the "expected" text in the test data may
+ * require to be modified in accordance with changes to CLDR, if any.
+ * @modules jdk.localedata
+ */
+
+package test.java.time.format;
+
+import static org.testng.Assert.assertEquals;
+
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.TextStyle;
+import java.util.Locale;
+import java.util.Objects;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/**
+ * Test DateTimeFormatterBuilder.appendOffset().
+ */
+@Test
+public class TestLocalizedOffsetPrinterParser {
+
+    private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40);
+
+    private static final Locale LOCALE_GA = new Locale("ga");
+
+    @DataProvider(name="print_localized_custom_locale")
+    Object[][] data_print_localized_custom_locale() {
+        return new Object[][] {
+                {TextStyle.FULL, DT_2012_06_30_12_30_40, ZoneOffset.UTC, LOCALE_GA, "MAG"},
+                {TextStyle.SHORT, DT_2012_06_30_12_30_40, ZoneOffset.ofHours(1), LOCALE_GA, "MAG+1"},
+                {TextStyle.FULL, DT_2012_06_30_12_30_40, ZoneOffset.ofHours(-1), LOCALE_GA, "MAG-01:00"}
+        };
+    }
+
+    @Test(dataProvider="print_localized_custom_locale")
+    public void test_print_localized_custom_locale(TextStyle style, LocalDateTime ldt, ZoneOffset offset, Locale locale, String expected) {
+
+        Objects.requireNonNull(locale, "Locale must not be null");
+
+        OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
+        ZonedDateTime zdt = ldt.atZone(offset);
+
+        DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style).toFormatter(locale);
+        assertEquals(f.format(odt), expected);
+        assertEquals(f.format(zdt), expected);
+        assertEquals(f.parse(expected, ZoneOffset::from), offset);
+
+        if (style == TextStyle.FULL) {
+            f = new DateTimeFormatterBuilder().appendPattern("ZZZZ").toFormatter(locale);
+            assertEquals(f.format(odt), expected);
+            assertEquals(f.format(zdt), expected);
+            assertEquals(f.parse(expected, ZoneOffset::from), offset);
+
+            f = new DateTimeFormatterBuilder().appendPattern("OOOO").toFormatter(locale);
+            assertEquals(f.format(odt), expected);
+            assertEquals(f.format(zdt), expected);
+            assertEquals(f.parse(expected, ZoneOffset::from), offset);
+        }
+
+        if (style == TextStyle.SHORT) {
+            f = new DateTimeFormatterBuilder().appendPattern("O").toFormatter(locale);
+            assertEquals(f.format(odt), expected);
+            assertEquals(f.format(zdt), expected);
+            assertEquals(f.parse(expected, ZoneOffset::from), offset);
+        }
+
+    }
+
+}