1 /*
   2  * Copyright (c) 2019, 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 static org.testng.Assert.assertEquals;
  27 
  28 import java.time.Instant;
  29 import java.time.LocalDate;
  30 import java.time.LocalDateTime;
  31 import java.time.LocalTime;
  32 import java.time.ZonedDateTime;
  33 import java.time.ZoneId;
  34 import java.time.ZoneOffset;
  35 import java.time.zone.ZoneOffsetTransition;
  36 import java.time.zone.ZoneRules;
  37 
  38 import org.testng.annotations.Test;
  39 import org.testng.annotations.DataProvider;
  40 import static org.testng.Assert.assertEquals;
  41 import static org.testng.Assert.assertTrue;
  42 
  43 /**
  44  * @summary Test ZoneRules whether the savings are positive in time zones that have
  45  *      negative savings in the source TZ files. Also, check the transition cutover
  46  *      time beyond 24:00, which should translate into the next day.
  47  *
  48  * @bug 8212970
  49  */
  50 @Test
  51 public class TestZoneRules {
  52 
  53     private static final ZoneId DUBLIN = ZoneId.of("Europe/Dublin");
  54     private static final ZoneId PRAGUE = ZoneId.of("Europe/Prague");
  55     private static final ZoneId WINDHOEK = ZoneId.of("Africa/Windhoek");
  56     private static final ZoneId CASABLANCA = ZoneId.of("Africa/Casablanca");
  57 
  58     private static final ZoneId TOKYO = ZoneId.of("Asia/Tokyo");
  59     private static final LocalTime ONE_AM = LocalTime.of(1, 0);
  60 
  61     @DataProvider
  62     private Object[][] negativeDST () {
  63         return new Object[][] {
  64             // ZoneId, localDate, offset, standard offset, isDaylightSavings
  65             // Europe/Dublin for the Rule "Eire"
  66             {DUBLIN, LocalDate.of(1970, 6, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  67             {DUBLIN, LocalDate.of(1971, 6, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  68             {DUBLIN, LocalDate.of(1971, 11, 1), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  69             {DUBLIN, LocalDate.of(2019, 6, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  70             {DUBLIN, LocalDate.of(2019, 12, 23), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  71 
  72             // Europe/Prague which contains fixed negative savings (not a named Rule)
  73             {PRAGUE, LocalDate.of(1946, 9, 30), ZoneOffset.ofHours(2), ZoneOffset.ofHours(1), true},
  74             {PRAGUE, LocalDate.of(1946, 10, 10), ZoneOffset.ofHours(1), ZoneOffset.ofHours(1), false},
  75             {PRAGUE, LocalDate.of(1946, 12, 3), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  76             {PRAGUE, LocalDate.of(1947, 2, 25), ZoneOffset.ofHours(1), ZoneOffset.ofHours(1), false},
  77             {PRAGUE, LocalDate.of(1947, 4, 30), ZoneOffset.ofHours(2), ZoneOffset.ofHours(1), true},
  78 
  79             // Africa/Windhoek for the Rule "Namibia"
  80             {WINDHOEK, LocalDate.of(1994, 3, 23), ZoneOffset.ofHours(1), ZoneOffset.ofHours(1), false},
  81             {WINDHOEK, LocalDate.of(2016, 9, 23), ZoneOffset.ofHours(2), ZoneOffset.ofHours(1), true},
  82 
  83             // Africa/Casablanca for the Rule "Morocco" Defines negative DST till 2037 as of 2019a.
  84             {CASABLANCA, LocalDate.of(1939, 9, 13), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  85             {CASABLANCA, LocalDate.of(1939, 11, 20), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  86             {CASABLANCA, LocalDate.of(2018, 6, 18), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  87             {CASABLANCA, LocalDate.of(2019, 1, 1), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  88             {CASABLANCA, LocalDate.of(2019, 5, 6), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  89             {CASABLANCA, LocalDate.of(2037, 10, 5), ZoneOffset.ofHours(0), ZoneOffset.ofHours(0), false},
  90             {CASABLANCA, LocalDate.of(2037, 11, 16), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  91             {CASABLANCA, LocalDate.of(2038, 11, 1), ZoneOffset.ofHours(1), ZoneOffset.ofHours(0), true},
  92         };
  93     }
  94 
  95     @DataProvider
  96     private Object[][] transitionBeyondDay() {
  97         return new Object[][] {
  98             // ZoneId, LocalDateTime, beforeOffset, afterOffset
  99 
 100             // Asserts that the rule:
 101             // Rule     Japan   1948    1951    -       Sep     Sat>=8       25:00   0       S
 102             // translates to the next day.
 103             {TOKYO, LocalDateTime.of(LocalDate.of(1948, 9, 12), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 104             {TOKYO, LocalDateTime.of(LocalDate.of(1949, 9, 11), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 105             {TOKYO, LocalDateTime.of(LocalDate.of(1950, 9, 10), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 106             {TOKYO, LocalDateTime.of(LocalDate.of(1951, 9, 9), ONE_AM), ZoneOffset.ofHours(10), ZoneOffset.ofHours(9)},
 107         };
 108     }
 109 
 110     @Test(dataProvider="negativeDST")
 111     public void test_NegativeDST(ZoneId zid, LocalDate ld, ZoneOffset offset, ZoneOffset stdOffset, boolean isDST) {
 112         Instant i = Instant.from(ZonedDateTime.of(ld, LocalTime.MIN, zid));
 113         ZoneRules zr = zid.getRules();
 114         assertEquals(zr.getOffset(i), offset);
 115         assertEquals(zr.getStandardOffset(i), stdOffset);
 116         assertEquals(zr.isDaylightSavings(i), isDST);
 117     }
 118 
 119     @Test(dataProvider="transitionBeyondDay")
 120     public void test_TransitionBeyondDay(ZoneId zid, LocalDateTime ldt, ZoneOffset before, ZoneOffset after) {
 121         ZoneOffsetTransition zot = ZoneOffsetTransition.of(ldt, before, after);
 122         ZoneRules zr = zid.getRules();
 123         assertTrue(zr.getTransitions().contains(zot));
 124     }
 125 }