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.Arrays;
  41 import java.util.Collections;
  42 
  43 import org.testng.annotations.Test;
  44 import org.testng.annotations.DataProvider;
  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     @DataProvider
  65     private Object[][] negativeDST () {
  66         return new Object[][] {
  67             // ZoneId, localDate, offset, standard offset, isDaylightSavings
  68             // Europe/Dublin for the Rule "Eire"
  69             {DUBLIN, LocalDate.of(1970, 6, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  70             {DUBLIN, LocalDate.of(1971, 6, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  71             {DUBLIN, LocalDate.of(1971, 11, 1), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  72             {DUBLIN, LocalDate.of(2019, 6, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  73             {DUBLIN, LocalDate.of(2019, 12, 23), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  74 
  75             // Europe/Prague which contains fixed negative savings (not a named Rule)
  76             {PRAGUE, LocalDate.of(1946, 9, 30), ZoneOffset.ofHours(2), ZoneOffset.ofHours(1), true},
  77             {PRAGUE, LocalDate.of(1946, 10, 10), ZoneOffset.ofHours(1), ZoneOffset.ofHours(1), false},
  78             {PRAGUE, LocalDate.of(1946, 12, 3), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  79             {PRAGUE, LocalDate.of(1947, 2, 25), ZoneOffset.ofHours(1), ZoneOffset.ofHours(1), false},
  80             {PRAGUE, LocalDate.of(1947, 4, 30), ZoneOffset.ofHours(2), ZoneOffset.ofHours(1), true},
  81 
  82             // Africa/Windhoek for the Rule "Namibia"
  83             {WINDHOEK, LocalDate.of(1994, 3, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(1), false},
  84             {WINDHOEK, LocalDate.of(2016, 9, 23), ZoneOffset.ofHours(2), ZoneOffset.ofHours(1), true},
  85 
  86             // Africa/Casablanca for the Rule "Morocco" Defines negative DST till 2037 as of 2019a.
  87             {CASABLANCA, LocalDate.of(1939, 9, 13), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  88             {CASABLANCA, LocalDate.of(1939, 11, 20), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  89             {CASABLANCA, LocalDate.of(2018, 6, 18), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  90             {CASABLANCA, LocalDate.of(2019, 1, 1), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  91             {CASABLANCA, LocalDate.of(2019, 5, 6), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  92             {CASABLANCA, LocalDate.of(2037, 10, 5), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  93             {CASABLANCA, LocalDate.of(2037, 11, 16), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  94             {CASABLANCA, LocalDate.of(2038, 11, 1), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  95         };
  96     }
  97 
  98     @DataProvider
  99     private Object[][] transitionBeyondDay() {
 100         return new Object[][] {
 101             // ZoneId, LocalDateTime, beforeOffset, afterOffset
 102 
 103             // Asserts that the rule:
 104             // Rule Japan   1948    1951    -   Sep Sat>=8  25:00   0   S
 105             // translates to the next day.
 106             {TOKYO, LocalDateTime.of(LocalDate.of(1948, 9, 12), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 107             {TOKYO, LocalDateTime.of(LocalDate.of(1949, 9, 11), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 108             {TOKYO, LocalDateTime.of(LocalDate.of(1950, 9, 10), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 109             {TOKYO, LocalDateTime.of(LocalDate.of(1951, 9, 9), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 110         };
 111     }
 112 
 113     @DataProvider
 114     private Object[][] emptyTransitionList() {
 115         return new Object[][] {
 116             // days, offset, std offset, savings, isDST
 117             {7, 1, 2, -1, true},
 118             {-7, 1, 1, 0, false},
 119         };
 120     }
 121 
 122     /**
 123      * Test ZoneRules whether the savings are positive in time zones that have
 124      * negative savings in the source TZ files.
 125      * @bug 8212970
 126      */
 127     @Test(dataProvider="negativeDST")
 128     public void test_NegativeDST(ZoneId zid, LocalDate ld, ZoneOffset offset, ZoneOffset stdOffset, boolean isDST) {
 129         Instant i = Instant.from(ZonedDateTime.of(ld, LocalTime.MIN, zid));
 130         ZoneRules zr = zid.getRules();
 131         assertEquals(zr.getOffset(i), offset);
 132         assertEquals(zr.getStandardOffset(i), stdOffset);
 133         assertEquals(zr.isDaylightSavings(i), isDST);
 134     }
 135 
 136     /**
 137      * Check the transition cutover time beyond 24:00, which should translate into the next day.
 138      * @bug 8212970
 139      */
 140     @Test(dataProvider="transitionBeyondDay")
 141     public void test_TransitionBeyondDay(ZoneId zid, LocalDateTime ldt, ZoneOffset before, ZoneOffset after) {
 142         ZoneOffsetTransition zot = ZoneOffsetTransition.of(ldt, before, after);
 143         ZoneRules zr = zid.getRules();
 144         assertTrue(zr.getTransitions().contains(zot));
 145     }
 146 
 147     /**
 148      * Make sure ZoneRules.findYear() won't throw out-of-range DateTimeException for
 149      * year calculation.
 150      * @bug 8236903
 151      */
 152     @Test
 153     public void test_TransitionLastRuleYear() {
 154         Instant maxLocalDateTime = LocalDateTime.of(Year.MAX_VALUE,
 155                 12,
 156                 31,
 157                 23,
 158                 59,
 159                 59,
 160                 999999999).toInstant(ZoneOffset.UTC);
 161         ZoneOffset offsetZero = ZoneOffset.ofHours(0);
 162         ZoneOffset offsetPlusOneHour = ZoneOffset.ofHours(1);
 163         ZoneRules zoneRulesA = ZoneRules.of(offsetPlusOneHour);
 164         ZoneOffsetTransition transition = ZoneOffsetTransition.of(LocalDateTime.ofEpochSecond(0, 0, offsetZero),
 165                 offsetZero,
 166                 offsetPlusOneHour);
 167         ZoneOffsetTransitionRule transitionRule = ZoneOffsetTransitionRule.of(Month.JANUARY,
 168                 1,
 169                 DayOfWeek.SUNDAY,
 170                 LocalTime.MIDNIGHT,
 171                 true,
 172                 ZoneOffsetTransitionRule.TimeDefinition.STANDARD,
 173                 offsetZero,
 174                 offsetZero,
 175                 offsetPlusOneHour);
 176         ZoneRules zoneRulesB = ZoneRules.of(offsetZero,
 177                 offsetZero,
 178                 Collections.singletonList(transition),
 179                 Collections.singletonList(transition),
 180                 Collections.singletonList(transitionRule));
 181         ZoneOffset offsetA = zoneRulesA.getOffset(maxLocalDateTime);
 182         ZoneOffset offsetB = zoneRulesB.getOffset(maxLocalDateTime);
 183         assertEquals(offsetA, offsetB);
 184     }
 185 
 186     /**
 187      * Tests whether empty "transitionList" is correctly interpreted.
 188      * @bug 8239836
 189      */
 190     @Test(dataProvider="emptyTransitionList")
 191     public void test_EmptyTransitionList(int days, int offset, int stdOffset, int savings, boolean isDST) {
 192         LocalDateTime transitionDay = LocalDateTime.of(2020, 1, 1, 2, 0);
 193         Instant testDay = transitionDay.plusDays(days).toInstant(ZoneOffset.UTC);
 194         ZoneOffsetTransition trans = ZoneOffsetTransition.of(
 195             transitionDay,
 196             ZoneOffset.ofHours(1),
 197             ZoneOffset.ofHours(2));
 198         ZoneRules rules = ZoneRules.of(ZoneOffset.ofHours(1), ZoneOffset.ofHours(1),
 199             Arrays.asList(trans),
 200             Collections.emptyList(), Collections.emptyList());
 201 
 202         assertEquals(rules.getOffset(testDay), ZoneOffset.ofHours(offset));
 203         assertEquals(rules.getStandardOffset(testDay), ZoneOffset.ofHours(stdOffset));
 204         assertEquals(rules.getDaylightSavings(testDay), Duration.ofHours(savings));
 205         assertEquals(rules.isDaylightSavings(testDay), isDST);
 206         assertTrue(!rules.isFixedOffset());
 207     }
 208 }