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 * https://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 *
37 * @return A TimeZone with a raw offset of zero.
38 */
39 public static TimeZone getGmtTimeZone() {
40 return GREENWICH;
41 }
42
43 /**
44 * Gets a TimeZone with GMT offsets. A GMT offset must be either 'Z', or 'UTC', or match
45 * <em>(GMT)? hh?(:?mm?)?</em>, where h and m are digits representing hours and minutes.
46 *
47 * @param pattern The GMT offset
48 * @return A TimeZone with offset from GMT or null, if pattern does not match.
49 */
50 public static TimeZone getGmtTimeZone(final String pattern) {
51 if ("Z".equals(pattern) || "UTC".equals(pattern)) {
52 return GREENWICH;
53 }
54
55 final Matcher m = GMT_PATTERN.matcher(pattern);
56 if (m.matches()) {
57 final int hours = parseInt(m.group(2));
58 final int minutes = parseInt(m.group(4));
59 if (hours == 0 && minutes == 0) {
60 return GREENWICH;
61 }
62 return new GmtTimeZone(parseSign(m.group(1)), hours, minutes);
63 }
64 return null;
65 }
66
67 /**
68 * Gets a TimeZone, looking first for GMT custom ids, then falling back to Olson ids.
69 * A GMT custom id can be 'Z', or 'UTC', or has an optional prefix of GMT,
70 * followed by sign, hours digit(s), optional colon(':'), and optional minutes digits.
71 * i.e. <em>[GMT] (+|-) Hours [[:] Minutes]</em>
72 *
73 * @param id A GMT custom id (or Olson id
74 * @return A time zone
75 */
76 public static TimeZone getTimeZone(final String id) {
77 final TimeZone tz = getGmtTimeZone(id);
78 if (tz != null) {
79 return tz;
80 }
81 return TimeZones.getTimeZone(id);
82 }
83
84 private static int parseInt(final String group) {
85 return group != null ? Integer.parseInt(group) : 0;
86 }
87
88 private static boolean parseSign(final String group) {
89 return group != null && group.charAt(0) == '-';
90 }
91
92 // do not instantiate
93 private FastTimeZone() {
94 }
95
96 }