1 /* 2 * Copyright (c) 2012, 2013, 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 /* 25 * This file is available under and governed by the GNU General Public 26 * License version 2 only, as published by the Free Software Foundation. 27 * However, the following notice accompanied the original version of this 28 * file: 29 * 30 * Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos 31 * 32 * All rights reserved. 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions are met: 36 * 37 * * Redistributions of source code must retain the above copyright notice, 38 * this list of conditions and the following disclaimer. 39 * 40 * * Redistributions in binary form must reproduce the above copyright notice, 41 * this list of conditions and the following disclaimer in the documentation 42 * and/or other materials provided with the distribution. 43 * 44 * * Neither the name of JSR-310 nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 package test.java.time; 61 62 import static org.testng.Assert.assertEquals; 63 import static org.testng.Assert.assertFalse; 64 import static org.testng.Assert.assertNotNull; 65 import static org.testng.Assert.assertTrue; 66 67 import java.lang.reflect.Field; 68 import java.lang.reflect.Modifier; 69 import java.time.DateTimeException; 70 import java.time.Instant; 71 import java.time.LocalDateTime; 72 import java.time.ZoneId; 73 import java.time.ZoneOffset; 74 import java.time.ZonedDateTime; 75 import java.time.format.TextStyle; 76 import java.time.zone.ZoneOffsetTransition; 77 import java.time.zone.ZoneRules; 78 import java.time.zone.ZoneRulesException; 79 import java.util.List; 80 import java.util.Locale; 81 import java.util.SimpleTimeZone; 82 import java.util.TimeZone; 83 84 import org.testng.annotations.Test; 85 86 /** 87 * Test ZoneId. 88 */ 89 @Test 90 public class TestZoneId extends AbstractTest { 91 92 private static final int OVERLAP = 2; 93 private static final int GAP = 0; 94 95 //----------------------------------------------------------------------- 96 // Basics 97 //----------------------------------------------------------------------- test_immutable()98 public void test_immutable() { 99 // cannot use standard test as ZoneId is abstract 100 Class<ZoneId> cls = ZoneId.class; 101 assertTrue(Modifier.isPublic(cls.getModifiers())); 102 Field[] fields = cls.getDeclaredFields(); 103 for (Field field : fields) { 104 if (Modifier.isStatic(field.getModifiers()) == false) { 105 assertTrue(Modifier.isPrivate(field.getModifiers())); 106 assertTrue(Modifier.isFinal(field.getModifiers()) || 107 (Modifier.isVolatile(field.getModifiers()) && Modifier.isTransient(field.getModifiers()))); 108 } 109 } 110 } 111 112 //----------------------------------------------------------------------- 113 // UTC 114 //----------------------------------------------------------------------- test_constant_UTC()115 public void test_constant_UTC() { 116 ZoneId test = ZoneOffset.UTC; 117 assertEquals(test.getId(), "Z"); 118 assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), "Z"); 119 assertEquals(test.getRules().isFixedOffset(), true); 120 assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), ZoneOffset.UTC); 121 checkOffset(test.getRules(), createLDT(2008, 6, 30), ZoneOffset.UTC, 1); 122 } 123 124 //----------------------------------------------------------------------- 125 // system default 126 //----------------------------------------------------------------------- test_systemDefault()127 public void test_systemDefault() { 128 ZoneId test = ZoneId.systemDefault(); 129 assertEquals(test.getId(), TimeZone.getDefault().getID()); 130 } 131 132 @Test(expectedExceptions = DateTimeException.class) test_systemDefault_unableToConvert_badFormat()133 public void test_systemDefault_unableToConvert_badFormat() { 134 TimeZone current = TimeZone.getDefault(); 135 try { 136 TimeZone.setDefault(new SimpleTimeZone(127, "Something Weird")); 137 ZoneId.systemDefault(); 138 } finally { 139 TimeZone.setDefault(current); 140 } 141 } 142 143 @Test(expectedExceptions = ZoneRulesException.class) test_systemDefault_unableToConvert_unknownId()144 public void test_systemDefault_unableToConvert_unknownId() { 145 TimeZone current = TimeZone.getDefault(); 146 try { 147 TimeZone.setDefault(new SimpleTimeZone(127, "SomethingWeird")); 148 ZoneId.systemDefault(); 149 } finally { 150 TimeZone.setDefault(current); 151 } 152 } 153 154 //----------------------------------------------------------------------- 155 // Europe/London 156 //----------------------------------------------------------------------- test_London()157 public void test_London() { 158 ZoneId test = ZoneId.of("Europe/London"); 159 assertEquals(test.getId(), "Europe/London"); 160 assertEquals(test.getRules().isFixedOffset(), false); 161 } 162 test_London_getOffset()163 public void test_London_getOffset() { 164 ZoneId test = ZoneId.of("Europe/London"); 165 assertEquals(test.getRules().getOffset(createInstant(2008, 1, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 166 assertEquals(test.getRules().getOffset(createInstant(2008, 2, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 167 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 168 assertEquals(test.getRules().getOffset(createInstant(2008, 4, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 169 assertEquals(test.getRules().getOffset(createInstant(2008, 5, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 170 assertEquals(test.getRules().getOffset(createInstant(2008, 6, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 171 assertEquals(test.getRules().getOffset(createInstant(2008, 7, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 172 assertEquals(test.getRules().getOffset(createInstant(2008, 8, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 173 assertEquals(test.getRules().getOffset(createInstant(2008, 9, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 174 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 175 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 176 assertEquals(test.getRules().getOffset(createInstant(2008, 12, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 177 } 178 test_London_getOffset_toDST()179 public void test_London_getOffset_toDST() { 180 ZoneId test = ZoneId.of("Europe/London"); 181 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 24, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 182 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 25, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 183 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 26, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 184 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 27, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 185 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 28, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 186 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 29, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 187 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 188 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 31, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 189 // cutover at 01:00Z 190 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, 0, 59, 59, 999999999, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 191 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, 1, 0, 0, 0, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 192 } 193 test_London_getOffset_fromDST()194 public void test_London_getOffset_fromDST() { 195 ZoneId test = ZoneId.of("Europe/London"); 196 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 24, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 197 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 25, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 198 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 26, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 199 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 27, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 200 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 28, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 201 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 29, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 202 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 30, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 203 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 31, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 204 // cutover at 01:00Z 205 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 26, 0, 59, 59, 999999999, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 206 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 26, 1, 0, 0, 0, ZoneOffset.UTC)), ZoneOffset.ofHours(0)); 207 } 208 test_London_getOffsetInfo()209 public void test_London_getOffsetInfo() { 210 ZoneId test = ZoneId.of("Europe/London"); 211 checkOffset(test.getRules(), createLDT(2008, 1, 1), ZoneOffset.ofHours(0), 1); 212 checkOffset(test.getRules(), createLDT(2008, 2, 1), ZoneOffset.ofHours(0), 1); 213 checkOffset(test.getRules(), createLDT(2008, 3, 1), ZoneOffset.ofHours(0), 1); 214 checkOffset(test.getRules(), createLDT(2008, 4, 1), ZoneOffset.ofHours(1), 1); 215 checkOffset(test.getRules(), createLDT(2008, 5, 1), ZoneOffset.ofHours(1), 1); 216 checkOffset(test.getRules(), createLDT(2008, 6, 1), ZoneOffset.ofHours(1), 1); 217 checkOffset(test.getRules(), createLDT(2008, 7, 1), ZoneOffset.ofHours(1), 1); 218 checkOffset(test.getRules(), createLDT(2008, 8, 1), ZoneOffset.ofHours(1), 1); 219 checkOffset(test.getRules(), createLDT(2008, 9, 1), ZoneOffset.ofHours(1), 1); 220 checkOffset(test.getRules(), createLDT(2008, 10, 1), ZoneOffset.ofHours(1), 1); 221 checkOffset(test.getRules(), createLDT(2008, 11, 1), ZoneOffset.ofHours(0), 1); 222 checkOffset(test.getRules(), createLDT(2008, 12, 1), ZoneOffset.ofHours(0), 1); 223 } 224 test_London_getOffsetInfo_toDST()225 public void test_London_getOffsetInfo_toDST() { 226 ZoneId test = ZoneId.of("Europe/London"); 227 checkOffset(test.getRules(), createLDT(2008, 3, 24), ZoneOffset.ofHours(0), 1); 228 checkOffset(test.getRules(), createLDT(2008, 3, 25), ZoneOffset.ofHours(0), 1); 229 checkOffset(test.getRules(), createLDT(2008, 3, 26), ZoneOffset.ofHours(0), 1); 230 checkOffset(test.getRules(), createLDT(2008, 3, 27), ZoneOffset.ofHours(0), 1); 231 checkOffset(test.getRules(), createLDT(2008, 3, 28), ZoneOffset.ofHours(0), 1); 232 checkOffset(test.getRules(), createLDT(2008, 3, 29), ZoneOffset.ofHours(0), 1); 233 checkOffset(test.getRules(), createLDT(2008, 3, 30), ZoneOffset.ofHours(0), 1); 234 checkOffset(test.getRules(), createLDT(2008, 3, 31), ZoneOffset.ofHours(1), 1); 235 // cutover at 01:00Z 236 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 30, 0, 59, 59, 999999999), ZoneOffset.ofHours(0), 1); 237 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 30, 1, 30, 0, 0), ZoneOffset.ofHours(0), GAP); 238 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 30, 2, 0, 0, 0), ZoneOffset.ofHours(1), 1); 239 } 240 test_London_getOffsetInfo_fromDST()241 public void test_London_getOffsetInfo_fromDST() { 242 ZoneId test = ZoneId.of("Europe/London"); 243 checkOffset(test.getRules(), createLDT(2008, 10, 24), ZoneOffset.ofHours(1), 1); 244 checkOffset(test.getRules(), createLDT(2008, 10, 25), ZoneOffset.ofHours(1), 1); 245 checkOffset(test.getRules(), createLDT(2008, 10, 26), ZoneOffset.ofHours(1), 1); 246 checkOffset(test.getRules(), createLDT(2008, 10, 27), ZoneOffset.ofHours(0), 1); 247 checkOffset(test.getRules(), createLDT(2008, 10, 28), ZoneOffset.ofHours(0), 1); 248 checkOffset(test.getRules(), createLDT(2008, 10, 29), ZoneOffset.ofHours(0), 1); 249 checkOffset(test.getRules(), createLDT(2008, 10, 30), ZoneOffset.ofHours(0), 1); 250 checkOffset(test.getRules(), createLDT(2008, 10, 31), ZoneOffset.ofHours(0), 1); 251 // cutover at 01:00Z 252 checkOffset(test.getRules(), LocalDateTime.of(2008, 10, 26, 0, 59, 59, 999999999), ZoneOffset.ofHours(1), 1); 253 checkOffset(test.getRules(), LocalDateTime.of(2008, 10, 26, 1, 30, 0, 0), ZoneOffset.ofHours(1), OVERLAP); 254 checkOffset(test.getRules(), LocalDateTime.of(2008, 10, 26, 2, 0, 0, 0), ZoneOffset.ofHours(0), 1); 255 } 256 test_London_getOffsetInfo_gap()257 public void test_London_getOffsetInfo_gap() { 258 ZoneId test = ZoneId.of("Europe/London"); 259 final LocalDateTime dateTime = LocalDateTime.of(2008, 3, 30, 1, 0, 0, 0); 260 ZoneOffsetTransition trans = checkOffset(test.getRules(), dateTime, ZoneOffset.ofHours(0), GAP); 261 assertEquals(trans.isGap(), true); 262 assertEquals(trans.isOverlap(), false); 263 assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(0)); 264 assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(1)); 265 assertEquals(trans.getInstant(), dateTime.toInstant(ZoneOffset.UTC)); 266 assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(2008, 3, 30, 1, 0)); 267 assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(2008, 3, 30, 2, 0)); 268 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-1)), false); 269 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(0)), false); 270 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(1)), false); 271 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(2)), false); 272 assertEquals(trans.toString(), "Transition[Gap at 2008-03-30T01:00Z to +01:00]"); 273 274 assertFalse(trans.equals(null)); 275 assertFalse(trans.equals(ZoneOffset.ofHours(0))); 276 assertTrue(trans.equals(trans)); 277 278 final ZoneOffsetTransition otherTrans = test.getRules().getTransition(dateTime); 279 assertTrue(trans.equals(otherTrans)); 280 assertEquals(trans.hashCode(), otherTrans.hashCode()); 281 } 282 test_London_getOffsetInfo_overlap()283 public void test_London_getOffsetInfo_overlap() { 284 ZoneId test = ZoneId.of("Europe/London"); 285 final LocalDateTime dateTime = LocalDateTime.of(2008, 10, 26, 1, 0, 0, 0); 286 ZoneOffsetTransition trans = checkOffset(test.getRules(), dateTime, ZoneOffset.ofHours(1), OVERLAP); 287 assertEquals(trans.isGap(), false); 288 assertEquals(trans.isOverlap(), true); 289 assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(1)); 290 assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(0)); 291 assertEquals(trans.getInstant(), dateTime.toInstant(ZoneOffset.UTC)); 292 assertEquals(trans.getDateTimeBefore(), LocalDateTime.of(2008, 10, 26, 2, 0)); 293 assertEquals(trans.getDateTimeAfter(), LocalDateTime.of(2008, 10, 26, 1, 0)); 294 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-1)), false); 295 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(0)), true); 296 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(1)), true); 297 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(2)), false); 298 assertEquals(trans.toString(), "Transition[Overlap at 2008-10-26T02:00+01:00 to Z]"); 299 300 assertFalse(trans.equals(null)); 301 assertFalse(trans.equals(ZoneOffset.ofHours(1))); 302 assertTrue(trans.equals(trans)); 303 304 final ZoneOffsetTransition otherTrans = test.getRules().getTransition(dateTime); 305 assertTrue(trans.equals(otherTrans)); 306 assertEquals(trans.hashCode(), otherTrans.hashCode()); 307 } 308 309 //----------------------------------------------------------------------- 310 // Europe/Paris 311 //----------------------------------------------------------------------- test_Paris()312 public void test_Paris() { 313 ZoneId test = ZoneId.of("Europe/Paris"); 314 assertEquals(test.getId(), "Europe/Paris"); 315 assertEquals(test.getRules().isFixedOffset(), false); 316 } 317 test_Paris_getOffset()318 public void test_Paris_getOffset() { 319 ZoneId test = ZoneId.of("Europe/Paris"); 320 assertEquals(test.getRules().getOffset(createInstant(2008, 1, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 321 assertEquals(test.getRules().getOffset(createInstant(2008, 2, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 322 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 323 assertEquals(test.getRules().getOffset(createInstant(2008, 4, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 324 assertEquals(test.getRules().getOffset(createInstant(2008, 5, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 325 assertEquals(test.getRules().getOffset(createInstant(2008, 6, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 326 assertEquals(test.getRules().getOffset(createInstant(2008, 7, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 327 assertEquals(test.getRules().getOffset(createInstant(2008, 8, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 328 assertEquals(test.getRules().getOffset(createInstant(2008, 9, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 329 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 330 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 331 assertEquals(test.getRules().getOffset(createInstant(2008, 12, 1, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 332 } 333 test_Paris_getOffset_toDST()334 public void test_Paris_getOffset_toDST() { 335 ZoneId test = ZoneId.of("Europe/Paris"); 336 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 24, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 337 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 25, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 338 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 26, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 339 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 27, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 340 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 28, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 341 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 29, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 342 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 343 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 31, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 344 // cutover at 01:00Z 345 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, 0, 59, 59, 999999999, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 346 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 30, 1, 0, 0, 0, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 347 } 348 test_Paris_getOffset_fromDST()349 public void test_Paris_getOffset_fromDST() { 350 ZoneId test = ZoneId.of("Europe/Paris"); 351 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 24, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 352 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 25, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 353 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 26, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 354 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 27, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 355 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 28, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 356 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 29, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 357 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 30, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 358 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 31, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 359 // cutover at 01:00Z 360 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 26, 0, 59, 59, 999999999, ZoneOffset.UTC)), ZoneOffset.ofHours(2)); 361 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 26, 1, 0, 0, 0, ZoneOffset.UTC)), ZoneOffset.ofHours(1)); 362 } 363 test_Paris_getOffsetInfo()364 public void test_Paris_getOffsetInfo() { 365 ZoneId test = ZoneId.of("Europe/Paris"); 366 checkOffset(test.getRules(), createLDT(2008, 1, 1), ZoneOffset.ofHours(1), 1); 367 checkOffset(test.getRules(), createLDT(2008, 2, 1), ZoneOffset.ofHours(1), 1); 368 checkOffset(test.getRules(), createLDT(2008, 3, 1), ZoneOffset.ofHours(1), 1); 369 checkOffset(test.getRules(), createLDT(2008, 4, 1), ZoneOffset.ofHours(2), 1); 370 checkOffset(test.getRules(), createLDT(2008, 5, 1), ZoneOffset.ofHours(2), 1); 371 checkOffset(test.getRules(), createLDT(2008, 6, 1), ZoneOffset.ofHours(2), 1); 372 checkOffset(test.getRules(), createLDT(2008, 7, 1), ZoneOffset.ofHours(2), 1); 373 checkOffset(test.getRules(), createLDT(2008, 8, 1), ZoneOffset.ofHours(2), 1); 374 checkOffset(test.getRules(), createLDT(2008, 9, 1), ZoneOffset.ofHours(2), 1); 375 checkOffset(test.getRules(), createLDT(2008, 10, 1), ZoneOffset.ofHours(2), 1); 376 checkOffset(test.getRules(), createLDT(2008, 11, 1), ZoneOffset.ofHours(1), 1); 377 checkOffset(test.getRules(), createLDT(2008, 12, 1), ZoneOffset.ofHours(1), 1); 378 } 379 test_Paris_getOffsetInfo_toDST()380 public void test_Paris_getOffsetInfo_toDST() { 381 ZoneId test = ZoneId.of("Europe/Paris"); 382 checkOffset(test.getRules(), createLDT(2008, 3, 24), ZoneOffset.ofHours(1), 1); 383 checkOffset(test.getRules(), createLDT(2008, 3, 25), ZoneOffset.ofHours(1), 1); 384 checkOffset(test.getRules(), createLDT(2008, 3, 26), ZoneOffset.ofHours(1), 1); 385 checkOffset(test.getRules(), createLDT(2008, 3, 27), ZoneOffset.ofHours(1), 1); 386 checkOffset(test.getRules(), createLDT(2008, 3, 28), ZoneOffset.ofHours(1), 1); 387 checkOffset(test.getRules(), createLDT(2008, 3, 29), ZoneOffset.ofHours(1), 1); 388 checkOffset(test.getRules(), createLDT(2008, 3, 30), ZoneOffset.ofHours(1), 1); 389 checkOffset(test.getRules(), createLDT(2008, 3, 31), ZoneOffset.ofHours(2), 1); 390 // cutover at 01:00Z which is 02:00+01:00(local Paris time) 391 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 30, 1, 59, 59, 999999999), ZoneOffset.ofHours(1), 1); 392 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 30, 2, 30, 0, 0), ZoneOffset.ofHours(1), GAP); 393 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 30, 3, 0, 0, 0), ZoneOffset.ofHours(2), 1); 394 } 395 test_Paris_getOffsetInfo_fromDST()396 public void test_Paris_getOffsetInfo_fromDST() { 397 ZoneId test = ZoneId.of("Europe/Paris"); 398 checkOffset(test.getRules(), createLDT(2008, 10, 24), ZoneOffset.ofHours(2), 1); 399 checkOffset(test.getRules(), createLDT(2008, 10, 25), ZoneOffset.ofHours(2), 1); 400 checkOffset(test.getRules(), createLDT(2008, 10, 26), ZoneOffset.ofHours(2), 1); 401 checkOffset(test.getRules(), createLDT(2008, 10, 27), ZoneOffset.ofHours(1), 1); 402 checkOffset(test.getRules(), createLDT(2008, 10, 28), ZoneOffset.ofHours(1), 1); 403 checkOffset(test.getRules(), createLDT(2008, 10, 29), ZoneOffset.ofHours(1), 1); 404 checkOffset(test.getRules(), createLDT(2008, 10, 30), ZoneOffset.ofHours(1), 1); 405 checkOffset(test.getRules(), createLDT(2008, 10, 31), ZoneOffset.ofHours(1), 1); 406 // cutover at 01:00Z which is 02:00+01:00(local Paris time) 407 checkOffset(test.getRules(), LocalDateTime.of(2008, 10, 26, 1, 59, 59, 999999999), ZoneOffset.ofHours(2), 1); 408 checkOffset(test.getRules(), LocalDateTime.of(2008, 10, 26, 2, 30, 0, 0), ZoneOffset.ofHours(2), OVERLAP); 409 checkOffset(test.getRules(), LocalDateTime.of(2008, 10, 26, 3, 0, 0, 0), ZoneOffset.ofHours(1), 1); 410 } 411 test_Paris_getOffsetInfo_gap()412 public void test_Paris_getOffsetInfo_gap() { 413 ZoneId test = ZoneId.of("Europe/Paris"); 414 final LocalDateTime dateTime = LocalDateTime.of(2008, 3, 30, 2, 0, 0, 0); 415 ZoneOffsetTransition trans = checkOffset(test.getRules(), dateTime, ZoneOffset.ofHours(1), GAP); 416 assertEquals(trans.isGap(), true); 417 assertEquals(trans.isOverlap(), false); 418 assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(1)); 419 assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(2)); 420 assertEquals(trans.getInstant(), createInstant(2008, 3, 30, 1, 0, 0, 0, ZoneOffset.UTC)); 421 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(0)), false); 422 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(1)), false); 423 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(2)), false); 424 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(3)), false); 425 assertEquals(trans.toString(), "Transition[Gap at 2008-03-30T02:00+01:00 to +02:00]"); 426 427 assertFalse(trans.equals(null)); 428 assertFalse(trans.equals(ZoneOffset.ofHours(1))); 429 assertTrue(trans.equals(trans)); 430 431 final ZoneOffsetTransition otherDis = test.getRules().getTransition(dateTime); 432 assertTrue(trans.equals(otherDis)); 433 assertEquals(trans.hashCode(), otherDis.hashCode()); 434 } 435 test_Paris_getOffsetInfo_overlap()436 public void test_Paris_getOffsetInfo_overlap() { 437 ZoneId test = ZoneId.of("Europe/Paris"); 438 final LocalDateTime dateTime = LocalDateTime.of(2008, 10, 26, 2, 0, 0, 0); 439 ZoneOffsetTransition trans = checkOffset(test.getRules(), dateTime, ZoneOffset.ofHours(2), OVERLAP); 440 assertEquals(trans.isGap(), false); 441 assertEquals(trans.isOverlap(), true); 442 assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(2)); 443 assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(1)); 444 assertEquals(trans.getInstant(), createInstant(2008, 10, 26, 1, 0, 0, 0, ZoneOffset.UTC)); 445 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(0)), false); 446 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(1)), true); 447 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(2)), true); 448 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(3)), false); 449 assertEquals(trans.toString(), "Transition[Overlap at 2008-10-26T03:00+02:00 to +01:00]"); 450 451 assertFalse(trans.equals(null)); 452 assertFalse(trans.equals(ZoneOffset.ofHours(2))); 453 assertTrue(trans.equals(trans)); 454 455 final ZoneOffsetTransition otherDis = test.getRules().getTransition(dateTime); 456 assertTrue(trans.equals(otherDis)); 457 assertEquals(trans.hashCode(), otherDis.hashCode()); 458 } 459 460 //----------------------------------------------------------------------- 461 // America/New_York 462 //----------------------------------------------------------------------- test_NewYork()463 public void test_NewYork() { 464 ZoneId test = ZoneId.of("America/New_York"); 465 assertEquals(test.getId(), "America/New_York"); 466 assertEquals(test.getRules().isFixedOffset(), false); 467 } 468 test_NewYork_getOffset()469 public void test_NewYork_getOffset() { 470 ZoneId test = ZoneId.of("America/New_York"); 471 ZoneOffset offset = ZoneOffset.ofHours(-5); 472 assertEquals(test.getRules().getOffset(createInstant(2008, 1, 1, offset)), ZoneOffset.ofHours(-5)); 473 assertEquals(test.getRules().getOffset(createInstant(2008, 2, 1, offset)), ZoneOffset.ofHours(-5)); 474 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 1, offset)), ZoneOffset.ofHours(-5)); 475 assertEquals(test.getRules().getOffset(createInstant(2008, 4, 1, offset)), ZoneOffset.ofHours(-4)); 476 assertEquals(test.getRules().getOffset(createInstant(2008, 5, 1, offset)), ZoneOffset.ofHours(-4)); 477 assertEquals(test.getRules().getOffset(createInstant(2008, 6, 1, offset)), ZoneOffset.ofHours(-4)); 478 assertEquals(test.getRules().getOffset(createInstant(2008, 7, 1, offset)), ZoneOffset.ofHours(-4)); 479 assertEquals(test.getRules().getOffset(createInstant(2008, 8, 1, offset)), ZoneOffset.ofHours(-4)); 480 assertEquals(test.getRules().getOffset(createInstant(2008, 9, 1, offset)), ZoneOffset.ofHours(-4)); 481 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 1, offset)), ZoneOffset.ofHours(-4)); 482 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 1, offset)), ZoneOffset.ofHours(-4)); 483 assertEquals(test.getRules().getOffset(createInstant(2008, 12, 1, offset)), ZoneOffset.ofHours(-5)); 484 assertEquals(test.getRules().getOffset(createInstant(2008, 1, 28, offset)), ZoneOffset.ofHours(-5)); 485 assertEquals(test.getRules().getOffset(createInstant(2008, 2, 28, offset)), ZoneOffset.ofHours(-5)); 486 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 28, offset)), ZoneOffset.ofHours(-4)); 487 assertEquals(test.getRules().getOffset(createInstant(2008, 4, 28, offset)), ZoneOffset.ofHours(-4)); 488 assertEquals(test.getRules().getOffset(createInstant(2008, 5, 28, offset)), ZoneOffset.ofHours(-4)); 489 assertEquals(test.getRules().getOffset(createInstant(2008, 6, 28, offset)), ZoneOffset.ofHours(-4)); 490 assertEquals(test.getRules().getOffset(createInstant(2008, 7, 28, offset)), ZoneOffset.ofHours(-4)); 491 assertEquals(test.getRules().getOffset(createInstant(2008, 8, 28, offset)), ZoneOffset.ofHours(-4)); 492 assertEquals(test.getRules().getOffset(createInstant(2008, 9, 28, offset)), ZoneOffset.ofHours(-4)); 493 assertEquals(test.getRules().getOffset(createInstant(2008, 10, 28, offset)), ZoneOffset.ofHours(-4)); 494 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 28, offset)), ZoneOffset.ofHours(-5)); 495 assertEquals(test.getRules().getOffset(createInstant(2008, 12, 28, offset)), ZoneOffset.ofHours(-5)); 496 } 497 test_NewYork_getOffset_toDST()498 public void test_NewYork_getOffset_toDST() { 499 ZoneId test = ZoneId.of("America/New_York"); 500 ZoneOffset offset = ZoneOffset.ofHours(-5); 501 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 8, offset)), ZoneOffset.ofHours(-5)); 502 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 9, offset)), ZoneOffset.ofHours(-5)); 503 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 10, offset)), ZoneOffset.ofHours(-4)); 504 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 11, offset)), ZoneOffset.ofHours(-4)); 505 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 12, offset)), ZoneOffset.ofHours(-4)); 506 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 13, offset)), ZoneOffset.ofHours(-4)); 507 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 14, offset)), ZoneOffset.ofHours(-4)); 508 // cutover at 02:00 local 509 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 9, 1, 59, 59, 999999999, offset)), ZoneOffset.ofHours(-5)); 510 assertEquals(test.getRules().getOffset(createInstant(2008, 3, 9, 2, 0, 0, 0, offset)), ZoneOffset.ofHours(-4)); 511 } 512 test_NewYork_getOffset_fromDST()513 public void test_NewYork_getOffset_fromDST() { 514 ZoneId test = ZoneId.of("America/New_York"); 515 ZoneOffset offset = ZoneOffset.ofHours(-4); 516 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 1, offset)), ZoneOffset.ofHours(-4)); 517 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 2, offset)), ZoneOffset.ofHours(-4)); 518 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 3, offset)), ZoneOffset.ofHours(-5)); 519 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 4, offset)), ZoneOffset.ofHours(-5)); 520 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 5, offset)), ZoneOffset.ofHours(-5)); 521 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 6, offset)), ZoneOffset.ofHours(-5)); 522 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 7, offset)), ZoneOffset.ofHours(-5)); 523 // cutover at 02:00 local 524 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 2, 1, 59, 59, 999999999, offset)), ZoneOffset.ofHours(-4)); 525 assertEquals(test.getRules().getOffset(createInstant(2008, 11, 2, 2, 0, 0, 0, offset)), ZoneOffset.ofHours(-5)); 526 } 527 test_NewYork_getOffsetInfo()528 public void test_NewYork_getOffsetInfo() { 529 ZoneId test = ZoneId.of("America/New_York"); 530 checkOffset(test.getRules(), createLDT(2008, 1, 1), ZoneOffset.ofHours(-5), 1); 531 checkOffset(test.getRules(), createLDT(2008, 2, 1), ZoneOffset.ofHours(-5), 1); 532 checkOffset(test.getRules(), createLDT(2008, 3, 1), ZoneOffset.ofHours(-5), 1); 533 checkOffset(test.getRules(), createLDT(2008, 4, 1), ZoneOffset.ofHours(-4), 1); 534 checkOffset(test.getRules(), createLDT(2008, 5, 1), ZoneOffset.ofHours(-4), 1); 535 checkOffset(test.getRules(), createLDT(2008, 6, 1), ZoneOffset.ofHours(-4), 1); 536 checkOffset(test.getRules(), createLDT(2008, 7, 1), ZoneOffset.ofHours(-4), 1); 537 checkOffset(test.getRules(), createLDT(2008, 8, 1), ZoneOffset.ofHours(-4), 1); 538 checkOffset(test.getRules(), createLDT(2008, 9, 1), ZoneOffset.ofHours(-4), 1); 539 checkOffset(test.getRules(), createLDT(2008, 10, 1), ZoneOffset.ofHours(-4), 1); 540 checkOffset(test.getRules(), createLDT(2008, 11, 1), ZoneOffset.ofHours(-4), 1); 541 checkOffset(test.getRules(), createLDT(2008, 12, 1), ZoneOffset.ofHours(-5), 1); 542 checkOffset(test.getRules(), createLDT(2008, 1, 28), ZoneOffset.ofHours(-5), 1); 543 checkOffset(test.getRules(), createLDT(2008, 2, 28), ZoneOffset.ofHours(-5), 1); 544 checkOffset(test.getRules(), createLDT(2008, 3, 28), ZoneOffset.ofHours(-4), 1); 545 checkOffset(test.getRules(), createLDT(2008, 4, 28), ZoneOffset.ofHours(-4), 1); 546 checkOffset(test.getRules(), createLDT(2008, 5, 28), ZoneOffset.ofHours(-4), 1); 547 checkOffset(test.getRules(), createLDT(2008, 6, 28), ZoneOffset.ofHours(-4), 1); 548 checkOffset(test.getRules(), createLDT(2008, 7, 28), ZoneOffset.ofHours(-4), 1); 549 checkOffset(test.getRules(), createLDT(2008, 8, 28), ZoneOffset.ofHours(-4), 1); 550 checkOffset(test.getRules(), createLDT(2008, 9, 28), ZoneOffset.ofHours(-4), 1); 551 checkOffset(test.getRules(), createLDT(2008, 10, 28), ZoneOffset.ofHours(-4), 1); 552 checkOffset(test.getRules(), createLDT(2008, 11, 28), ZoneOffset.ofHours(-5), 1); 553 checkOffset(test.getRules(), createLDT(2008, 12, 28), ZoneOffset.ofHours(-5), 1); 554 } 555 test_NewYork_getOffsetInfo_toDST()556 public void test_NewYork_getOffsetInfo_toDST() { 557 ZoneId test = ZoneId.of("America/New_York"); 558 checkOffset(test.getRules(), createLDT(2008, 3, 8), ZoneOffset.ofHours(-5), 1); 559 checkOffset(test.getRules(), createLDT(2008, 3, 9), ZoneOffset.ofHours(-5), 1); 560 checkOffset(test.getRules(), createLDT(2008, 3, 10), ZoneOffset.ofHours(-4), 1); 561 checkOffset(test.getRules(), createLDT(2008, 3, 11), ZoneOffset.ofHours(-4), 1); 562 checkOffset(test.getRules(), createLDT(2008, 3, 12), ZoneOffset.ofHours(-4), 1); 563 checkOffset(test.getRules(), createLDT(2008, 3, 13), ZoneOffset.ofHours(-4), 1); 564 checkOffset(test.getRules(), createLDT(2008, 3, 14), ZoneOffset.ofHours(-4), 1); 565 // cutover at 02:00 local 566 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 9, 1, 59, 59, 999999999), ZoneOffset.ofHours(-5), 1); 567 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 9, 2, 30, 0, 0), ZoneOffset.ofHours(-5), GAP); 568 checkOffset(test.getRules(), LocalDateTime.of(2008, 3, 9, 3, 0, 0, 0), ZoneOffset.ofHours(-4), 1); 569 } 570 test_NewYork_getOffsetInfo_fromDST()571 public void test_NewYork_getOffsetInfo_fromDST() { 572 ZoneId test = ZoneId.of("America/New_York"); 573 checkOffset(test.getRules(), createLDT(2008, 11, 1), ZoneOffset.ofHours(-4), 1); 574 checkOffset(test.getRules(), createLDT(2008, 11, 2), ZoneOffset.ofHours(-4), 1); 575 checkOffset(test.getRules(), createLDT(2008, 11, 3), ZoneOffset.ofHours(-5), 1); 576 checkOffset(test.getRules(), createLDT(2008, 11, 4), ZoneOffset.ofHours(-5), 1); 577 checkOffset(test.getRules(), createLDT(2008, 11, 5), ZoneOffset.ofHours(-5), 1); 578 checkOffset(test.getRules(), createLDT(2008, 11, 6), ZoneOffset.ofHours(-5), 1); 579 checkOffset(test.getRules(), createLDT(2008, 11, 7), ZoneOffset.ofHours(-5), 1); 580 // cutover at 02:00 local 581 checkOffset(test.getRules(), LocalDateTime.of(2008, 11, 2, 0, 59, 59, 999999999), ZoneOffset.ofHours(-4), 1); 582 checkOffset(test.getRules(), LocalDateTime.of(2008, 11, 2, 1, 30, 0, 0), ZoneOffset.ofHours(-4), OVERLAP); 583 checkOffset(test.getRules(), LocalDateTime.of(2008, 11, 2, 2, 0, 0, 0), ZoneOffset.ofHours(-5), 1); 584 } 585 test_NewYork_getOffsetInfo_gap()586 public void test_NewYork_getOffsetInfo_gap() { 587 ZoneId test = ZoneId.of("America/New_York"); 588 final LocalDateTime dateTime = LocalDateTime.of(2008, 3, 9, 2, 0, 0, 0); 589 ZoneOffsetTransition trans = checkOffset(test.getRules(), dateTime, ZoneOffset.ofHours(-5), GAP); 590 assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(-5)); 591 assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(-4)); 592 assertEquals(trans.getInstant(), createInstant(2008, 3, 9, 2, 0, 0, 0, ZoneOffset.ofHours(-5))); 593 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-6)), false); 594 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-5)), false); 595 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-4)), false); 596 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-3)), false); 597 assertEquals(trans.toString(), "Transition[Gap at 2008-03-09T02:00-05:00 to -04:00]"); 598 599 assertFalse(trans.equals(null)); 600 assertFalse(trans.equals(ZoneOffset.ofHours(-5))); 601 assertTrue(trans.equals(trans)); 602 603 final ZoneOffsetTransition otherTrans = test.getRules().getTransition(dateTime); 604 assertTrue(trans.equals(otherTrans)); 605 606 assertEquals(trans.hashCode(), otherTrans.hashCode()); 607 } 608 test_NewYork_getOffsetInfo_overlap()609 public void test_NewYork_getOffsetInfo_overlap() { 610 ZoneId test = ZoneId.of("America/New_York"); 611 final LocalDateTime dateTime = LocalDateTime.of(2008, 11, 2, 1, 0, 0, 0); 612 ZoneOffsetTransition trans = checkOffset(test.getRules(), dateTime, ZoneOffset.ofHours(-4), OVERLAP); 613 assertEquals(trans.getOffsetBefore(), ZoneOffset.ofHours(-4)); 614 assertEquals(trans.getOffsetAfter(), ZoneOffset.ofHours(-5)); 615 assertEquals(trans.getInstant(), createInstant(2008, 11, 2, 2, 0, 0, 0, ZoneOffset.ofHours(-4))); 616 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-1)), false); 617 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-5)), true); 618 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(-4)), true); 619 assertEquals(trans.isValidOffset(ZoneOffset.ofHours(2)), false); 620 assertEquals(trans.toString(), "Transition[Overlap at 2008-11-02T02:00-04:00 to -05:00]"); 621 622 assertFalse(trans.equals(null)); 623 assertFalse(trans.equals(ZoneOffset.ofHours(-4))); 624 assertTrue(trans.equals(trans)); 625 626 final ZoneOffsetTransition otherTrans = test.getRules().getTransition(dateTime); 627 assertTrue(trans.equals(otherTrans)); 628 629 assertEquals(trans.hashCode(), otherTrans.hashCode()); 630 } 631 632 //----------------------------------------------------------------------- 633 // getXxx() isXxx() 634 //----------------------------------------------------------------------- test_get_Tzdb()635 public void test_get_Tzdb() { 636 ZoneId test = ZoneId.of("Europe/London"); 637 assertEquals(test.getId(), "Europe/London"); 638 assertEquals(test.getRules().isFixedOffset(), false); 639 } 640 test_get_TzdbFixed()641 public void test_get_TzdbFixed() { 642 ZoneId test = ZoneId.of("+01:30"); 643 assertEquals(test.getId(), "+01:30"); 644 assertEquals(test.getRules().isFixedOffset(), true); 645 } 646 647 //----------------------------------------------------------------------- 648 //----------------------------------------------------------------------- 649 //----------------------------------------------------------------------- createInstant(int year, int month, int day, ZoneOffset offset)650 private Instant createInstant(int year, int month, int day, ZoneOffset offset) { 651 return LocalDateTime.of(year, month, day, 0, 0).toInstant(offset); 652 } 653 createInstant(int year, int month, int day, int hour, int min, int sec, int nano, ZoneOffset offset)654 private Instant createInstant(int year, int month, int day, int hour, int min, int sec, int nano, ZoneOffset offset) { 655 return LocalDateTime.of(year, month, day, hour, min, sec, nano).toInstant(offset); 656 } 657 createZDT(int year, int month, int day, int hour, int min, int sec, int nano, ZoneId zone)658 private ZonedDateTime createZDT(int year, int month, int day, int hour, int min, int sec, int nano, ZoneId zone) { 659 return LocalDateTime.of(year, month, day, hour, min, sec, nano).atZone(zone); 660 } 661 createLDT(int year, int month, int day)662 private LocalDateTime createLDT(int year, int month, int day) { 663 return LocalDateTime.of(year, month, day, 0, 0); 664 } 665 checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type)666 private ZoneOffsetTransition checkOffset(ZoneRules rules, LocalDateTime dateTime, ZoneOffset offset, int type) { 667 List<ZoneOffset> validOffsets = rules.getValidOffsets(dateTime); 668 assertEquals(validOffsets.size(), type); 669 assertEquals(rules.getOffset(dateTime), offset); 670 if (type == 1) { 671 assertEquals(validOffsets.get(0), offset); 672 return null; 673 } else { 674 ZoneOffsetTransition zot = rules.getTransition(dateTime); 675 assertNotNull(zot); 676 assertEquals(zot.isOverlap(), type == 2); 677 assertEquals(zot.isGap(), type == 0); 678 assertEquals(zot.isValidOffset(offset), type == 2); 679 return zot; 680 } 681 } 682 683 } 684