View Javadoc
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 static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.util.TimeZone;
22  
23  import org.apache.commons.lang3.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests for FastTimeZone
28   */
29  public class FastTimeZoneTest extends AbstractLangTest {
30  
31      private static final int HOURS_23 = 23 * 60 * 60 * 1000;
32      private static final int HOURS_2 = 2 * 60 * 60 * 1000;
33      private static final int MINUTES_59 = 59 * 60 * 1000;
34      private static final int MINUTES_5 = 5 * 60 * 1000;
35  
36      @Test
37      public void testBareGmt() {
38          assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone(TimeZones.GMT_ID));
39      }
40  
41      @Test
42      public void testGetGmtTimeZone() {
43          assertEquals(0, FastTimeZone.getGmtTimeZone().getRawOffset());
44      }
45  
46      @Test
47      public void testGmtPrefix() {
48          assertEquals(HOURS_23, FastTimeZone.getGmtTimeZone("GMT+23:00").getRawOffset());
49          assertEquals(-HOURS_23, FastTimeZone.getGmtTimeZone("GMT-23:00").getRawOffset());
50      }
51  
52      @Test
53      public void testHoursColonMinutes() {
54          assertEquals(HOURS_23, FastTimeZone.getGmtTimeZone("23:00").getRawOffset());
55          assertEquals(HOURS_2, FastTimeZone.getGmtTimeZone("2:00").getRawOffset());
56          assertEquals(MINUTES_59, FastTimeZone.getGmtTimeZone("00:59").getRawOffset());
57          assertEquals(MINUTES_5, FastTimeZone.getGmtTimeZone("00:5").getRawOffset());
58          assertEquals(HOURS_23+MINUTES_59, FastTimeZone.getGmtTimeZone("23:59").getRawOffset());
59          assertEquals(HOURS_2+MINUTES_5, FastTimeZone.getGmtTimeZone("2:5").getRawOffset());
60      }
61  
62      @Test
63      public void testHoursMinutes() {
64          assertEquals(HOURS_23, FastTimeZone.getGmtTimeZone("2300").getRawOffset());
65          assertEquals(HOURS_2, FastTimeZone.getGmtTimeZone("0200").getRawOffset());
66          assertEquals(MINUTES_59, FastTimeZone.getGmtTimeZone("0059").getRawOffset());
67          assertEquals(MINUTES_5, FastTimeZone.getGmtTimeZone("0005").getRawOffset());
68          assertEquals(HOURS_23+MINUTES_59, FastTimeZone.getGmtTimeZone("2359").getRawOffset());
69          assertEquals(HOURS_2+MINUTES_5, FastTimeZone.getGmtTimeZone("0205").getRawOffset());
70      }
71  
72      @Test
73      public void testOlson() {
74          assertEquals(TimeZone.getTimeZone("America/New_York"), FastTimeZone.getTimeZone("America/New_York"));
75      }
76  
77      @Test
78      public void testSign() {
79          assertEquals(HOURS_23, FastTimeZone.getGmtTimeZone("+23:00").getRawOffset());
80          assertEquals(HOURS_2, FastTimeZone.getGmtTimeZone("+2:00").getRawOffset());
81          assertEquals(-HOURS_23, FastTimeZone.getGmtTimeZone("-23:00").getRawOffset());
82          assertEquals(-HOURS_2, FastTimeZone.getGmtTimeZone("-2:00").getRawOffset());
83      }
84  
85      @Test
86      public void testUTC() {
87          assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("UTC"));
88      }
89  
90      @Test
91      public void testZ() {
92          assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("Z"));
93      }
94  
95      @Test
96      public void testZeroOffsetsReturnSingleton() {
97          assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("+0"));
98          assertEquals(FastTimeZone.getGmtTimeZone(), FastTimeZone.getTimeZone("-0"));
99      }
100 
101 }