protected PrefixTree getTree(DateTimeParseContext context) { // prepare parse tree Set<String> regionIds = ZoneRulesProvider.getAvailableZoneIds(); final int regionIdsSize = regionIds.size(); Entry<Integer, PrefixTree> cached = context.isCaseSensitive() ? cachedPrefixTree : cachedPrefixTreeCI; if (cached == null || cached.getKey() != regionIdsSize) { synchronized (this) { cached = context.isCaseSensitive() ? cachedPrefixTree : cachedPrefixTreeCI; if (cached == null || cached.getKey() != regionIdsSize) { cached = new SimpleImmutableEntry<>(regionIdsSize, PrefixTree.newTree(regionIds, context)); if (context.isCaseSensitive()) { cachedPrefixTree = cached; } else { cachedPrefixTreeCI = cached; } } } } return cached.getValue(); }
/** * Obtains an instance of {@code ZoneId} from an identifier. * * @param zoneId the time-zone ID, not null * @param checkAvailable whether to check if the zone ID is available * @return the zone ID, not null * @throws DateTimeException if the ID format is invalid * @throws DateTimeException if checking availability and the ID cannot be found */ static ZoneRegion ofId(String zoneId, boolean checkAvailable) { Jdk8Methods.requireNonNull(zoneId, "zoneId"); if (zoneId.length() < 2 || PATTERN.matcher(zoneId).matches() == false) { throw new DateTimeException("Invalid ID for region-based ZoneId, invalid format: " + zoneId); } ZoneRules rules = null; try { // always attempt load for better behavior after deserialization rules = ZoneRulesProvider.getRules(zoneId, true); } catch (ZoneRulesException ex) { // special case as removed from data file if (zoneId.equals("GMT0")) { rules = ZoneOffset.UTC.getRules(); } else if (checkAvailable) { throw ex; } } return new ZoneRegion(zoneId, rules); }
/** * Obtains an instance of {@code ZoneId} from an identifier. * * @param zoneId the time-zone ID, not null * @param checkAvailable whether to check if the zone ID is available * @return the zone ID, not null * @throws DateTimeException if the ID format is invalid * @throws DateTimeException if checking availability and the ID cannot be found */ static ZoneRegion ofId(String zoneId, boolean checkAvailable) { Jdk7Methods.Objects_requireNonNull(zoneId, "zoneId"); if (zoneId.length() < 2 || zoneId.startsWith("UTC") || zoneId.startsWith("GMT") || (PATTERN.matcher(zoneId).matches() == false)) { throw new DateTimeException("ZoneId format is not a valid region format"); } ZoneRules rules = null; try { // always attempt load for better behavior after deserialization rules = ZoneRulesProvider.getRules(zoneId); } catch (ZoneRulesException ex) { if (checkAvailable) { throw ex; } } return new ZoneRegion(zoneId, rules); }
/** * Obtains an instance of {@code ZoneId} from an identifier. * * @param zoneId the time-zone ID, not null * @param checkAvailable whether to check if the zone ID is available * @return the zone ID, not null * @throws DateTimeException if the ID format is invalid * @throws ZoneRulesException if checking availability and the ID cannot be found */ static ZoneRegion ofId(String zoneId, boolean checkAvailable) { Objects.requireNonNull(zoneId, "zoneId"); checkName(zoneId); ZoneRules rules = null; try { // always attempt load for better behavior after deserialization rules = ZoneRulesProvider.getRules(zoneId, true); } catch (ZoneRulesException ex) { if (checkAvailable) { throw ex; } } return new ZoneRegion(zoneId, rules); }
@Test public void test_getAvailableGroupIds() { Set<String> zoneIds = ZoneRulesProvider.getAvailableZoneIds(); assertEquals(zoneIds.contains("Europe/London"), true); zoneIds.clear(); assertEquals(zoneIds.size(), 0); Set<String> zoneIds2 = ZoneRulesProvider.getAvailableZoneIds(); assertEquals(zoneIds2.contains("Europe/London"), true); }
@Test public void test_getRules_StringBoolean() { ZoneRules rules = ZoneRulesProvider.getRules("Europe/London", false); assertNotNull(rules); ZoneRules rules2 = ZoneRulesProvider.getRules("Europe/London", false); assertEquals(rules2, rules); }
@Test public void test_getRules_StringBoolean_dynamic() { MockDynamicProvider dynamicProvider = new MockDynamicProvider(); ZoneRulesProvider.registerProvider(dynamicProvider); assertEquals(dynamicProvider.count, 0); ZoneRules rules1 = ZoneId.of("DynamicLocation").getRules(); assertEquals(dynamicProvider.count, 2); assertEquals(rules1, dynamicProvider.BASE); ZoneRules rules2 = ZoneId.of("DynamicLocation").getRules(); assertEquals(dynamicProvider.count, 4); assertEquals(rules2, dynamicProvider.ALTERNATE); }
@Test public void test_getVersions_String() { NavigableMap<String, ZoneRules> versions = ZoneRulesProvider.getVersions("Europe/London"); assertTrue(versions.size() >= 1); ZoneRules rules = ZoneRulesProvider.getRules("Europe/London", false); assertEquals(versions.lastEntry().getValue(), rules); NavigableMap<String, ZoneRules> copy = new TreeMap<>(versions); versions.clear(); assertEquals(versions.size(), 0); NavigableMap<String, ZoneRules> versions2 = ZoneRulesProvider.getVersions("Europe/London"); assertEquals(versions2, copy); }
@Test public void test_registerProvider() { Set<String> pre = ZoneRulesProvider.getAvailableZoneIds(); assertEquals(pre.contains("FooLocation"), false); ZoneRulesProvider.registerProvider(new MockTempProvider()); assertEquals(pre.contains("FooLocation"), false); Set<String> post = ZoneRulesProvider.getAvailableZoneIds(); assertEquals(post.contains("FooLocation"), true); assertEquals(ZoneRulesProvider.getRules("FooLocation", false), ZoneOffset.of("+01:45").getRules()); }
public void test_printText() { Random r = RandomFactory.getRandom(); int N = 8; Locale[] locales = Locale.getAvailableLocales(); Set<String> zids = ZoneRulesProvider.getAvailableZoneIds(); ZonedDateTime zdt = ZonedDateTime.now(); //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size()); while (N-- > 0) { zdt = zdt.withDayOfYear(r.nextInt(365) + 1) .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); for (String zid : zids) { if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) { continue; // TBD: match jdk behavior? } zdt = zdt.withZoneSameLocal(ZoneId.of(zid)); TimeZone tz = TimeZone.getTimeZone(zid); boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli())); for (Locale locale : locales) { String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale); String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale); if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+")) || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) { printText(locale, zdt, TextStyle.FULL, tz, tz.getID()); printText(locale, zdt, TextStyle.SHORT, tz, tz.getID()); continue; } printText(locale, zdt, TextStyle.FULL, tz, tz.getDisplayName(isDST, TimeZone.LONG, locale)); printText(locale, zdt, TextStyle.SHORT, tz, tz.getDisplayName(isDST, TimeZone.SHORT, locale)); } } } }
public void test_ParseText() { Locale[] locales = new Locale[] { Locale.ENGLISH, Locale.JAPANESE, Locale.FRENCH }; Set<String> zids = ZoneRulesProvider.getAvailableZoneIds(); for (Locale locale : locales) { parseText(zids, locale, TextStyle.FULL, false); parseText(zids, locale, TextStyle.FULL, true); parseText(zids, locale, TextStyle.SHORT, false); parseText(zids, locale, TextStyle.SHORT, true); } }
public void test_printText() { Random r = RandomFactory.getRandom(); int N = 8; Locale[] locales = Locale.getAvailableLocales(); Set<String> zids = ZoneRulesProvider.getAvailableZoneIds(); ZonedDateTime zdt = ZonedDateTime.now(); //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size()); while (N-- > 0) { zdt = zdt.withDayOfYear(r.nextInt(365) + 1) .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); for (String zid : zids) { if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) { continue; // TBD: match jdk behavior? } zdt = zdt.withZoneSameLocal(ZoneId.of(zid)); TimeZone tz = TimeZone.getTimeZone(zid); boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli())); for (Locale locale : locales) { printText(locale, zdt, TextStyle.FULL, tz, tz.getDisplayName(isDST, TimeZone.LONG, locale)); printText(locale, zdt, TextStyle.SHORT, tz, tz.getDisplayName(isDST, TimeZone.SHORT, locale)); } } } }