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  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNotSame;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  
24  import java.text.ParsePosition;
25  import java.util.Date;
26  import java.util.Locale;
27  import java.util.TimeZone;
28  
29  import org.apache.commons.lang3.AbstractLangTest;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.params.ParameterizedTest;
32  import org.junit.jupiter.params.provider.CsvSource;
33  import org.junitpioneer.jupiter.ReadsDefaultLocale;
34  import org.junitpioneer.jupiter.ReadsDefaultTimeZone;
35  
36  /**
37   * Tests that {@link FastDateFormat} can parse what its own 'Z' patterns format: FastDatePrinter emits the
38   * ISO 8601 extended offset form "+hh:mm" for a pattern with two, or three or more, 'Z' letters, so the
39   * parser side must accept that form.
40   */
41  class FastDateFormat_ZRoundTripTest extends AbstractLangTest {
42  
43      private static final TimeZone NEW_YORK = TimeZones.getTimeZone("America/New_York");
44  
45      private static Date parse(final DateParser parser, final String text) {
46          final ParsePosition parsePosition = new ParsePosition(0);
47          final Date date = parser.parse(text, parsePosition);
48          assertNotNull(date, () -> parser.getPattern() + " failed to parse '" + text + "' (error index " + parsePosition.getErrorIndex() + ")");
49          assertEquals(text.length(), parsePosition.getIndex(), parser.getPattern());
50          return date;
51      }
52  
53      private static Date parse(final String pattern, final String text) {
54          return parse(FastDateFormat.getInstance(pattern, NEW_YORK, Locale.US), text);
55      }
56  
57      @Test
58      @ReadsDefaultLocale
59      @ReadsDefaultTimeZone
60      void testClearExtendedTimeZoneCache() {
61          final Object strategy = FastDateParser.TimeZoneStrategy.getIsoExtendedStrategy(Locale.US);
62          assertSame(strategy, FastDateParser.TimeZoneStrategy.getIsoExtendedStrategy(Locale.US));
63          final FastDateParser parser = new FastDateParser("yyyy-MM-ddZZZ", NEW_YORK, Locale.US);
64          final String text = "2023-10-27+05:30";
65          final Date expected = parse("yyyy-MM-ddZZ", text);
66          FastDateParser.clear();
67          assertNotSame(strategy, FastDateParser.TimeZoneStrategy.getIsoExtendedStrategy(Locale.US));
68          assertEquals(expected, parse(parser, text));
69          assertEquals(expected, parse(new FastDateParser("yyyy-MM-ddZZZ", NEW_YORK, Locale.US), text));
70      }
71  
72      @Test
73      @ReadsDefaultLocale
74      @ReadsDefaultTimeZone
75      void testParseExplicitColonOffset() {
76          // Same instant as the known-good 'ZZ' parsing of the identical text.
77          assertEquals(parse("yyyy-MM-ddZZ", "2023-10-27-05:30"), parse("yyyy-MM-ddZZZ", "2023-10-27-05:30"));
78      }
79  
80      @ParameterizedTest
81      @CsvSource({"+0530, +05:30", "-0330, -03:30", "GMT+05:30, +05:30", "PST, -08:00", "Pacific Standard Time, -08:00", "UTC, +00:00"})
82      @ReadsDefaultLocale
83      @ReadsDefaultTimeZone
84      void testParseLegacyTimeZones(final String zone, final String offset) {
85          assertEquals(parse("yyyy-MM-ddZZ", "2023-10-27" + offset), parse("yyyy-MM-ddZZZ", "2023-10-27" + zone));
86      }
87  
88      @ParameterizedTest
89      @CsvSource({"UTC, +00:00", "GMT+05:30, +05:30", "GMT-03:30, -03:30", "GMT+12:45, +12:45"})
90      @ReadsDefaultLocale
91      @ReadsDefaultTimeZone
92      void testRoundTripOffsets(final String zoneId, final String offset) {
93          final Date date = new Date(1698446400123L);
94          final TimeZone zone = TimeZones.getTimeZone(zoneId);
95          for (int zCount = 2; zCount <= 8; zCount++) {
96              final String pattern = "yyyy-MM-dd HH:mm:ss.SSS" + "ZZZZZZZZ".substring(0, zCount);
97              final FastDateFormat format = FastDateFormat.getInstance(pattern, zone, Locale.US);
98              final String text = format.format(date);
99              final String expectedOffset = zCount == 2 && "UTC".equals(zoneId) ? "Z" : offset;
100             final String localDateTime = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSS", zone, Locale.US).format(date);
101             assertEquals(localDateTime + expectedOffset, text, pattern);
102             assertEquals(date, parse(format, text), pattern);
103         }
104     }
105 
106     @Test
107     @ReadsDefaultLocale
108     @ReadsDefaultTimeZone
109     void testRoundTripOneAndTwoZ() {
110         final Date date = new Date(1698446400000L);
111         assertEquals("2023-10-27-0400", FastDateFormat.getInstance("yyyy-MM-ddZ", NEW_YORK, Locale.US).format(date));
112         assertEquals("2023-10-27-04:00", FastDateFormat.getInstance("yyyy-MM-ddZZ", NEW_YORK, Locale.US).format(date));
113         assertEquals(parse("yyyy-MM-ddZZ", "2023-10-27-04:00"), parse("yyyy-MM-ddZ", "2023-10-27-0400"));
114     }
115 
116     @Test
117     @ReadsDefaultLocale
118     @ReadsDefaultTimeZone
119     void testRoundTripThreeOrMoreZ() {
120         for (int zCount = 3; zCount <= 6; zCount++) {
121             final String pattern = "yyyy-MM-dd" + "ZZZZZZZZ".substring(0, zCount);
122             final FastDateFormat format = FastDateFormat.getInstance(pattern, NEW_YORK, Locale.US);
123             final String text = format.format(new Date(1698446400000L));
124             assertEquals("2023-10-27-04:00", text, pattern);
125             assertEquals(parse("yyyy-MM-ddZZ", "2023-10-27-04:00"), parse(format, text), pattern);
126         }
127     }
128 }