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 */ 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 * 037 * @return A TimeZone with a raw offset of zero. 038 */ 039 public static TimeZone getGmtTimeZone() { 040 return GREENWICH; 041 } 042 043 /** 044 * Gets a TimeZone with GMT offsets. A GMT offset must be either 'Z', or 'UTC', or match 045 * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes. 046 * 047 * @param pattern The GMT offset 048 * @return A TimeZone with offset from GMT or null, if pattern does not match. 049 */ 050 public static TimeZone getGmtTimeZone(final String pattern) { 051 if ("Z".equals(pattern) || "UTC".equals(pattern)) { 052 return GREENWICH; 053 } 054 055 final Matcher m = GMT_PATTERN.matcher(pattern); 056 if (m.matches()) { 057 final int hours = parseInt(m.group(2)); 058 final int minutes = parseInt(m.group(4)); 059 if (hours == 0 && minutes == 0) { 060 return GREENWICH; 061 } 062 return new GmtTimeZone(parseSign(m.group(1)), hours, minutes); 063 } 064 return null; 065 } 066 067 /** 068 * Gets a TimeZone, looking first for GMT custom ids, then falling back to Olson ids. 069 * A GMT custom id can be 'Z', or 'UTC', or has an optional prefix of GMT, 070 * followed by sign, hours digit(s), optional colon(':'), and optional minutes digits. 071 * i.e. <em>[GMT] (+|-) Hours [[:] Minutes]</em> 072 * 073 * @param id A GMT custom id (or Olson id 074 * @return A time zone 075 */ 076 public static TimeZone getTimeZone(final String id) { 077 final TimeZone tz = getGmtTimeZone(id); 078 if (tz != null) { 079 return tz; 080 } 081 return TimeZones.getTimeZone(id); 082 } 083 084 private static int parseInt(final String group) { 085 return group != null ? Integer.parseInt(group) : 0; 086 } 087 088 private static boolean parseSign(final String group) { 089 return group != null && group.charAt(0) == '-'; 090 } 091 092 // do not instantiate 093 private FastTimeZone() { 094 } 095 096}