test/java/time/test/java/time/TestOffsetDateTime_instants.java

Print this page




  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 


 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"));




  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 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.OffsetDateTime;
  70 import java.time.Year;
  71 
  72 import static org.testng.Assert.assertEquals;
  73 
  74 import org.testng.annotations.Test;
  75 
  76 /**
  77  * Test OffsetDateTime 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 


 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.toLocalDate().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"));