RFR: 8307466: java.time.Instant calculation bug in until and between methods

Raffaello Giulietti rgiulietti at openjdk.org
Sat May 6 21:20:17 UTC 2023


On Fri, 5 May 2023 21:28:25 GMT, Roger Riggs <rriggs at openjdk.org> wrote:

> The implementation of java.time.Instant.until(I2, ChronoUnit) in some cases did not correctly borrow or carry from the nanos to the seconds when computing using ChronoUnit.MILLIS or ChronoUnit.MICROS.
> The errant computation was introduced by [JDK-8273369](https://bugs.openjdk.org/browse/JDK-8273369).

src/java.base/share/classes/java/time/Instant.java line 1173:

> 1171:     private long microsUntil(Instant end) {
> 1172:         long microsDiff = Math.multiplyExact(end.seconds - seconds, MICROS_PER_SECOND);
> 1173:         long nanosDiff = end.nanos - nanos;

FWIW, `nanosDiff` could be declared `int` to slightly speed up the division later in the code, while still not risking overflows.

Similarly in `millisUntil()`.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/13846#discussion_r1186747228


More information about the core-libs-dev mailing list