1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.lang3.time; 18 19 import java.util.TimeZone; 20 import java.util.regex.Matcher; 21 import java.util.regex.Pattern; 22 23 /** 24 * Faster methods to produce custom time zones. 25 * 26 * @since 3.7 27 */ 28 public class FastTimeZone { 29 30 private static final Pattern GMT_PATTERN = Pattern.compile("^(?:(?i)GMT)?([+-])?(\\d\\d?)?(:?(\\d\\d?))?$"); 31 32 private static final TimeZone GREENWICH = new GmtTimeZone(false, 0, 0); 33 34 /** 35 * Gets the GMT TimeZone. 36 * @return A TimeZone with a raw offset of zero. 37 */ 38 public static TimeZone getGmtTimeZone() { 39 return GREENWICH; 40 } 41 42 /** 43 * Gets a TimeZone with GMT offsets. A GMT offset must be either 'Z', or 'UTC', or match 44 * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes. 45 * 46 * @param pattern The GMT offset 47 * @return A TimeZone with offset from GMT or null, if pattern does not match. 48 */ 49 public static TimeZone getGmtTimeZone(final String pattern) { 50 if ("Z".equals(pattern) || "UTC".equals(pattern)) { 51 return GREENWICH; 52 } 53 54 final Matcher m = GMT_PATTERN.matcher(pattern); 55 if (m.matches()) { 56 final int hours = parseInt(m.group(2)); 57 final int minutes = parseInt(m.group(4)); 58 if (hours == 0 && minutes == 0) { 59 return GREENWICH; 60 } 61 return new GmtTimeZone(parseSign(m.group(1)), hours, minutes); 62 } 63 return null; 64 } 65 66 /** 67 * Gets a TimeZone, looking first for GMT custom ids, then falling back to Olson ids. 68 * A GMT custom id can be 'Z', or 'UTC', or has an optional prefix of GMT, 69 * followed by sign, hours digit(s), optional colon(':'), and optional minutes digits. 70 * i.e. <em>[GMT] (+|-) Hours [[:] Minutes]</em> 71 * 72 * @param id A GMT custom id (or Olson id 73 * @return A time zone 74 */ 75 public static TimeZone getTimeZone(final String id) { 76 final TimeZone tz = getGmtTimeZone(id); 77 if (tz != null) { 78 return tz; 79 } 80 return TimeZone.getTimeZone(id); 81 } 82 83 private static int parseInt(final String group) { 84 return group != null ? Integer.parseInt(group) : 0; 85 } 86 87 private static boolean parseSign(final String group) { 88 return group != null && group.charAt(0) == '-'; 89 } 90 91 // do not instantiate 92 private FastTimeZone() { 93 } 94 95 }