/* * 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+ (instead of GMT+). * 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 ZoneOffset OFFSET_UTC = ZoneOffset.UTC; private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45); private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45); private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1); private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23); private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45); private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45); @DataProvider(name="print_localized_custom_locale") Object[][] data_print_localized_custom_locale() { return new Object[][] { {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, new Locale("am"), "\u1302 \u12a4\u121d \u1272-1:23"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, new Locale("ar"), "\u063a\u0631\u064a\u0646\u062a\u0634-01:00"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, new Locale("bg"), "\u0413\u0440\u0438\u043d\u0443\u0438\u0447+01:23:45"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, Locale.CHINESE, "GMT-00:00:45"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, Locale.ENGLISH, "GMT+01:23:45"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, new Locale("eo"), "GMT-1:23:45"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P000045, Locale.FRANCE, "UTC+00:00:45"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, new Locale("ga"), "MAG-1"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, new Locale("km"), "\u1798\u17c9\u17c4\u1784\u200b\u179f\u1780\u179b"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, new Locale("ml"), "\u0d1c\u0d3f\u0d0e\u0d02\u0d1f\u0d3f"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, new Locale("mr"), "[GMT]+01:23:45"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, new Locale("se"), "UTC+1:23:45"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, new Locale("yo", "BJ"), "WAT-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); } } }