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