Home | History | Annotate | Download | only in zonetree
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.libcore.timezone.tzlookup.zonetree;
     18 
     19 import com.ibm.icu.text.TimeZoneNames;
     20 import com.ibm.icu.util.BasicTimeZone;
     21 import com.ibm.icu.util.TimeZone;
     22 import com.ibm.icu.util.ULocale;
     23 
     24 import org.junit.Test;
     25 
     26 import java.time.Instant;
     27 
     28 import static org.junit.Assert.assertEquals;
     29 import static org.junit.Assert.assertFalse;
     30 
     31 public class ZoneOffsetPeriodTest {
     32 
     33     @Test
     34     public void testEqualPeriods_withDstTransitions() {
     35         TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(ULocale.ENGLISH);
     36 
     37         // These two zones have DST and have had the same rules since 1974-10-27.
     38         BasicTimeZone denverTz = (BasicTimeZone) TimeZone.getTimeZone("America/Denver");
     39         BasicTimeZone boiseTz = (BasicTimeZone) TimeZone.getTimeZone("America/Boise");
     40         Instant startInstant = Instant.ofEpochSecond(152092800L); /* 1974-10-27T08:00:00Z */
     41         Instant expectedPeriod0End = Instant.ofEpochSecond(162378000L); /* 1975-02-23T09:00:00Z */
     42         // endInstant is an arbitrary "ceiling" time value for a period if there are no transitions.
     43         // It just needs to be after the period we are testing and does not influence the test.
     44         Instant endInstant = Instant.ofEpochSecond(631152000L); /* 1990-01-01T00:00:00Z */
     45         {
     46             // Request periods from both zones starting at startInstant. They should be the same.
     47             int expectedPeriod0RawOffset = -25200000;
     48             int expectedPeriod0DstOffset = 0;
     49             String expectedPeriod0Name = "Mountain Standard Time";
     50 
     51             ZoneOffsetPeriod denverPeriod0 =
     52                     ZoneOffsetPeriod.create(timeZoneNames, denverTz, startInstant, endInstant);
     53             assertEquals(startInstant, denverPeriod0.getStartInstant());
     54             assertEquals(expectedPeriod0End, denverPeriod0.getEndInstant());
     55             assertEquals(expectedPeriod0RawOffset, denverPeriod0.getRawOffsetMillis());
     56             assertEquals(expectedPeriod0DstOffset, denverPeriod0.getDstOffsetMillis());
     57             assertEquals(expectedPeriod0Name, denverPeriod0.getName());
     58 
     59             ZoneOffsetPeriod boisePeriod0 =
     60                     ZoneOffsetPeriod.create(timeZoneNames, boiseTz, startInstant, endInstant);
     61             assertEquals(startInstant, boisePeriod0.getStartInstant());
     62             assertEquals(expectedPeriod0End, boisePeriod0.getEndInstant());
     63             assertEquals(expectedPeriod0RawOffset, boisePeriod0.getRawOffsetMillis());
     64             assertEquals(expectedPeriod0DstOffset, boisePeriod0.getDstOffsetMillis());
     65             assertEquals(expectedPeriod0Name, boisePeriod0.getName());
     66 
     67             assertEquals(denverPeriod0, boisePeriod0);
     68         }
     69 
     70         {
     71             // Request two other periods from both zones starting at expectedPeriod0End.
     72             // They should be the same.
     73             Instant expectedPeriod1Start = expectedPeriod0End;
     74             Instant expectedPeriod1End =
     75                     Instant.ofEpochSecond(183542400L); /* 1975-10-26T08:00:00Z */
     76             int expectedPeriod1RawOffset = -25200000;
     77             int expectedPeriod1DstOffset = 3600000;
     78             String expectedPeriod1Name = "Mountain Daylight Time";
     79 
     80             ZoneOffsetPeriod denverPeriod1 = ZoneOffsetPeriod.create(
     81                     timeZoneNames, denverTz, expectedPeriod1Start, endInstant);
     82             assertEquals(expectedPeriod1Start, denverPeriod1.getStartInstant());
     83             assertEquals(expectedPeriod1End, denverPeriod1.getEndInstant());
     84             assertEquals(expectedPeriod1RawOffset, denverPeriod1.getRawOffsetMillis());
     85             assertEquals(expectedPeriod1DstOffset, denverPeriod1.getDstOffsetMillis());
     86             assertEquals(expectedPeriod1Name, denverPeriod1.getName());
     87 
     88             ZoneOffsetPeriod boisePeriod1 = ZoneOffsetPeriod.create(
     89                     timeZoneNames, boiseTz, expectedPeriod1Start, endInstant);
     90             assertEquals(expectedPeriod1Start, boisePeriod1.getStartInstant());
     91             assertEquals(expectedPeriod1End, boisePeriod1.getEndInstant());
     92             assertEquals(expectedPeriod1End, boisePeriod1.getEndInstant());
     93             assertEquals(expectedPeriod1RawOffset, boisePeriod1.getRawOffsetMillis());
     94             assertEquals(expectedPeriod1DstOffset, boisePeriod1.getDstOffsetMillis());
     95             assertEquals(expectedPeriod1Name, boisePeriod1.getName());
     96 
     97             assertEquals(denverPeriod1, boisePeriod1);
     98         }
     99     }
    100 
    101     @Test
    102     public void testEqualPeriods_withoutDstTransitions() {
    103         TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(ULocale.ENGLISH);
    104 
    105         Instant startInstant = Instant.EPOCH; /* 1970-01-01T00:00:00Z */
    106         // endInstant is an arbitrary "ceiling" time value for a period if there are no transitions.
    107         // Since there are no transitions this will be the end of the period.
    108         Instant endInstant = Instant.ofEpochSecond(631152000L); /* 1990-01-01T00:00:00Z */
    109 
    110         // These two zones have not observed DST but have different offsets and names for their
    111         // local time.
    112         BasicTimeZone honoluluTz = (BasicTimeZone) TimeZone.getTimeZone("Pacific/Honolulu");
    113         int honoluluRawOffset = -36000000;
    114         int honoluluDstOffset = 0;
    115         // ICU doesn't have a name for this zone for 1970.
    116         String honoluluName = null;
    117         ZoneOffsetPeriod honoluluPeriod =
    118                 ZoneOffsetPeriod.create(timeZoneNames, honoluluTz, startInstant, endInstant);
    119         assertEquals(startInstant, honoluluPeriod.getStartInstant());
    120         assertEquals(endInstant, honoluluPeriod.getEndInstant());
    121         assertEquals(honoluluRawOffset, honoluluPeriod.getRawOffsetMillis());
    122         assertEquals(honoluluDstOffset, honoluluPeriod.getDstOffsetMillis());
    123         assertEquals(honoluluName, honoluluPeriod.getName());
    124 
    125         BasicTimeZone phoenixTz = (BasicTimeZone) TimeZone.getTimeZone("America/Phoenix");
    126         int phoenixRawOffset = -25200000;
    127         int phoenixDstOffset = 0;
    128         String phoenixName = "Mountain Standard Time";
    129         ZoneOffsetPeriod phoenixPeriod =
    130                 ZoneOffsetPeriod.create(timeZoneNames, phoenixTz, startInstant, endInstant);
    131         assertEquals(startInstant, phoenixPeriod.getStartInstant());
    132         assertEquals(endInstant, phoenixPeriod.getEndInstant());
    133         assertEquals(phoenixRawOffset, phoenixPeriod.getRawOffsetMillis());
    134         assertEquals(phoenixDstOffset, phoenixPeriod.getDstOffsetMillis());
    135         assertEquals(phoenixName, phoenixPeriod.getName());
    136 
    137         assertFalse(honoluluPeriod.equals(phoenixPeriod));
    138     }
    139 
    140     @Test
    141     public void testSplitAtTime() {
    142         TimeZoneNames timeZoneNames = TimeZoneNames.getInstance(ULocale.ENGLISH);
    143 
    144         // This zone does not observe DST.
    145         BasicTimeZone honoluluTz = (BasicTimeZone) TimeZone.getTimeZone("Pacific/Honolulu");
    146         int honoluluRawOffset = -36000000;
    147         int honoluluDstOffset = 0;
    148         // ICU doesn't have a display name for the zone in 1970....
    149         String honoluluOldName = null;
    150         // ... but it does in 1990.
    151         String honoluluNewName = "Hawaii-Aleutian Standard Time";
    152 
    153         Instant startInstant = Instant.EPOCH; /* 1970-01-01T00:00:00Z */
    154         // endInstant is an arbitrary "ceiling" time value for a period if there are no transitions.
    155         // Since there are no transitions for the zone this will be the end of the period.
    156         Instant endInstant = Instant.ofEpochSecond(631152000L); /* 1990-01-01T00:00:00Z */
    157 
    158         ZoneOffsetPeriod honoluluPeriod =
    159                 ZoneOffsetPeriod.create(timeZoneNames, honoluluTz, startInstant, endInstant);
    160 
    161         // Split at an arbitrary point.
    162         Instant partitionInstant = Instant.ofEpochSecond(500000000L);
    163         ZoneOffsetPeriod[] shards = ZoneOffsetPeriod.splitAtTime(
    164                 honoluluPeriod, timeZoneNames, honoluluTz, partitionInstant);
    165 
    166         // Check the properties.
    167         assertEquals(startInstant, shards[0].getStartInstant());
    168         assertEquals(partitionInstant, shards[0].getEndInstant());
    169         assertEquals(honoluluRawOffset, shards[0].getRawOffsetMillis());
    170         assertEquals(honoluluDstOffset, shards[0].getDstOffsetMillis());
    171         assertEquals(honoluluOldName, shards[0].getName());
    172 
    173         assertEquals(partitionInstant, shards[1].getStartInstant());
    174         assertEquals(endInstant, shards[1].getEndInstant());
    175         assertEquals(honoluluRawOffset, shards[1].getRawOffsetMillis());
    176         assertEquals(honoluluDstOffset, shards[1].getDstOffsetMillis());
    177         assertEquals(honoluluNewName, shards[1].getName());
    178     }
    179 }