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 tck.java.time; 61 62 import static java.time.temporal.ChronoField.OFFSET_SECONDS; 63 import static org.testng.Assert.assertEquals; 64 import static org.testng.Assert.assertSame; 65 import static org.testng.Assert.assertTrue; 66 import static org.testng.Assert.fail; 67 68 import java.io.ByteArrayOutputStream; 69 import java.io.DataOutputStream; 70 import java.time.DateTimeException; 71 import java.time.Duration; 72 import java.time.Instant; 73 import java.time.LocalDate; 74 import java.time.LocalDateTime; 75 import java.time.LocalTime; 76 import java.time.ZoneId; 77 import java.time.ZoneOffset; 78 import java.time.ZonedDateTime; 79 import java.time.OffsetDateTime; 80 import java.time.temporal.ChronoField; 81 import java.time.temporal.JulianFields; 82 import java.time.temporal.TemporalAccessor; 83 import java.time.temporal.TemporalField; 84 import java.time.temporal.TemporalQueries; 85 import java.time.temporal.TemporalQuery; 86 import java.util.ArrayList; 87 import java.util.Arrays; 88 import java.util.List; 89 90 import org.testng.annotations.DataProvider; 91 import org.testng.annotations.Test; 92 93 /** 94 * Test ZoneOffset. 95 */ 96 @Test 97 public class TCKZoneOffset extends AbstractDateTimeTest { 98 99 //----------------------------------------------------------------------- 100 @Override samples()101 protected List<TemporalAccessor> samples() { 102 TemporalAccessor[] array = {ZoneOffset.ofHours(1), ZoneOffset.ofHoursMinutesSeconds(-5, -6, -30) }; 103 return Arrays.asList(array); 104 } 105 106 @Override validFields()107 protected List<TemporalField> validFields() { 108 TemporalField[] array = { 109 OFFSET_SECONDS, 110 }; 111 return Arrays.asList(array); 112 } 113 114 @Override invalidFields()115 protected List<TemporalField> invalidFields() { 116 List<TemporalField> list = new ArrayList<>(Arrays.<TemporalField>asList(ChronoField.values())); 117 list.removeAll(validFields()); 118 list.add(JulianFields.JULIAN_DAY); 119 list.add(JulianFields.MODIFIED_JULIAN_DAY); 120 list.add(JulianFields.RATA_DIE); 121 return list; 122 } 123 124 //----------------------------------------------------------------------- 125 // constants 126 //----------------------------------------------------------------------- 127 @Test test_constant_UTC()128 public void test_constant_UTC() { 129 ZoneOffset test = ZoneOffset.UTC; 130 doTestOffset(test, 0, 0, 0); 131 } 132 133 @Test test_constant_MIN()134 public void test_constant_MIN() { 135 ZoneOffset test = ZoneOffset.MIN; 136 doTestOffset(test, -18, 0, 0); 137 } 138 139 @Test test_constant_MAX()140 public void test_constant_MAX() { 141 ZoneOffset test = ZoneOffset.MAX; 142 doTestOffset(test, 18, 0, 0); 143 } 144 145 //----------------------------------------------------------------------- 146 // of(String) 147 //----------------------------------------------------------------------- 148 @Test test_factory_string_UTC()149 public void test_factory_string_UTC() { 150 String[] values = new String[] { 151 "Z", "+0", 152 "+00","+0000","+00:00","+000000","+00:00:00", 153 "-00","-0000","-00:00","-000000","-00:00:00", 154 }; 155 for (int i = 0; i < values.length; i++) { 156 ZoneOffset test = ZoneOffset.of(values[i]); 157 assertSame(test, ZoneOffset.UTC); 158 } 159 } 160 161 @Test test_factory_string_invalid()162 public void test_factory_string_invalid() { 163 String[] values = new String[] { 164 "","A","B","C","D","E","F","G","H","I","J","K","L","M", 165 "N","O","P","Q","R","S","T","U","V","W","X","Y","ZZ", 166 "0", "+0:00","+00:0","+0:0", 167 "+000","+00000", 168 "+0:00:00","+00:0:00","+00:00:0","+0:0:0","+0:0:00","+00:0:0","+0:00:0", 169 "1", "+01_00","+01;00","+01@00","+01:AA", 170 "+19","+19:00","+18:01","+18:00:01","+1801","+180001", 171 "-0:00","-00:0","-0:0", 172 "-000","-00000", 173 "-0:00:00","-00:0:00","-00:00:0","-0:0:0","-0:0:00","-00:0:0","-0:00:0", 174 "-19","-19:00","-18:01","-18:00:01","-1801","-180001", 175 "-01_00","-01;00","-01@00","-01:AA", 176 "@01:00", 177 }; 178 for (int i = 0; i < values.length; i++) { 179 try { 180 ZoneOffset.of(values[i]); 181 fail("Should have failed:" + values[i]); 182 } catch (DateTimeException ex) { 183 // expected 184 } 185 } 186 } 187 188 @Test(expectedExceptions=NullPointerException.class) test_factory_string_null()189 public void test_factory_string_null() { 190 ZoneOffset.of((String) null); 191 } 192 193 //----------------------------------------------------------------------- 194 @Test test_factory_string_singleDigitHours()195 public void test_factory_string_singleDigitHours() { 196 for (int i = -9; i <= 9; i++) { 197 String str = (i < 0 ? "-" : "+") + Math.abs(i); 198 ZoneOffset test = ZoneOffset.of(str); 199 doTestOffset(test, i, 0, 0); 200 } 201 } 202 203 @Test test_factory_string_hours()204 public void test_factory_string_hours() { 205 for (int i = -18; i <= 18; i++) { 206 String str = (i < 0 ? "-" : "+") + Integer.toString(Math.abs(i) + 100).substring(1); 207 ZoneOffset test = ZoneOffset.of(str); 208 doTestOffset(test, i, 0, 0); 209 } 210 } 211 212 @Test test_factory_string_hours_minutes_noColon()213 public void test_factory_string_hours_minutes_noColon() { 214 for (int i = -17; i <= 17; i++) { 215 for (int j = -59; j <= 59; j++) { 216 if ((i < 0 && j <= 0) || (i > 0 && j >= 0) || i == 0) { 217 String str = (i < 0 || j < 0 ? "-" : "+") + 218 Integer.toString(Math.abs(i) + 100).substring(1) + 219 Integer.toString(Math.abs(j) + 100).substring(1); 220 ZoneOffset test = ZoneOffset.of(str); 221 doTestOffset(test, i, j, 0); 222 } 223 } 224 } 225 ZoneOffset test1 = ZoneOffset.of("-1800"); 226 doTestOffset(test1, -18, 0, 0); 227 ZoneOffset test2 = ZoneOffset.of("+1800"); 228 doTestOffset(test2, 18, 0, 0); 229 } 230 231 @Test test_factory_string_hours_minutes_colon()232 public void test_factory_string_hours_minutes_colon() { 233 for (int i = -17; i <= 17; i++) { 234 for (int j = -59; j <= 59; j++) { 235 if ((i < 0 && j <= 0) || (i > 0 && j >= 0) || i == 0) { 236 String str = (i < 0 || j < 0 ? "-" : "+") + 237 Integer.toString(Math.abs(i) + 100).substring(1) + ":" + 238 Integer.toString(Math.abs(j) + 100).substring(1); 239 ZoneOffset test = ZoneOffset.of(str); 240 doTestOffset(test, i, j, 0); 241 } 242 } 243 } 244 ZoneOffset test1 = ZoneOffset.of("-18:00"); 245 doTestOffset(test1, -18, 0, 0); 246 ZoneOffset test2 = ZoneOffset.of("+18:00"); 247 doTestOffset(test2, 18, 0, 0); 248 } 249 250 @Test test_factory_string_hours_minutes_seconds_noColon()251 public void test_factory_string_hours_minutes_seconds_noColon() { 252 for (int i = -17; i <= 17; i++) { 253 for (int j = -59; j <= 59; j++) { 254 for (int k = -59; k <= 59; k++) { 255 if ((i < 0 && j <= 0 && k <= 0) || (i > 0 && j >= 0 && k >= 0) || 256 (i == 0 && ((j < 0 && k <= 0) || (j > 0 && k >= 0) || j == 0))) { 257 String str = (i < 0 || j < 0 || k < 0 ? "-" : "+") + 258 Integer.toString(Math.abs(i) + 100).substring(1) + 259 Integer.toString(Math.abs(j) + 100).substring(1) + 260 Integer.toString(Math.abs(k) + 100).substring(1); 261 ZoneOffset test = ZoneOffset.of(str); 262 doTestOffset(test, i, j, k); 263 } 264 } 265 } 266 } 267 ZoneOffset test1 = ZoneOffset.of("-180000"); 268 doTestOffset(test1, -18, 0, 0); 269 ZoneOffset test2 = ZoneOffset.of("+180000"); 270 doTestOffset(test2, 18, 0, 0); 271 } 272 273 @Test test_factory_string_hours_minutes_seconds_colon()274 public void test_factory_string_hours_minutes_seconds_colon() { 275 for (int i = -17; i <= 17; i++) { 276 for (int j = -59; j <= 59; j++) { 277 for (int k = -59; k <= 59; k++) { 278 if ((i < 0 && j <= 0 && k <= 0) || (i > 0 && j >= 0 && k >= 0) || 279 (i == 0 && ((j < 0 && k <= 0) || (j > 0 && k >= 0) || j == 0))) { 280 String str = (i < 0 || j < 0 || k < 0 ? "-" : "+") + 281 Integer.toString(Math.abs(i) + 100).substring(1) + ":" + 282 Integer.toString(Math.abs(j) + 100).substring(1) + ":" + 283 Integer.toString(Math.abs(k) + 100).substring(1); 284 ZoneOffset test = ZoneOffset.of(str); 285 doTestOffset(test, i, j, k); 286 } 287 } 288 } 289 } 290 ZoneOffset test1 = ZoneOffset.of("-18:00:00"); 291 doTestOffset(test1, -18, 0, 0); 292 ZoneOffset test2 = ZoneOffset.of("+18:00:00"); 293 doTestOffset(test2, 18, 0, 0); 294 } 295 296 //----------------------------------------------------------------------- 297 @Test test_factory_int_hours()298 public void test_factory_int_hours() { 299 for (int i = -18; i <= 18; i++) { 300 ZoneOffset test = ZoneOffset.ofHours(i); 301 doTestOffset(test, i, 0, 0); 302 } 303 } 304 305 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_tooBig()306 public void test_factory_int_hours_tooBig() { 307 ZoneOffset.ofHours(19); 308 } 309 310 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_tooSmall()311 public void test_factory_int_hours_tooSmall() { 312 ZoneOffset.ofHours(-19); 313 } 314 315 //----------------------------------------------------------------------- 316 @Test test_factory_int_hours_minutes()317 public void test_factory_int_hours_minutes() { 318 for (int i = -17; i <= 17; i++) { 319 for (int j = -59; j <= 59; j++) { 320 if ((i < 0 && j <= 0) || (i > 0 && j >= 0) || i == 0) { 321 ZoneOffset test = ZoneOffset.ofHoursMinutes(i, j); 322 doTestOffset(test, i, j, 0); 323 } 324 } 325 } 326 ZoneOffset test1 = ZoneOffset.ofHoursMinutes(-18, 0); 327 doTestOffset(test1, -18, 0, 0); 328 ZoneOffset test2 = ZoneOffset.ofHoursMinutes(18, 0); 329 doTestOffset(test2, 18, 0, 0); 330 } 331 332 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_tooBig()333 public void test_factory_int_hours_minutes_tooBig() { 334 ZoneOffset.ofHoursMinutes(19, 0); 335 } 336 337 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_tooSmall()338 public void test_factory_int_hours_minutes_tooSmall() { 339 ZoneOffset.ofHoursMinutes(-19, 0); 340 } 341 342 //----------------------------------------------------------------------- 343 @Test test_factory_int_hours_minutes_seconds()344 public void test_factory_int_hours_minutes_seconds() { 345 for (int i = -17; i <= 17; i++) { 346 for (int j = -59; j <= 59; j++) { 347 for (int k = -59; k <= 59; k++) { 348 if ((i < 0 && j <= 0 && k <= 0) || (i > 0 && j >= 0 && k >= 0) || 349 (i == 0 && ((j < 0 && k <= 0) || (j > 0 && k >= 0) || j == 0))) { 350 ZoneOffset test = ZoneOffset.ofHoursMinutesSeconds(i, j, k); 351 doTestOffset(test, i, j, k); 352 } 353 } 354 } 355 } 356 ZoneOffset test1 = ZoneOffset.ofHoursMinutesSeconds(-18, 0, 0); 357 doTestOffset(test1, -18, 0, 0); 358 ZoneOffset test2 = ZoneOffset.ofHoursMinutesSeconds(18, 0, 0); 359 doTestOffset(test2, 18, 0, 0); 360 } 361 362 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_plusHoursMinusMinutes()363 public void test_factory_int_hours_minutes_seconds_plusHoursMinusMinutes() { 364 ZoneOffset.ofHoursMinutesSeconds(1, -1, 0); 365 } 366 367 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_plusHoursMinusSeconds()368 public void test_factory_int_hours_minutes_seconds_plusHoursMinusSeconds() { 369 ZoneOffset.ofHoursMinutesSeconds(1, 0, -1); 370 } 371 372 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_minusHoursPlusMinutes()373 public void test_factory_int_hours_minutes_seconds_minusHoursPlusMinutes() { 374 ZoneOffset.ofHoursMinutesSeconds(-1, 1, 0); 375 } 376 377 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_minusHoursPlusSeconds()378 public void test_factory_int_hours_minutes_seconds_minusHoursPlusSeconds() { 379 ZoneOffset.ofHoursMinutesSeconds(-1, 0, 1); 380 } 381 382 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_zeroHoursMinusMinutesPlusSeconds()383 public void test_factory_int_hours_minutes_seconds_zeroHoursMinusMinutesPlusSeconds() { 384 ZoneOffset.ofHoursMinutesSeconds(0, -1, 1); 385 } 386 387 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_zeroHoursPlusMinutesMinusSeconds()388 public void test_factory_int_hours_minutes_seconds_zeroHoursPlusMinutesMinusSeconds() { 389 ZoneOffset.ofHoursMinutesSeconds(0, 1, -1); 390 } 391 392 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_minutesTooLarge()393 public void test_factory_int_hours_minutes_seconds_minutesTooLarge() { 394 ZoneOffset.ofHoursMinutesSeconds(0, 60, 0); 395 } 396 397 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_minutesTooSmall()398 public void test_factory_int_hours_minutes_seconds_minutesTooSmall() { 399 ZoneOffset.ofHoursMinutesSeconds(0, -60, 0); 400 } 401 402 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_secondsTooLarge()403 public void test_factory_int_hours_minutes_seconds_secondsTooLarge() { 404 ZoneOffset.ofHoursMinutesSeconds(0, 0, 60); 405 } 406 407 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_secondsTooSmall()408 public void test_factory_int_hours_minutes_seconds_secondsTooSmall() { 409 ZoneOffset.ofHoursMinutesSeconds(0, 0, 60); 410 } 411 412 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_hoursTooBig()413 public void test_factory_int_hours_minutes_seconds_hoursTooBig() { 414 ZoneOffset.ofHoursMinutesSeconds(19, 0, 0); 415 } 416 417 @Test(expectedExceptions=DateTimeException.class) test_factory_int_hours_minutes_seconds_hoursTooSmall()418 public void test_factory_int_hours_minutes_seconds_hoursTooSmall() { 419 ZoneOffset.ofHoursMinutesSeconds(-19, 0, 0); 420 } 421 422 //----------------------------------------------------------------------- 423 @Test test_factory_ofTotalSeconds()424 public void test_factory_ofTotalSeconds() { 425 assertEquals(ZoneOffset.ofTotalSeconds(60 * 60 + 1), ZoneOffset.ofHoursMinutesSeconds(1, 0, 1)); 426 assertEquals(ZoneOffset.ofTotalSeconds(18 * 60 * 60), ZoneOffset.ofHours(18)); 427 assertEquals(ZoneOffset.ofTotalSeconds(-18 * 60 * 60), ZoneOffset.ofHours(-18)); 428 } 429 430 @Test(expectedExceptions=DateTimeException.class) test_factory_ofTotalSeconds_tooLarge()431 public void test_factory_ofTotalSeconds_tooLarge() { 432 ZoneOffset.ofTotalSeconds(18 * 60 * 60 + 1); 433 } 434 435 @Test(expectedExceptions=DateTimeException.class) test_factory_ofTotalSeconds_tooSmall()436 public void test_factory_ofTotalSeconds_tooSmall() { 437 ZoneOffset.ofTotalSeconds(-18 * 60 * 60 - 1); 438 } 439 440 //----------------------------------------------------------------------- 441 // from() 442 //----------------------------------------------------------------------- 443 @Test test_factory_CalendricalObject()444 public void test_factory_CalendricalObject() { 445 assertEquals(ZoneOffset.from(ZonedDateTime.of(LocalDateTime.of(LocalDate.of(2007, 7, 15), 446 LocalTime.of(17, 30)), ZoneOffset.ofHours(2))), ZoneOffset.ofHours(2)); 447 } 448 449 @Test(expectedExceptions=DateTimeException.class) test_factory_CalendricalObject_invalid_noDerive()450 public void test_factory_CalendricalObject_invalid_noDerive() { 451 ZoneOffset.from(LocalTime.of(12, 30)); 452 } 453 454 @Test(expectedExceptions=NullPointerException.class) test_factory_CalendricalObject_null()455 public void test_factory_CalendricalObject_null() { 456 ZoneOffset.from((TemporalAccessor) null); 457 } 458 459 //----------------------------------------------------------------------- 460 // getTotalSeconds() 461 //----------------------------------------------------------------------- 462 @Test test_getTotalSeconds()463 public void test_getTotalSeconds() { 464 ZoneOffset offset = ZoneOffset.ofTotalSeconds(60 * 60 + 1); 465 assertEquals(offset.getTotalSeconds(), 60 * 60 + 1); 466 } 467 468 //----------------------------------------------------------------------- 469 // getId() 470 //----------------------------------------------------------------------- 471 @Test test_getId()472 public void test_getId() { 473 ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 0, 0); 474 assertEquals(offset.getId(), "+01:00"); 475 offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 476 assertEquals(offset.getId(), "+01:02:03"); 477 offset = ZoneOffset.UTC; 478 assertEquals(offset.getId(), "Z"); 479 } 480 481 //----------------------------------------------------------------------- 482 // getRules() 483 //----------------------------------------------------------------------- 484 @Test test_getRules()485 public void test_getRules() { 486 ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 487 assertEquals(offset.getRules().isFixedOffset(), true); 488 assertEquals(offset.getRules().getOffset((Instant) null), offset); 489 assertEquals(offset.getRules().getDaylightSavings((Instant) null), Duration.ZERO); 490 assertEquals(offset.getRules().getStandardOffset((Instant) null), offset); 491 assertEquals(offset.getRules().nextTransition((Instant) null), null); 492 assertEquals(offset.getRules().previousTransition((Instant) null), null); 493 494 assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, offset), true); 495 assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, ZoneOffset.UTC), false); 496 assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, null), false); 497 assertEquals(offset.getRules().getOffset((LocalDateTime) null), offset); 498 assertEquals(offset.getRules().getValidOffsets((LocalDateTime) null), Arrays.asList(offset)); 499 assertEquals(offset.getRules().getTransition((LocalDateTime) null), null); 500 assertEquals(offset.getRules().getTransitions().size(), 0); 501 assertEquals(offset.getRules().getTransitionRules().size(), 0); 502 } 503 504 //----------------------------------------------------------------------- 505 // get(TemporalField) 506 //----------------------------------------------------------------------- 507 @Test test_get_TemporalField()508 public void test_get_TemporalField() { 509 assertEquals(ZoneOffset.UTC.get(OFFSET_SECONDS), 0); 510 assertEquals(ZoneOffset.ofHours(-2).get(OFFSET_SECONDS), -7200); 511 assertEquals(ZoneOffset.ofHoursMinutesSeconds(0, 1, 5).get(OFFSET_SECONDS), 65); 512 } 513 514 @Test test_getLong_TemporalField()515 public void test_getLong_TemporalField() { 516 assertEquals(ZoneOffset.UTC.getLong(OFFSET_SECONDS), 0); 517 assertEquals(ZoneOffset.ofHours(-2).getLong(OFFSET_SECONDS), -7200); 518 assertEquals(ZoneOffset.ofHoursMinutesSeconds(0, 1, 5).getLong(OFFSET_SECONDS), 65); 519 } 520 521 //----------------------------------------------------------------------- 522 // query(TemporalQuery) 523 //----------------------------------------------------------------------- 524 @DataProvider(name="query") data_query()525 Object[][] data_query() { 526 return new Object[][] { 527 {ZoneOffset.UTC, TemporalQueries.chronology(), null}, 528 {ZoneOffset.UTC, TemporalQueries.zoneId(), null}, 529 {ZoneOffset.UTC, TemporalQueries.precision(), null}, 530 {ZoneOffset.UTC, TemporalQueries.zone(), ZoneOffset.UTC}, 531 {ZoneOffset.UTC, TemporalQueries.offset(), ZoneOffset.UTC}, 532 {ZoneOffset.UTC, TemporalQueries.localDate(), null}, 533 {ZoneOffset.UTC, TemporalQueries.localTime(), null}, 534 }; 535 } 536 537 @Test(dataProvider="query") test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected)538 public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 539 assertEquals(temporal.query(query), expected); 540 } 541 542 @Test(dataProvider="query") test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected)543 public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) { 544 assertEquals(query.queryFrom(temporal), expected); 545 } 546 547 @Test(expectedExceptions=NullPointerException.class) test_query_null()548 public void test_query_null() { 549 ZoneOffset.UTC.query(null); 550 } 551 552 //----------------------------------------------------------------------- 553 // compareTo() 554 //----------------------------------------------------------------------- 555 @Test test_compareTo()556 public void test_compareTo() { 557 ZoneOffset offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 558 ZoneOffset offset2 = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4); 559 assertTrue(offset1.compareTo(offset2) > 0); 560 assertTrue(offset2.compareTo(offset1) < 0); 561 assertTrue(offset1.compareTo(offset1) == 0); 562 assertTrue(offset2.compareTo(offset2) == 0); 563 } 564 565 //----------------------------------------------------------------------- 566 // equals() / hashCode() 567 //----------------------------------------------------------------------- 568 @Test 569 public void test_equals() { 570 ZoneOffset offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 571 ZoneOffset offset2 = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4); 572 ZoneOffset offset2b = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4); 573 assertEquals(offset1.equals(offset2), false); 574 assertEquals(offset2.equals(offset1), false); 575 576 assertEquals(offset1.equals(offset1), true); 577 assertEquals(offset2.equals(offset2), true); 578 assertEquals(offset2.equals(offset2b), true); 579 580 assertEquals(offset1.hashCode() == offset1.hashCode(), true); 581 assertEquals(offset2.hashCode() == offset2.hashCode(), true); 582 assertEquals(offset2.hashCode() == offset2b.hashCode(), true); 583 } 584 585 //----------------------------------------------------------------------- 586 // adjustInto() 587 //----------------------------------------------------------------------- 588 @Test 589 public void test_adjustInto_ZonedDateTime() { 590 ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1); 591 for (String zoneId : ZoneId.getAvailableZoneIds()) { 592 //Do not change offset of ZonedDateTime after adjustInto() 593 ZonedDateTime zonedDateTime_target = ZonedDateTime.of(LocalDate.of(1909, 2, 2), LocalTime.of(10, 10, 10), ZoneId.of(zoneId)); 594 ZonedDateTime zonedDateTime_result = (ZonedDateTime)(base.adjustInto(zonedDateTime_target)); 595 assertEquals(zonedDateTime_target.getOffset(), zonedDateTime_result.getOffset()); 596 597 OffsetDateTime offsetDateTime_target = zonedDateTime_target.toOffsetDateTime(); 598 OffsetDateTime offsetDateTime_result = (OffsetDateTime)(base.adjustInto(offsetDateTime_target)); 599 assertEquals(base, offsetDateTime_result.getOffset()); 600 } 601 } 602 603 @Test 604 public void test_adjustInto_OffsetDateTime() { 605 ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1); 606 for (int i=-18; i<=18; i++) { 607 OffsetDateTime offsetDateTime_target = OffsetDateTime.of(LocalDate.of(1909, 2, 2), LocalTime.of(10, 10, 10), ZoneOffset.ofHours(i)); 608 OffsetDateTime offsetDateTime_result = (OffsetDateTime)base.adjustInto(offsetDateTime_target); 609 assertEquals(base, offsetDateTime_result.getOffset()); 610 611 //Do not change offset of ZonedDateTime after adjustInto() 612 ZonedDateTime zonedDateTime_target = offsetDateTime_target.toZonedDateTime(); 613 ZonedDateTime zonedDateTime_result = (ZonedDateTime)(base.adjustInto(zonedDateTime_target)); 614 assertEquals(zonedDateTime_target.getOffset(), zonedDateTime_result.getOffset()); 615 } 616 } 617 618 @Test(expectedExceptions=DateTimeException.class) 619 public void test_adjustInto_dateOnly() { 620 ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1); 621 base.adjustInto((LocalDate.of(1909, 2, 2))); 622 } 623 624 //----------------------------------------------------------------------- 625 // toString() 626 //----------------------------------------------------------------------- 627 @Test 628 public void test_toString() { 629 ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 0, 0); 630 assertEquals(offset.toString(), "+01:00"); 631 offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3); 632 assertEquals(offset.toString(), "+01:02:03"); 633 offset = ZoneOffset.UTC; 634 assertEquals(offset.toString(), "Z"); 635 } 636 637 //----------------------------------------------------------------------- 638 //----------------------------------------------------------------------- 639 //----------------------------------------------------------------------- 640 private void doTestOffset(ZoneOffset offset, int hours, int minutes, int seconds) { 641 assertEquals(offset.getTotalSeconds(), hours * 60 * 60 + minutes * 60 + seconds); 642 final String id; 643 if (hours == 0 && minutes == 0 && seconds == 0) { 644 id = "Z"; 645 } else { 646 String str = (hours < 0 || minutes < 0 || seconds < 0) ? "-" : "+"; 647 str += Integer.toString(Math.abs(hours) + 100).substring(1); 648 str += ":"; 649 str += Integer.toString(Math.abs(minutes) + 100).substring(1); 650 if (seconds != 0) { 651 str += ":"; 652 str += Integer.toString(Math.abs(seconds) + 100).substring(1); 653 } 654 id = str; 655 } 656 assertEquals(offset.getId(), id); 657 assertEquals(offset, ZoneOffset.ofHoursMinutesSeconds(hours, minutes, seconds)); 658 if (seconds == 0) { 659 assertEquals(offset, ZoneOffset.ofHoursMinutes(hours, minutes)); 660 if (minutes == 0) { 661 assertEquals(offset, ZoneOffset.ofHours(hours)); 662 } 663 } 664 assertEquals(ZoneOffset.of(id), offset); 665 assertEquals(offset.toString(), id); 666 } 667 668 } 669