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