• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp;
33 
34 import static org.testng.Assert.assertEquals;
35 
36 import org.testng.annotations.Test;
37 
38 /**
39  * Test OffsetDateTime creation.
40  */
41 @Test
42 public class TestOffsetDateTime_instants {
43 
44     private static final ZoneOffset OFFSET_PONE = ZoneOffset.ofHours(1);
45     private static final ZoneOffset OFFSET_MAX = ZoneOffset.ofHours(18);
46     private static final ZoneOffset OFFSET_MIN = ZoneOffset.ofHours(-18);
47 
48     //-----------------------------------------------------------------------
49     @Test(expectedExceptions=NullPointerException.class)
factory_ofInstant_nullInstant()50     public void factory_ofInstant_nullInstant() {
51         OffsetDateTime.ofInstant((Instant) null, OFFSET_PONE);
52     }
53 
54     @Test(expectedExceptions=NullPointerException.class)
factory_ofInstant_nullOffset()55     public void factory_ofInstant_nullOffset() {
56         Instant instant = Instant.ofEpochSecond(0L);
57         OffsetDateTime.ofInstant(instant, (ZoneOffset) null);
58     }
59 
factory_ofInstant_allSecsInDay()60     public void factory_ofInstant_allSecsInDay() {
61         for (int i = 0; i < (24 * 60 * 60); i++) {
62             Instant instant = Instant.ofEpochSecond(i);
63             OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_PONE);
64             assertEquals(test.getYear(), 1970);
65             assertEquals(test.getMonth(), Month.JANUARY);
66             assertEquals(test.getDayOfMonth(), 1 + (i >= 23 * 60 * 60 ? 1 : 0));
67             assertEquals(test.getHour(), ((i / (60 * 60)) + 1) % 24);
68             assertEquals(test.getMinute(), (i / 60) % 60);
69             assertEquals(test.getSecond(), i % 60);
70         }
71     }
72 
factory_ofInstant_allDaysInCycle()73     public void factory_ofInstant_allDaysInCycle() {
74         // sanity check using different algorithm
75         OffsetDateTime expected = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
76         for (long i = 0; i < 146097; i++) {
77             Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
78             OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
79             assertEquals(test, expected);
80             expected = expected.plusDays(1);
81         }
82     }
83 
factory_ofInstant_history()84     public void factory_ofInstant_history() {
85         doTest_factory_ofInstant_all(-2820, 2820);
86     }
87 
88     //-----------------------------------------------------------------------
factory_ofInstant_minYear()89     public void factory_ofInstant_minYear() {
90         doTest_factory_ofInstant_all(Year.MIN_VALUE, Year.MIN_VALUE + 420);
91     }
92 
93     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_tooLow()94     public void factory_ofInstant_tooLow() {
95         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
96         int year = Year.MIN_VALUE - 1;
97         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
98         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
99         OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
100     }
101 
factory_ofInstant_maxYear()102     public void factory_ofInstant_maxYear() {
103         doTest_factory_ofInstant_all(Year.MAX_VALUE - 420, Year.MAX_VALUE);
104     }
105 
106     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_tooBig()107     public void factory_ofInstant_tooBig() {
108         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
109         long year = Year.MAX_VALUE + 1L;
110         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
111         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L);
112         OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
113     }
114 
115     //-----------------------------------------------------------------------
factory_ofInstant_minWithMinOffset()116     public void factory_ofInstant_minWithMinOffset() {
117         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
118         int year = Year.MIN_VALUE;
119         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
120         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MIN.getTotalSeconds());
121         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MIN);
122         assertEquals(test.getYear(), Year.MIN_VALUE);
123         assertEquals(test.getMonth().getValue(), 1);
124         assertEquals(test.getDayOfMonth(), 1);
125         assertEquals(test.getOffset(), OFFSET_MIN);
126         assertEquals(test.getHour(), 0);
127         assertEquals(test.getMinute(), 0);
128         assertEquals(test.getSecond(), 0);
129         assertEquals(test.getNano(), 0);
130     }
131 
factory_ofInstant_minWithMaxOffset()132     public void factory_ofInstant_minWithMaxOffset() {
133         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
134         int year = Year.MIN_VALUE;
135         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) - days_0000_to_1970;
136         Instant instant = Instant.ofEpochSecond(days * 24L * 60L * 60L - OFFSET_MAX.getTotalSeconds());
137         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MAX);
138         assertEquals(test.getYear(), Year.MIN_VALUE);
139         assertEquals(test.getMonth().getValue(), 1);
140         assertEquals(test.getDayOfMonth(), 1);
141         assertEquals(test.getOffset(), OFFSET_MAX);
142         assertEquals(test.getHour(), 0);
143         assertEquals(test.getMinute(), 0);
144         assertEquals(test.getSecond(), 0);
145         assertEquals(test.getNano(), 0);
146     }
147 
factory_ofInstant_maxWithMinOffset()148     public void factory_ofInstant_maxWithMinOffset() {
149         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
150         int year = Year.MAX_VALUE;
151         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970;
152         Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MIN.getTotalSeconds());
153         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MIN);
154         assertEquals(test.getYear(), Year.MAX_VALUE);
155         assertEquals(test.getMonth().getValue(), 12);
156         assertEquals(test.getDayOfMonth(), 31);
157         assertEquals(test.getOffset(), OFFSET_MIN);
158         assertEquals(test.getHour(), 23);
159         assertEquals(test.getMinute(), 59);
160         assertEquals(test.getSecond(), 59);
161         assertEquals(test.getNano(), 0);
162     }
163 
factory_ofInstant_maxWithMaxOffset()164     public void factory_ofInstant_maxWithMaxOffset() {
165         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
166         int year = Year.MAX_VALUE;
167         long days = (year * 365L + (year / 4 - year / 100 + year / 400)) + 365 - days_0000_to_1970;
168         Instant instant = Instant.ofEpochSecond((days + 1) * 24L * 60L * 60L - 1 - OFFSET_MAX.getTotalSeconds());
169         OffsetDateTime test = OffsetDateTime.ofInstant(instant, OFFSET_MAX);
170         assertEquals(test.getYear(), Year.MAX_VALUE);
171         assertEquals(test.getMonth().getValue(), 12);
172         assertEquals(test.getDayOfMonth(), 31);
173         assertEquals(test.getOffset(), OFFSET_MAX);
174         assertEquals(test.getHour(), 23);
175         assertEquals(test.getMinute(), 59);
176         assertEquals(test.getSecond(), 59);
177         assertEquals(test.getNano(), 0);
178     }
179 
180     //-----------------------------------------------------------------------
181     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_maxInstantWithMaxOffset()182     public void factory_ofInstant_maxInstantWithMaxOffset() {
183         Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
184         OffsetDateTime.ofInstant(instant, OFFSET_MAX);
185     }
186 
187     @Test(expectedExceptions=DateTimeException.class)
factory_ofInstant_maxInstantWithMinOffset()188     public void factory_ofInstant_maxInstantWithMinOffset() {
189         Instant instant = Instant.ofEpochSecond(Long.MAX_VALUE);
190         OffsetDateTime.ofInstant(instant, OFFSET_MIN);
191     }
192 
193     //-----------------------------------------------------------------------
doTest_factory_ofInstant_all(long minYear, long maxYear)194     private void doTest_factory_ofInstant_all(long minYear, long maxYear) {
195         long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
196         int minOffset = (minYear <= 0 ? 0 : 3);
197         int maxOffset = (maxYear <= 0 ? 0 : 3);
198         long minDays = (minYear * 365L + ((minYear + minOffset) / 4L - (minYear + minOffset) / 100L + (minYear + minOffset) / 400L)) - days_0000_to_1970;
199         long maxDays = (maxYear * 365L + ((maxYear + maxOffset) / 4L - (maxYear + maxOffset) / 100L + (maxYear + maxOffset) / 400L)) + 365L - days_0000_to_1970;
200 
201         final LocalDate maxDate = LocalDate.of(Year.MAX_VALUE, 12, 31);
202         OffsetDateTime expected = OffsetDateTime.of(LocalDate.of((int) minYear, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
203         for (long i = minDays; i < maxDays; i++) {
204             Instant instant = Instant.ofEpochSecond(i * 24L * 60L * 60L);
205             try {
206                 OffsetDateTime test = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
207                 assertEquals(test, expected);
208                 if (expected.toLocalDate().equals(maxDate) == false) {
209                     expected = expected.plusDays(1);
210                 }
211             } catch (RuntimeException ex) {
212                 System.out.println("RuntimeException: " + i + " " + expected);
213                 throw ex;
214             } catch (Error ex) {
215                 System.out.println("Error: " + i + " " + expected);
216                 throw ex;
217             }
218         }
219     }
220 
221     // for performance testing
222     //    private void doTest_factory_ofInstant_all(int minYear, int maxYear) {
223     //        long days_0000_to_1970 = (146097 * 5) - (30 * 365 + 7);
224     //        int minOffset = (minYear <= 0 ? 0 : 3);
225     //        int maxOffset = (maxYear <= 0 ? 0 : 3);
226     //        long minDays = (long) (minYear * 365L + ((minYear + minOffset) / 4L - (minYear + minOffset) / 100L + (minYear + minOffset) / 400L)) - days_0000_to_1970;
227     //        long maxDays = (long) (maxYear * 365L + ((maxYear + maxOffset) / 4L - (maxYear + maxOffset) / 100L + (maxYear + maxOffset) / 400L)) + 365L - days_0000_to_1970;
228     //
229     //        OffsetDateTime expected = OffsetDateTime.dateTime(minYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
230     //        Date cutover = new Date(Long.MIN_VALUE);
231     //        GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
232     //        cal.setGregorianChange(cutover);
233     //        for (long i = minDays; i < maxDays; i++) {
234     //            Instant instant = Instant.instant(i * 24L * 60L * 60L);
235     //            try {
236     //                cal.setTimeInMillis(instant.getEpochSecond() * 1000L);
237     //                assertEquals(cal.get(GregorianCalendar.MONTH), expected.getMonth().getValue() - 1);
238     //                assertEquals(cal.get(GregorianCalendar.DAY_OF_MONTH), expected.getDayOfMonth().getValue());
239     //                expected = expected.plusDays(1);
240     //            } catch (RuntimeException ex) {
241     //                System.out.println("Error: " + i + " " + expected);
242     //                throw ex;
243     //            } catch (Error ex) {
244     //                System.out.println("Error: " + i + " " + expected);
245     //                throw ex;
246     //            }
247     //        }
248     //    }
249 
250     //-----------------------------------------------------------------------
test_toInstant_19700101()251     public void test_toInstant_19700101() {
252         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
253         Instant test = dt.toInstant();
254         assertEquals(test.getEpochSecond(), 0);
255         assertEquals(test.getNano(), 0);
256     }
257 
test_toInstant_19700101_oneNano()258     public void test_toInstant_19700101_oneNano() {
259         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 1), ZoneOffset.UTC);
260         Instant test = dt.toInstant();
261         assertEquals(test.getEpochSecond(), 0);
262         assertEquals(test.getNano(), 1);
263     }
264 
test_toInstant_19700101_minusOneNano()265     public void test_toInstant_19700101_minusOneNano() {
266         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(23, 59, 59, 999999999), ZoneOffset.UTC);
267         Instant test = dt.toInstant();
268         assertEquals(test.getEpochSecond(), -1);
269         assertEquals(test.getNano(), 999999999);
270     }
271 
test_toInstant_19700102()272     public void test_toInstant_19700102() {
273         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 2), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
274         Instant test = dt.toInstant();
275         assertEquals(test.getEpochSecond(), 24L * 60L * 60L);
276         assertEquals(test.getNano(), 0);
277     }
278 
test_toInstant_19691231()279     public void test_toInstant_19691231() {
280         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
281         Instant test = dt.toInstant();
282         assertEquals(test.getEpochSecond(), -24L * 60L * 60L);
283         assertEquals(test.getNano(), 0);
284     }
285 
286     //-----------------------------------------------------------------------
test_toEpochSecond_19700101()287     public void test_toEpochSecond_19700101() {
288         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
289         assertEquals(dt.toEpochSecond(), 0);
290     }
291 
test_toEpochSecond_19700101_oneNano()292     public void test_toEpochSecond_19700101_oneNano() {
293         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 1), LocalTime.of( 0, 0, 0, 1), ZoneOffset.UTC);
294         assertEquals(dt.toEpochSecond(), 0);
295     }
296 
test_toEpochSecond_19700101_minusOneNano()297     public void test_toEpochSecond_19700101_minusOneNano() {
298         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(23, 59, 59, 999999999), ZoneOffset.UTC);
299         assertEquals(dt.toEpochSecond(), -1);
300     }
301 
test_toEpochSecond_19700102()302     public void test_toEpochSecond_19700102() {
303         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1970, 1, 2), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
304         assertEquals(dt.toEpochSecond(), 24L * 60L * 60L);
305     }
306 
test_toEpochSecond_19691231()307     public void test_toEpochSecond_19691231() {
308         OffsetDateTime dt = OffsetDateTime.of(LocalDate.of(1969, 12, 31), LocalTime.of(0, 0, 0, 0), ZoneOffset.UTC);
309         assertEquals(dt.toEpochSecond(), -24L * 60L * 60L);
310     }
311 
312 }
313