8134928: java.time.Instant.truncatedTo(TemporalUnit unit) is truncating up if the year < 1970
authorntv
Tue, 20 Oct 2015 13:10:28 -0400
changeset 33243 c32d902a31d6
parent 33242 eafa1e90b0e1
child 33259 bbb84c5a1228
8134928: java.time.Instant.truncatedTo(TemporalUnit unit) is truncating up if the year < 1970 Reviewed-by: rriggs, scolebourne
jdk/src/java.base/share/classes/java/time/Instant.java
jdk/test/java/time/tck/java/time/TCKInstant.java
--- a/jdk/src/java.base/share/classes/java/time/Instant.java	Tue Oct 20 19:34:04 2015 +0900
+++ b/jdk/src/java.base/share/classes/java/time/Instant.java	Tue Oct 20 13:10:28 2015 -0400
@@ -758,7 +758,7 @@
             throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
         }
         long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
-        long result = (nod / dur) * dur;
+        long result = Math.floorDiv(nod, dur) * dur ;
         return plusNanos(result - nod);
     }
 
--- a/jdk/test/java/time/tck/java/time/TCKInstant.java	Tue Oct 20 19:34:04 2015 +0900
+++ b/jdk/test/java/time/tck/java/time/TCKInstant.java	Tue Oct 20 13:10:28 2015 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -638,6 +638,12 @@
                 {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 0, 0)},
                 {Instant.ofEpochSecond(86400 + 7200 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 5400, 0)},
                 {Instant.ofEpochSecond(86400 + 10800 + 60 + 1, 123_456_789), NINETY_MINS, Instant.ofEpochSecond(86400 + 10800, 0)},
+
+                {Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_789), MINUTES, Instant.ofEpochSecond(-86400 - 3600 - 120, 0 )},
+                {Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_789), MICROS, Instant.ofEpochSecond(-86400 - 3600 - 60 - 1, 123_456_000)},
+
+                {Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 0), SECONDS, Instant.ofEpochSecond(86400 + 3600 + 60 + 1, 0)},
+                {Instant.ofEpochSecond(-86400 - 3600 - 120, 0), MINUTES, Instant.ofEpochSecond(-86400 - 3600 - 120, 0)},
         };
     }
     @Test(dataProvider="truncatedToValid")