001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.lang3.time;
019
020import java.time.ZoneId;
021import java.util.TimeZone;
022
023import org.apache.commons.lang3.ArraySorter;
024import org.apache.commons.lang3.JavaVersion;
025import org.apache.commons.lang3.ObjectUtils;
026import org.apache.commons.lang3.SystemProperties;
027import org.apache.commons.lang3.SystemUtils;
028
029/**
030 * Helps dealing with {@link java.util.TimeZone}s.
031 *
032 * @since 3.7
033 */
034public class TimeZones {
035
036    /**
037     * A public version of {@link java.util.TimeZone}'s package private {@code GMT_ID} field.
038     */
039    public static final String GMT_ID = "GMT";
040
041    /**
042     * The GMT time zone.
043     *
044     * @since 3.13.0
045     */
046    public static final TimeZone GMT = TimeZones.getTimeZone(GMT_ID);
047
048    private static final boolean JAVA_25 = SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_25);
049
050    /**
051     * The sorted available IDs.
052     * <p>
053     * Make a defensive copy, just in case.
054     * </p>
055     * @see TimeZone#getAvailableIDs()
056     */
057    static final String[] SORTED_AVAILABLE_IDS = ArraySorter.sort(TimeZone.getAvailableIDs().clone());
058
059    /**
060     * Delegates to {@link TimeZone#getTimeZone(String)}, on Java 25 and up, maps an ID if it's a key in {@link ZoneId#SHORT_IDS}.
061     * <p>
062     * On Java 25, calling {@link TimeZone#getTimeZone(String)} with an ID in {@link ZoneId#SHORT_IDS} writes a message to {@link System#err} in the form:
063     * </p>
064     *
065     * <pre>
066     * WARNING: Use of the three-letter time zone ID "the-short-id" is deprecated and it will be removed in a future release
067     * </pre>
068     * <p>
069     * You can disable mapping from {@link ZoneId#SHORT_IDS} by setting the system property {@code "TimeZones.mapShortIDs=false"}.
070     * </p>
071     *
072     * @param id Same as {@link TimeZone#getTimeZone(String)}.
073     * @return Same as {@link TimeZone#getTimeZone(String)}.
074     * @since 3.20.0
075     */
076    public static TimeZone getTimeZone(final String id) {
077        return TimeZone.getTimeZone(JAVA_25 && mapShortIDs() ? ZoneId.SHORT_IDS.getOrDefault(id, id) : id);
078    }
079
080    private static boolean mapShortIDs() {
081        return SystemProperties.getBoolean(TimeZones.class, "mapShortIDs", () -> true);
082    }
083
084    /**
085     * Returns the given TimeZone if non-{@code null}, otherwise {@link TimeZone#getDefault()}.
086     *
087     * @param timeZone a locale or {@code null}.
088     * @return the given locale if non-{@code null}, otherwise {@link TimeZone#getDefault()}.
089     * @since 3.13.0
090     */
091    public static TimeZone toTimeZone(final TimeZone timeZone) {
092        return ObjectUtils.getIfNull(timeZone, TimeZone::getDefault);
093    }
094
095    /** Do not instantiate. */
096    private TimeZones() {
097    }
098}