1 /*
2  * Copyright (c) 2012, 2016, 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     @Test(expectedExceptions=DateTimeException.class)
test_factory_int_hours_minutes_seconds_minutesMinValue()423     public void test_factory_int_hours_minutes_seconds_minutesMinValue() {
424         ZoneOffset.ofHoursMinutesSeconds(0, Integer.MIN_VALUE, -1);
425     }
426 
427     @Test(expectedExceptions=DateTimeException.class)
test_factory_int_hours_minutes_seconds_secondsMinValue()428     public void test_factory_int_hours_minutes_seconds_secondsMinValue() {
429         ZoneOffset.ofHoursMinutesSeconds(0, 0, Integer.MIN_VALUE);
430     }
431 
432     @Test(expectedExceptions=DateTimeException.class)
test_factory_int_hours_minutes_seconds_minutesAndSecondsMinValue()433     public void test_factory_int_hours_minutes_seconds_minutesAndSecondsMinValue() {
434         ZoneOffset.ofHoursMinutesSeconds(0, Integer.MIN_VALUE, Integer.MIN_VALUE);
435     }
436 
437     //-----------------------------------------------------------------------
438     @Test
test_factory_ofTotalSeconds()439     public void test_factory_ofTotalSeconds() {
440         assertEquals(ZoneOffset.ofTotalSeconds(60 * 60 + 1), ZoneOffset.ofHoursMinutesSeconds(1, 0, 1));
441         assertEquals(ZoneOffset.ofTotalSeconds(18 * 60 * 60), ZoneOffset.ofHours(18));
442         assertEquals(ZoneOffset.ofTotalSeconds(-18 * 60 * 60), ZoneOffset.ofHours(-18));
443     }
444 
445     @Test(expectedExceptions=DateTimeException.class)
test_factory_ofTotalSeconds_tooLarge()446     public void test_factory_ofTotalSeconds_tooLarge() {
447         ZoneOffset.ofTotalSeconds(18 * 60 * 60 + 1);
448     }
449 
450     @Test(expectedExceptions=DateTimeException.class)
test_factory_ofTotalSeconds_tooSmall()451     public void test_factory_ofTotalSeconds_tooSmall() {
452         ZoneOffset.ofTotalSeconds(-18 * 60 * 60 - 1);
453     }
454 
455     @Test(expectedExceptions=DateTimeException.class)
test_factory_ofTotalSeconds_minValue()456     public void test_factory_ofTotalSeconds_minValue() {
457         ZoneOffset.ofTotalSeconds(Integer.MIN_VALUE);
458     }
459 
460     //-----------------------------------------------------------------------
461     // from()
462     //-----------------------------------------------------------------------
463     @Test
test_factory_CalendricalObject()464     public void test_factory_CalendricalObject() {
465         assertEquals(ZoneOffset.from(ZonedDateTime.of(LocalDateTime.of(LocalDate.of(2007, 7, 15),
466                 LocalTime.of(17, 30)), ZoneOffset.ofHours(2))), ZoneOffset.ofHours(2));
467     }
468 
469     @Test(expectedExceptions=DateTimeException.class)
test_factory_CalendricalObject_invalid_noDerive()470     public void test_factory_CalendricalObject_invalid_noDerive() {
471         ZoneOffset.from(LocalTime.of(12, 30));
472     }
473 
474     @Test(expectedExceptions=NullPointerException.class)
test_factory_CalendricalObject_null()475     public void test_factory_CalendricalObject_null() {
476         ZoneOffset.from((TemporalAccessor) null);
477     }
478 
479     //-----------------------------------------------------------------------
480     // getTotalSeconds()
481     //-----------------------------------------------------------------------
482     @Test
test_getTotalSeconds()483     public void test_getTotalSeconds() {
484         ZoneOffset offset = ZoneOffset.ofTotalSeconds(60 * 60 + 1);
485         assertEquals(offset.getTotalSeconds(), 60 * 60 + 1);
486     }
487 
488     //-----------------------------------------------------------------------
489     // getId()
490     //-----------------------------------------------------------------------
491     @Test
test_getId()492     public void test_getId() {
493         ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 0, 0);
494         assertEquals(offset.getId(), "+01:00");
495         offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
496         assertEquals(offset.getId(), "+01:02:03");
497         offset = ZoneOffset.UTC;
498         assertEquals(offset.getId(), "Z");
499     }
500 
501     //-----------------------------------------------------------------------
502     // getRules()
503     //-----------------------------------------------------------------------
504     @Test
test_getRules()505     public void test_getRules() {
506         ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
507         assertEquals(offset.getRules().isFixedOffset(), true);
508         assertEquals(offset.getRules().getOffset((Instant) null), offset);
509         assertEquals(offset.getRules().getDaylightSavings((Instant) null), Duration.ZERO);
510         assertEquals(offset.getRules().getStandardOffset((Instant) null), offset);
511         assertEquals(offset.getRules().nextTransition((Instant) null), null);
512         assertEquals(offset.getRules().previousTransition((Instant) null), null);
513 
514         assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, offset), true);
515         assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, ZoneOffset.UTC), false);
516         assertEquals(offset.getRules().isValidOffset((LocalDateTime) null, null), false);
517         assertEquals(offset.getRules().getOffset((LocalDateTime) null), offset);
518         assertEquals(offset.getRules().getValidOffsets((LocalDateTime) null), Arrays.asList(offset));
519         assertEquals(offset.getRules().getTransition((LocalDateTime) null), null);
520         assertEquals(offset.getRules().getTransitions().size(), 0);
521         assertEquals(offset.getRules().getTransitionRules().size(), 0);
522     }
523 
524     //-----------------------------------------------------------------------
525     // get(TemporalField)
526     //-----------------------------------------------------------------------
527     @Test
test_get_TemporalField()528     public void test_get_TemporalField() {
529         assertEquals(ZoneOffset.UTC.get(OFFSET_SECONDS), 0);
530         assertEquals(ZoneOffset.ofHours(-2).get(OFFSET_SECONDS), -7200);
531         assertEquals(ZoneOffset.ofHoursMinutesSeconds(0, 1, 5).get(OFFSET_SECONDS), 65);
532     }
533 
534     @Test
test_getLong_TemporalField()535     public void test_getLong_TemporalField() {
536         assertEquals(ZoneOffset.UTC.getLong(OFFSET_SECONDS), 0);
537         assertEquals(ZoneOffset.ofHours(-2).getLong(OFFSET_SECONDS), -7200);
538         assertEquals(ZoneOffset.ofHoursMinutesSeconds(0, 1, 5).getLong(OFFSET_SECONDS), 65);
539     }
540 
541     //-----------------------------------------------------------------------
542     // query(TemporalQuery)
543     //-----------------------------------------------------------------------
544     @DataProvider(name="query")
data_query()545     Object[][] data_query() {
546         return new Object[][] {
547                 {ZoneOffset.UTC, TemporalQueries.chronology(), null},
548                 {ZoneOffset.UTC, TemporalQueries.zoneId(), null},
549                 {ZoneOffset.UTC, TemporalQueries.precision(), null},
550                 {ZoneOffset.UTC, TemporalQueries.zone(), ZoneOffset.UTC},
551                 {ZoneOffset.UTC, TemporalQueries.offset(), ZoneOffset.UTC},
552                 {ZoneOffset.UTC, TemporalQueries.localDate(), null},
553                 {ZoneOffset.UTC, TemporalQueries.localTime(), null},
554         };
555     }
556 
557     @Test(dataProvider="query")
test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected)558     public <T> void test_query(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
559         assertEquals(temporal.query(query), expected);
560     }
561 
562     @Test(dataProvider="query")
test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected)563     public <T> void test_queryFrom(TemporalAccessor temporal, TemporalQuery<T> query, T expected) {
564         assertEquals(query.queryFrom(temporal), expected);
565     }
566 
567     @Test(expectedExceptions=NullPointerException.class)
test_query_null()568     public void test_query_null() {
569         ZoneOffset.UTC.query(null);
570     }
571 
572     //-----------------------------------------------------------------------
573     // compareTo()
574     //-----------------------------------------------------------------------
575     @Test
test_compareTo()576     public void test_compareTo() {
577         ZoneOffset offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
578         ZoneOffset offset2 = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4);
579         assertTrue(offset1.compareTo(offset2) > 0);
580         assertTrue(offset2.compareTo(offset1) < 0);
581         assertTrue(offset1.compareTo(offset1) == 0);
582         assertTrue(offset2.compareTo(offset2) == 0);
583     }
584 
585     //-----------------------------------------------------------------------
586     // equals() / hashCode()
587     //-----------------------------------------------------------------------
588     @Test
589     public void test_equals() {
590         ZoneOffset offset1 = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
591         ZoneOffset offset2 = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4);
592         ZoneOffset offset2b = ZoneOffset.ofHoursMinutesSeconds(2, 3, 4);
593         assertEquals(offset1.equals(offset2), false);
594         assertEquals(offset2.equals(offset1), false);
595 
596         assertEquals(offset1.equals(offset1), true);
597         assertEquals(offset2.equals(offset2), true);
598         assertEquals(offset2.equals(offset2b), true);
599 
600         assertEquals(offset1.hashCode() == offset1.hashCode(), true);
601         assertEquals(offset2.hashCode() == offset2.hashCode(), true);
602         assertEquals(offset2.hashCode() == offset2b.hashCode(), true);
603     }
604 
605     //-----------------------------------------------------------------------
606     // adjustInto()
607     //-----------------------------------------------------------------------
608     @Test
609     public void test_adjustInto_ZonedDateTime() {
610         ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1);
611         for (String zoneId : ZoneId.getAvailableZoneIds()) {
612             //Do not change offset of ZonedDateTime after adjustInto()
613             ZonedDateTime zonedDateTime_target = ZonedDateTime.of(LocalDate.of(1909, 2, 2), LocalTime.of(10, 10, 10), ZoneId.of(zoneId));
614             ZonedDateTime zonedDateTime_result = (ZonedDateTime)(base.adjustInto(zonedDateTime_target));
615             assertEquals(zonedDateTime_target.getOffset(), zonedDateTime_result.getOffset());
616 
617             OffsetDateTime offsetDateTime_target = zonedDateTime_target.toOffsetDateTime();
618             OffsetDateTime offsetDateTime_result = (OffsetDateTime)(base.adjustInto(offsetDateTime_target));
619             assertEquals(base, offsetDateTime_result.getOffset());
620         }
621     }
622 
623     @Test
624     public void test_adjustInto_OffsetDateTime() {
625         ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1);
626         for (int i=-18; i<=18; i++) {
627             OffsetDateTime offsetDateTime_target = OffsetDateTime.of(LocalDate.of(1909, 2, 2), LocalTime.of(10, 10, 10), ZoneOffset.ofHours(i));
628             OffsetDateTime offsetDateTime_result = (OffsetDateTime)base.adjustInto(offsetDateTime_target);
629             assertEquals(base, offsetDateTime_result.getOffset());
630 
631             //Do not change offset of ZonedDateTime after adjustInto()
632             ZonedDateTime zonedDateTime_target = offsetDateTime_target.toZonedDateTime();
633             ZonedDateTime zonedDateTime_result = (ZonedDateTime)(base.adjustInto(zonedDateTime_target));
634             assertEquals(zonedDateTime_target.getOffset(), zonedDateTime_result.getOffset());
635         }
636     }
637 
638     @Test(expectedExceptions=DateTimeException.class)
639     public void test_adjustInto_dateOnly() {
640         ZoneOffset base = ZoneOffset.ofHoursMinutesSeconds(1, 1, 1);
641         base.adjustInto((LocalDate.of(1909, 2, 2)));
642     }
643 
644     //-----------------------------------------------------------------------
645     // toString()
646     //-----------------------------------------------------------------------
647     @Test
648     public void test_toString() {
649         ZoneOffset offset = ZoneOffset.ofHoursMinutesSeconds(1, 0, 0);
650         assertEquals(offset.toString(), "+01:00");
651         offset = ZoneOffset.ofHoursMinutesSeconds(1, 2, 3);
652         assertEquals(offset.toString(), "+01:02:03");
653         offset = ZoneOffset.UTC;
654         assertEquals(offset.toString(), "Z");
655     }
656 
657     //-----------------------------------------------------------------------
658     //-----------------------------------------------------------------------
659     //-----------------------------------------------------------------------
660     private void doTestOffset(ZoneOffset offset, int hours, int minutes, int seconds) {
661         assertEquals(offset.getTotalSeconds(), hours * 60 * 60 + minutes * 60 + seconds);
662         final String id;
663         if (hours == 0 && minutes == 0 && seconds == 0) {
664             id = "Z";
665         } else {
666             String str = (hours < 0 || minutes < 0 || seconds < 0) ? "-" : "+";
667             str += Integer.toString(Math.abs(hours) + 100).substring(1);
668             str += ":";
669             str += Integer.toString(Math.abs(minutes) + 100).substring(1);
670             if (seconds != 0) {
671                 str += ":";
672                 str += Integer.toString(Math.abs(seconds) + 100).substring(1);
673             }
674             id = str;
675         }
676         assertEquals(offset.getId(), id);
677         assertEquals(offset, ZoneOffset.ofHoursMinutesSeconds(hours, minutes, seconds));
678         if (seconds == 0) {
679             assertEquals(offset, ZoneOffset.ofHoursMinutes(hours, minutes));
680             if (minutes == 0) {
681                 assertEquals(offset, ZoneOffset.ofHours(hours));
682             }
683         }
684         assertEquals(ZoneOffset.of(id), offset);
685         assertEquals(offset.toString(), id);
686     }
687 
688 }
689