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 *      http://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 */
017package org.apache.commons.lang3.time;
018
019import java.util.TimeZone;
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022
023/**
024 * Faster methods to produce custom time zones.
025 *
026 * @since 3.7
027 */
028public class FastTimeZone {
029
030    private static final Pattern GMT_PATTERN = Pattern.compile("^(?:(?i)GMT)?([+-])?(\\d\\d?)?(:?(\\d\\d?))?$");
031
032    private static final TimeZone GREENWICH = new GmtTimeZone(false, 0, 0);
033
034    /**
035     * Gets the GMT TimeZone.
036     * @return A TimeZone with a raw offset of zero.
037     */
038    public static TimeZone getGmtTimeZone() {
039        return GREENWICH;
040    }
041
042    /**
043     * Gets a TimeZone with GMT offsets.  A GMT offset must be either 'Z', or 'UTC', or match
044     * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes.
045     *
046     * @param pattern The GMT offset
047     * @return A TimeZone with offset from GMT or null, if pattern does not match.
048     */
049    public static TimeZone getGmtTimeZone(final String pattern) {
050        if ("Z".equals(pattern) || "UTC".equals(pattern)) {
051            return GREENWICH;
052        }
053
054        final Matcher m = GMT_PATTERN.matcher(pattern);
055        if (m.matches()) {
056            final int hours = parseInt(m.group(2));
057            final int minutes = parseInt(m.group(4));
058            if (hours == 0 && minutes == 0) {
059                return GREENWICH;
060            }
061            return new GmtTimeZone(parseSign(m.group(1)), hours, minutes);
062        }
063        return null;
064    }
065
066    /**
067     * Gets a TimeZone, looking first for GMT custom ids, then falling back to Olson ids.
068     * A GMT custom id can be 'Z', or 'UTC', or has an optional prefix of GMT,
069     * followed by sign, hours digit(s), optional colon(':'), and optional minutes digits.
070     * i.e. <em>[GMT] (+|-) Hours [[:] Minutes]</em>
071     *
072     * @param id A GMT custom id (or Olson id
073     * @return A timezone
074     */
075    public static TimeZone getTimeZone(final String id) {
076        final TimeZone tz = getGmtTimeZone(id);
077        if (tz != null) {
078            return tz;
079        }
080        return TimeZone.getTimeZone(id);
081    }
082
083    private static int parseInt(final String group) {
084        return group != null ? Integer.parseInt(group) : 0;
085    }
086
087    private static boolean parseSign(final String group) {
088        return group != null && group.charAt(0) == '-';
089    }
090
091    // do not instantiate
092    private FastTimeZone() {
093    }
094
095}