• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
64 import java.time.DateTimeException;
65 import java.time.Instant;
66 import java.time.LocalDate;
67 import java.time.LocalTime;
68 import java.time.Month;
69 import java.time.OffsetDateTime;
70 import java.time.Year;
71 import java.time.ZoneOffset;
72 
73 import org.testng.annotations.Test;
74 
75 /**
76  * Test OffsetDateTime creation.
77  */
78 @Test
79 public class TestOffsetDateTime_instants {
80 
81     private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
82     private static final ZoneOffset OFFSET_MAX = ZoneOffset.ofHours(18);
83     private static final ZoneOffset OFFSET_MIN = ZoneOffset.ofHours(-18);
84 
85     //-----------------------------------------------------------------------
86     @Test(expectedExceptions=NullPointerException.class)
factory_ofInstant_nullInstant()87     public void factory_ofInstant_nullInstant() {
88         OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
89     }
90 
91     @Test(expectedExceptions=NullPointerException.class)
factory_ofInstant_nullOffset()92     public void factory_ofInstant_nullOffset() {
93         Instant instant = Instant.ofEpochSecond(0L);
94         OffsetDateTime.ofInstant(instant, (ZoneOffset) null);
95     }
96 
factory_ofInstant_allSecsInDay()97     public void factory_ofInstant_allSecsInDay() {
98         for (int i = 0; i < (24 * 60 * 60); i++) {
99             Instant instant = Instant.ofEpochSecond(i);
100             OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_PONE);
101             assertEquals(test.getYear(), 1970);
102             assertEquals(test.getMonth(), Month.JANUARY);
103             assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
104             assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
105             assertEquals(test.getMinute(), (i / 60) % 60);
106             assertEquals(test.getSecond(), i % 60);
107         }
108     }
109 
factory_ofInstant_allDaysInCycle()110     public void factory_ofInstant_allDaysInCycle() {
111         // sanity check using different algorithm
112         OffsetDateTime expected = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
113         for (long i = 0; i < 146097; i++) {
114             Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
115             OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
116             assertEquals(test, expected);
117             expected = expected.plusDays(1);
118         }
119     }
120 
factory_ofInstant_history()121     public void factory_ofInstant_history() {
122         doTest_factory_ofInstant_all(-2820, 2820);
123     }
124 
125     //-----------------------------------------------------------------------
factory_ofInstant_minYear()126     public void factory_ofInstant_minYear() {
127         doTest_factory_ofInstant_all(Year.MIN_VALUE, Year.MIN_VALUE + 420);
128     }
129 
130     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_tooLow()131     public void factory_ofInstant_tooLow() {
132         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
133         int year = Year.MIN_VALUE - 1;
134         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
135         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
136         OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
137     }
138 
factory_ofInstant_maxYear()139     public void factory_ofInstant_maxYear() {
140         doTest_factory_ofInstant_all(Year.MAX_VALUE - 420, Year.MAX_VALUE);
141     }
142 
143     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_tooBig()144     public void factory_ofInstant_tooBig() {
145         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
146         long year = Year.MAX_VALUE + 1L;
147         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
148         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
149         OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
150     }
151 
152     //-----------------------------------------------------------------------
factory_ofInstant_minWithMinOffset()153     public void factory_ofInstant_minWithMinOffset() {
154         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
155         int year = Year.MIN_VALUE;
156         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
157         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MIN.getTotalSeconds());
158         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MIN);
159         assertEquals(test.getYear(), Year.MIN_VALUE);
160         assertEquals(test.getMonth().getValue(), 1);
161         assertEquals(test.getDayOfMonth(), 1);
162         assertEquals(test.getOffset(), OFFSET_MIN);
163         assertEquals(test.getHour(), 0);
164         assertEquals(test.getMinute(), 0);
165         assertEquals(test.getSecond(), 0);
166         assertEquals(test.getNano(), 0);
167     }
168 
factory_ofInstant_minWithMaxOffset()169     public void factory_ofInstant_minWithMaxOffset() {
170         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
171         int year = Year.MIN_VALUE;
172         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
173         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MAX.getTotalSeconds());
174         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MAX);
175         assertEquals(test.getYear(), Year.MIN_VALUE);
176         assertEquals(test.getMonth().getValue(), 1);
177         assertEquals(test.getDayOfMonth(), 1);
178         assertEquals(test.getOffset(), OFFSET_MAX);
179         assertEquals(test.getHour(), 0);
180         assertEquals(test.getMinute(), 0);
181         assertEquals(test.getSecond(), 0);
182         assertEquals(test.getNano(), 0);
183     }
184 
factory_ofInstant_maxWithMinOffset()185     public void factory_ofInstant_maxWithMinOffset() {
186         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
187         int year = Year.MAX_VALUE;
188         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970;
189         Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MIN.getTotalSeconds());
190         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MIN);
191         assertEquals(test.getYear(), Year.MAX_VALUE);
192         assertEquals(test.getMonth().getValue(), 12);
193         assertEquals(test.getDayOfMonth(), 31);
194         assertEquals(test.getOffset(), OFFSET_MIN);
195         assertEquals(test.getHour(), 23);
196         assertEquals(test.getMinute(), 59);
197         assertEquals(test.getSecond(), 59);
198         assertEquals(test.getNano(), 0);
199     }
200 
factory_ofInstant_maxWithMaxOffset()201     public void factory_ofInstant_maxWithMaxOffset() {
202         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
203         int year = Year.MAX_VALUE;
204         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970;
205         Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MAX.getTotalSeconds());
206         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MAX);
207         assertEquals(test.getYear(), Year.MAX_VALUE);
208         assertEquals(test.getMonth().getValue(), 12);
209         assertEquals(test.getDayOfMonth(), 31);
210         assertEquals(test.getOffset(), OFFSET_MAX);
211         assertEquals(test.getHour(), 23);
212         assertEquals(test.getMinute(), 59);
213         assertEquals(test.getSecond(), 59);
214         assertEquals(test.getNano(), 0);
215     }
216 
217     //-----------------------------------------------------------------------
218     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_maxInstantWithMaxOffset()219     public void factory_ofInstant_maxInstantWithMaxOffset() {
220         Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
221         OffsetDateTime.ofInstant(instant, OFFSET_MAX);
222     }
223 
224     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_maxInstantWithMinOffset()225     public void factory_ofInstant_maxInstantWithMinOffset() {
226         Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
227         OffsetDateTime.ofInstant(instant, OFFSET_MIN);
228     }
229 
230     //-----------------------------------------------------------------------
doTest_factory_ofInstant_all(long minYear, long maxYear)231     private void doTest_factory_ofInstant_all(long minYear, long maxYear) {
232         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
233         int minOffset = (minYear <= 0 ? 0 : 3);
234         int maxOffset = (maxYear <= 0 ? 0 : 3);
235         long minDays = (minYear * 365L + ((minYear + minOffset) / 4L - (minYear + minOffset) / 100L + (minYear + minOffset) / 400L)) - days_0000_to_1970;
236         long maxDays = (maxYear * 365L + ((maxYear + maxOffset) / 4L - (maxYear + maxOffset) / 100L + (maxYear + maxOffset) / 400L)) + 365L - days_0000_to_1970;
237 
238         final LocalDate maxDate = LocalDate.of(Year.MAX_VALUE, 12, 31);
239         OffsetDateTime expected = OffsetDateTime.of(LocalDate.of((int) minYear, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
240         for (long i = minDays; i < maxDays; i++) {
241             Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
242             try {
243                 OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
244                 assertEquals(test, expected);
245                 if (expected.toLocalDate().equals(maxDate) == false) {
246                     expected = expected.plusDays(1);
247                 }
248             } catch (RuntimeException|Error ex) {
249                 System.out.println("Error: " + i + " " + expected);
250                 throw ex;
251             }
252         }
253     }
254 
255     // for performance testing
256     //    private void doTest_factory_ofInstant_all(int minYear, int maxYear) {
257     //        long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
258     //        int minOffset = (minYear <= 0 ? 0 : 3);
259     //        int maxOffset = (maxYear <= 0 ? 0 : 3);
260     //        long minDays = (long) (minYear * 365L + ((minYear + minOffset) / 4L - (minYear + minOffset) / 100L + (minYear + minOffset) / 400L)) - days_0000_to_1970;
261     //        long maxDays = (long) (maxYear * 365L + ((maxYear + maxOffset) / 4L - (maxYear + maxOffset) / 100L + (maxYear + maxOffset) / 400L)) + 365L - days_0000_to_1970;
262     //
263     //        OffsetDateTime expected = OffsetDateTime.dateTime(minYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
264     //        Date cutover = new Date(Long.MIN_VALUE);
265     //        GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
266     //        cal.setGregorianChange(cutover);
267     //        for (long i = minDays; i < maxDays; i++) {
268     //            Instant instant = Instant.instant(i * 24L * 60L * 60L);
269     //            try {
270     //                cal.setTimeInMillis(instant.getEpochSecond() * 1000L);
271     //                assertEquals(cal.get(GregorianCalendar.MONTH), expected.getMonth().getValue() - 1);
272     //                assertEquals(cal.get(GregorianCalendar.DAY_OF_MONTH), expected.getDayOfMonth().getValue());
273     //                expected = expected.plusDays(1);
274     //            } catch (RuntimeException ex) {
275     //                System.out.println("Error: " + i + " " + expected);
276     //                throw ex;
277     //            } catch (Error ex) {
278     //                System.out.println("Error: " + i + " " + expected);
279     //                throw ex;
280     //            }
281     //        }
282     //    }
283 
284     //-----------------------------------------------------------------------
test_toInstant_19700101()285     public void test_toInstant_19700101() {
286         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
287         Instant test = dt.toInstant();
288         assertEquals(test.getEpochSecond(), 0);
289         assertEquals(test.getNano(), 0);
290     }
291 
test_toInstant_19700101_oneNano()292     public void test_toInstant_19700101_oneNano() {
293         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 1), ZoneOffset.UTC);
294         Instant test = dt.toInstant();
295         assertEquals(test.getEpochSecond(), 0);
296         assertEquals(test.getNano(), 1);
297     }
298 
test_toInstant_19700101_minusOneNano()299     public void test_toInstant_19700101_minusOneNano() {
300         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(23, 59, 59, 999999999), ZoneOffset.UTC);
301         Instant test = dt.toInstant();
302         assertEquals(test.getEpochSecond(), -1);
303         assertEquals(test.getNano(), 999999999);
304     }
305 
test_toInstant_19700102()306     public void test_toInstant_19700102() {
307         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 2), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
308         Instant test = dt.toInstant();
309         assertEquals(test.getEpochSecond(), 24L * 60L * 60L);
310         assertEquals(test.getNano(), 0);
311     }
312 
test_toInstant_19691231()313     public void test_toInstant_19691231() {
314         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
315         Instant test = dt.toInstant();
316         assertEquals(test.getEpochSecond(), -24L * 60L * 60L);
317         assertEquals(test.getNano(), 0);
318     }
319 
320     //-----------------------------------------------------------------------
test_toEpochSecond_19700101()321     public void test_toEpochSecond_19700101() {
322         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
323         assertEquals(dt.toEpochSecond(), 0);
324     }
325 
test_toEpochSecond_19700101_oneNano()326     public void test_toEpochSecond_19700101_oneNano() {
327         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of( 0, 0, 0, 1), ZoneOffset.UTC);
328         assertEquals(dt.toEpochSecond(), 0);
329     }
330 
test_toEpochSecond_19700101_minusOneNano()331     public void test_toEpochSecond_19700101_minusOneNano() {
332         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(23, 59, 59, 999999999), ZoneOffset.UTC);
333         assertEquals(dt.toEpochSecond(), -1);
334     }
335 
test_toEpochSecond_19700102()336     public void test_toEpochSecond_19700102() {
337         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 2), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
338         assertEquals(dt.toEpochSecond(), 24L * 60L * 60L);
339     }
340 
test_toEpochSecond_19691231()341     public void test_toEpochSecond_19691231() {
342         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
343         assertEquals(dt.toEpochSecond(), -24L * 60L * 60L);
344     }
345 
346 }
347