1 /*
2  * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package test.java.time.zone;
25 
26 import java.time.DayOfWeek;
27 import java.time.Duration;
28 import java.time.Instant;
29 import java.time.LocalDate;
30 import java.time.LocalDateTime;
31 import java.time.LocalTime;
32 import java.time.Month;
33 import java.time.Year;
34 import java.time.ZonedDateTime;
35 import java.time.ZoneId;
36 import java.time.ZoneOffset;
37 import java.time.zone.ZoneOffsetTransition;
38 import java.time.zone.ZoneOffsetTransitionRule;
39 import java.time.zone.ZoneRules;
40 import java.util.Collections;
41 import java.util.List;
42 
43 import org.testng.annotations.DataProvider;
44 import org.testng.annotations.Test;
45 import static org.testng.Assert.assertEquals;
46 import static org.testng.Assert.assertTrue;
47 
48 /**
49  * @summary Tests for ZoneRules class.
50  *
51  * @bug 8212970 8236903 8239836
52  */
53 @Test
54 public class TestZoneRules {
55 
56     private static final ZoneId DUBLIN = ZoneId.of("Europe/Dublin");
57     private static final ZoneId PRAGUE = ZoneId.of("Europe/Prague");
58     private static final ZoneId WINDHOEK = ZoneId.of("Africa/Windhoek");
59     private static final ZoneId CASABLANCA = ZoneId.of("Africa/Casablanca");
60 
61     private static final ZoneId TOKYO = ZoneId.of("Asia/Tokyo");
62     private static final LocalTime ONE_AM = LocalTime.of(1, 0);
63 
64     private static final ZoneOffset OFF_0 = ZoneOffset.ofHours(0);
65     private static final ZoneOffset OFF_1 = ZoneOffset.ofHours(1);
66     private static final ZoneOffset OFF_2 = ZoneOffset.ofHours(2);
67     private static final List EL = Collections.emptyList();
68     private static final ZoneOffsetTransition ZOT = ZoneId.of("America/Los_Angeles").getRules().getTransitions().get(0);
69     private static final ZoneOffsetTransitionRule ZOTR = ZoneId.of("America/Los_Angeles").getRules().getTransitionRules().get(0);
70 
71     @DataProvider
negativeDST()72     private Object[][] negativeDST () {
73         return new Object[][] {
74             // ZoneId, localDate, offset, standard offset, isDaylightSavings
75             // Europe/Dublin for the Rule "Eire"
76             // Android-changed: During 1969 - 1971 summers, Europe/Dublin is using GMT+1 as the standard time.
77             // {DUBLIN, LocalDate.of(1970, 6, 23), OFF_1, OFF_0, true},
78             // {DUBLIN, LocalDate.of(1971, 6, 23), OFF_1, OFF_0, true},
79             {DUBLIN, LocalDate.of(1970, 6, 23), OFF_1, OFF_1, false},
80             {DUBLIN, LocalDate.of(1971, 6, 23), OFF_1, OFF_1, false},
81             {DUBLIN, LocalDate.of(1971, 11, 1), OFF_0, OFF_0, false},
82             {DUBLIN, LocalDate.of(2019, 6, 23), OFF_1, OFF_0, true},
83             {DUBLIN, LocalDate.of(2019, 12, 23), OFF_0, OFF_0, false},
84 
85             // Europe/Prague which contains fixed negative savings (not a named Rule)
86             {PRAGUE, LocalDate.of(1946, 9, 30), OFF_2, OFF_1, true},
87             {PRAGUE, LocalDate.of(1946, 10, 10), OFF_1, OFF_1, false},
88             {PRAGUE, LocalDate.of(1946, 12, 3), OFF_0, OFF_0, false},
89             {PRAGUE, LocalDate.of(1947, 2, 25), OFF_1, OFF_1, false},
90             {PRAGUE, LocalDate.of(1947, 4, 30), OFF_2, OFF_1, true},
91 
92             // Africa/Windhoek for the Rule "Namibia"
93             {WINDHOEK, LocalDate.of(1994, 3, 23), OFF_1, OFF_1, false},
94             {WINDHOEK, LocalDate.of(2016, 9, 23), OFF_2, OFF_1, true},
95 
96             // Africa/Casablanca for the Rule "Morocco" Defines negative DST till 2037 as of 2019a.
97             {CASABLANCA, LocalDate.of(1939, 9, 13), OFF_1, OFF_0, true},
98             {CASABLANCA, LocalDate.of(1939, 11, 20), OFF_0, OFF_0, false},
99             {CASABLANCA, LocalDate.of(2018, 6, 18), OFF_1, OFF_0, true},
100             {CASABLANCA, LocalDate.of(2019, 1, 1), OFF_1, OFF_0, true},
101             {CASABLANCA, LocalDate.of(2019, 5, 6), OFF_0, OFF_0, false},
102             {CASABLANCA, LocalDate.of(2037, 10, 5), OFF_0, OFF_0, false},
103             {CASABLANCA, LocalDate.of(2037, 11, 16), OFF_1, OFF_0, true},
104             {CASABLANCA, LocalDate.of(2038, 11, 8), OFF_1, OFF_0, true},
105         };
106     }
107 
108     @DataProvider
transitionBeyondDay()109     private Object[][] transitionBeyondDay() {
110         return new Object[][] {
111             // ZoneId, LocalDateTime, beforeOffset, afterOffset
112 
113             // Asserts that the rule:
114             // Rule Japan   1948    1951    -   Sep Sat>=8  25:00   0   S
115             // translates to the next day.
116             {TOKYO, LocalDateTime.of(LocalDate.of(1948, 9, 12), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
117             {TOKYO, LocalDateTime.of(LocalDate.of(1949, 9, 11), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
118             {TOKYO, LocalDateTime.of(LocalDate.of(1950, 9, 10), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
119             {TOKYO, LocalDateTime.of(LocalDate.of(1951, 9, 9), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
120         };
121     }
122 
123     @DataProvider
emptyTransitionList()124     private Object[][] emptyTransitionList() {
125         return new Object[][] {
126             // days, offset, std offset, savings, isDST
127             {7, 1, 2, -1, true},
128             {-7, 1, 1, 0, false},
129         };
130     }
131 
132     @DataProvider
isFixedOffset()133     private Object[][] isFixedOffset() {
134         return new Object[][] {
135             // ZoneRules, expected
136             {ZoneRules.of(OFF_0), true},
137             {ZoneRules.of(OFF_0, OFF_0, EL, EL, EL), true},
138             {ZoneRules.of(OFF_0, OFF_1, EL, EL, EL), false},
139             {ZoneRules.of(OFF_0, OFF_0, Collections.singletonList(ZOT), EL, EL), false},
140             {ZoneRules.of(OFF_0, OFF_0, EL, Collections.singletonList(ZOT), EL), false},
141             {ZoneRules.of(OFF_0, OFF_0, EL, EL, Collections.singletonList(ZOTR)), false},
142         };
143     }
144 
145     /**
146      * Test ZoneRules whether the savings are positive in time zones that have
147      * negative savings in the source TZ files.
148      * @bug 8212970
149      */
150     @Test(dataProvider="negativeDST")
test_NegativeDST(ZoneId zid, LocalDate ld, ZoneOffset offset, ZoneOffset stdOffset, boolean isDST)151     public void test_NegativeDST(ZoneId zid, LocalDate ld, ZoneOffset offset, ZoneOffset stdOffset, boolean isDST) {
152         Instant i = Instant.from(ZonedDateTime.of(ld, LocalTime.MIN, zid));
153         ZoneRules zr = zid.getRules();
154         assertEquals(zr.getOffset(i), offset);
155         assertEquals(zr.getStandardOffset(i), stdOffset);
156         assertEquals(zr.isDaylightSavings(i), isDST);
157     }
158 
159     /**
160      * Check the transition cutover time beyond 24:00, which should translate into the next day.
161      * @bug 8212970
162      */
163     @Test(dataProvider="transitionBeyondDay")
test_TransitionBeyondDay(ZoneId zid, LocalDateTime ldt, ZoneOffset before, ZoneOffset after)164     public void test_TransitionBeyondDay(ZoneId zid, LocalDateTime ldt, ZoneOffset before, ZoneOffset after) {
165         ZoneOffsetTransition zot = ZoneOffsetTransition.of(ldt, before, after);
166         ZoneRules zr = zid.getRules();
167         assertTrue(zr.getTransitions().contains(zot));
168     }
169 
170     /**
171      * Make sure ZoneRules.findYear() won't throw out-of-range DateTimeException for
172      * year calculation.
173      * @bug 8236903
174      */
175     @Test
test_TransitionLastRuleYear()176     public void test_TransitionLastRuleYear() {
177         Instant maxLocalDateTime = LocalDateTime.of(Year.MAX_VALUE,
178                 12,
179                 31,
180                 23,
181                 59,
182                 59,
183                 999999999).toInstant(ZoneOffset.UTC);
184         ZoneRules zoneRulesA = ZoneRules.of(OFF_1);
185         ZoneOffsetTransition transition = ZoneOffsetTransition.of(LocalDateTime.ofEpochSecond(0, 0, OFF_0),
186                 OFF_0,
187                 OFF_1);
188         ZoneOffsetTransitionRule transitionRule = ZoneOffsetTransitionRule.of(Month.JANUARY,
189                 1,
190                 DayOfWeek.SUNDAY,
191                 LocalTime.MIDNIGHT,
192                 true,
193                 ZoneOffsetTransitionRule.TimeDefinition.STANDARD,
194                 OFF_0,
195                 OFF_0,
196                 OFF_1);
197         ZoneRules zoneRulesB = ZoneRules.of(OFF_0,
198                 OFF_0,
199                 Collections.singletonList(transition),
200                 Collections.singletonList(transition),
201                 Collections.singletonList(transitionRule));
202         ZoneOffset offsetA = zoneRulesA.getOffset(maxLocalDateTime);
203         ZoneOffset offsetB = zoneRulesB.getOffset(maxLocalDateTime);
204         assertEquals(offsetA, offsetB);
205     }
206 
207     /**
208      * Tests whether empty "transitionList" is correctly interpreted.
209      * @bug 8239836
210      */
211     @Test(dataProvider="emptyTransitionList")
test_EmptyTransitionList(int days, int offset, int stdOffset, int savings, boolean isDST)212     public void test_EmptyTransitionList(int days, int offset, int stdOffset, int savings, boolean isDST) {
213         LocalDateTime transitionDay = LocalDateTime.of(2020, 1, 1, 2, 0);
214         Instant testDay = transitionDay.plusDays(days).toInstant(ZoneOffset.UTC);
215         ZoneOffsetTransition trans = ZoneOffsetTransition.of(
216             transitionDay,
217             OFF_1,
218             OFF_2);
219         ZoneRules rules = ZoneRules.of(OFF_1, OFF_1,
220             Collections.singletonList(trans),
221             Collections.emptyList(), Collections.emptyList());
222 
223         assertEquals(rules.getOffset(testDay), ZoneOffset.ofHours(offset));
224         assertEquals(rules.getStandardOffset(testDay), ZoneOffset.ofHours(stdOffset));
225         assertEquals(rules.getDaylightSavings(testDay), Duration.ofHours(savings));
226         assertEquals(rules.isDaylightSavings(testDay), isDST);
227     }
228 
229     /**
230      * Tests whether isFixedOffset() is working correctly
231      * @bug 8239836
232      */
233     @Test(dataProvider="isFixedOffset")
test_IsFixedOffset(ZoneRules zr, boolean expected)234     public void test_IsFixedOffset(ZoneRules zr, boolean expected) {
235         assertEquals(zr.isFixedOffset(), expected);
236     }
237 }
238