8347955: TimeZone methods to stream the available timezone IDs

Reviewed-by: naoto, rriggs
This commit is contained in:
Justin Lu 2025-01-28 21:31:46 +00:00
parent 1efae9a41e
commit 3a564ed101
16 changed files with 177 additions and 34 deletions

View file

@ -41,6 +41,7 @@ package java.util;
import java.io.Serializable;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.stream.Stream;
import jdk.internal.util.StaticProperty;
import sun.util.calendar.ZoneInfo;
@ -615,24 +616,63 @@ public abstract class TimeZone implements Serializable, Cloneable {
/**
* Gets the available IDs according to the given time zone offset in milliseconds.
*
* @apiNote Consider using {@link #availableIDs(int)} which returns
* a stream of the available time zone IDs according to the given offset.
*
* @param rawOffset the given time zone GMT offset in milliseconds.
* @return an array of IDs, where the time zone for that ID has
* the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
* both have GMT-07:00, but differ in daylight saving behavior.
* @see #getRawOffset()
* @see #availableIDs(int)
*/
public static String[] getAvailableIDs(int rawOffset) {
return ZoneInfo.getAvailableIDs(rawOffset);
}
/**
* Gets all the available IDs supported.
* @return an array of IDs.
* {@return an array of the available IDs supported}
*
* @apiNote Consider using {@link #availableIDs()} which returns
* a stream of the available time zone IDs.
*
* @see #availableIDs()
*/
public static String[] getAvailableIDs() {
return ZoneInfo.getAvailableIDs();
}
/**
* Gets the available IDs according to the given time zone offset in milliseconds.
*
* @implNote Unlike {@link #getAvailableIDs(int)}, this method does
* not create a copy of the {@code TimeZone} IDs array.
*
* @param rawOffset the given time zone GMT offset in milliseconds.
* @return a stream of IDs, where the time zone for that ID has
* the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
* both have GMT-07:00, but differ in daylight saving behavior.
* @see #getRawOffset()
* @see #getAvailableIDs(int)
* @since 25
*/
public static Stream<String> availableIDs(int rawOffset) {
return ZoneInfo.availableIDs(rawOffset);
}
/**
* {@return a stream of the available IDs supported}
*
* @implNote Unlike {@link #getAvailableIDs()}, this method does
* not create a copy of the {@code TimeZone} IDs array.
*
* @since 25
* @see #getAvailableIDs()
*/
public static Stream<String> availableIDs() {
return ZoneInfo.availableIDs();
}
/**
* Gets the platform defined TimeZone ID.
**/

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2025, 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
@ -31,6 +31,7 @@ import java.util.Date;
import java.util.Map;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import java.util.stream.Stream;
/**
* <code>ZoneInfo</code> is an implementation subclass of {@link
@ -562,6 +563,27 @@ public class ZoneInfo extends TimeZone {
return ZoneInfoFile.getZoneIds(rawOffset);
}
/**
* Gets all available IDs supported in the Java run-time.
*
* @return a stream of time zone IDs.
*/
public static Stream<String> availableIDs() {
return ZoneInfoFile.zoneIds();
}
/**
* Gets all available IDs that have the same value as the
* specified raw GMT offset.
*
* @param rawOffset the GMT offset in milliseconds. This
* value should not include any daylight saving time.
* @return a stream of time zone IDs.
*/
public static Stream<String> availableIDs(int rawOffset) {
return ZoneInfoFile.zoneIds(rawOffset);
}
/**
* Gets the ZoneInfo for the given ID.
*

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2025, 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
@ -45,6 +45,7 @@ import java.util.List;
import java.util.Map;
import java.util.SimpleTimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import java.util.zip.CRC32;
import jdk.internal.util.StaticProperty;
@ -59,7 +60,7 @@ public final class ZoneInfoFile {
/**
* Gets all available IDs supported in the Java run-time.
*
* @return a set of time zone IDs.
* @return an array of time zone IDs.
*/
public static String[] getZoneIds() {
var shortIDs = ZoneId.SHORT_IDS.keySet();
@ -92,11 +93,35 @@ public final class ZoneInfoFile {
// sorted list, though the specification does not
// specify it. Keep the same behavior for better
// compatibility.
String[] list = ids.toArray(new String[ids.size()]);
String[] list = ids.toArray(new String[0]);
Arrays.sort(list);
return list;
}
/**
* Gets all available IDs supported in the Java run-time.
*
* @return a stream of time zone IDs.
*/
public static Stream<String> zoneIds() {
return Stream.concat(Arrays.stream(regions),
ZoneId.SHORT_IDS.keySet().stream());
}
/**
* Gets all available IDs that have the same value as the
* specified raw GMT offset.
*
* @param rawOffset the GMT offset in milliseconds. This
* value should not include any daylight saving time.
* @return a stream of time zone IDs.
*/
public static Stream<String> zoneIds(int rawOffset) {
return zoneIds()
.filter(id -> getZoneInfo(id).getRawOffset() == rawOffset)
.sorted(); // Sort the IDs, see getZoneIds(int)
}
public static ZoneInfo getZoneInfo(String zoneId) {
if (zoneId == null) {
return null;

View file

@ -55,7 +55,7 @@ public class CLDRTimeZoneNameProviderImpl extends TimeZoneNameProviderImpl {
private static final String NO_INHERITANCE_MARKER = "\u2205\u2205\u2205";
private static class AVAILABLE_IDS {
static final String[] INSTANCE =
Arrays.stream(ZoneInfoFile.getZoneIds())
ZoneInfoFile.zoneIds()
.sorted()
.toArray(String[]::new);
}