Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk20u Public archive

Commit

Permalink
8307466: java.time.Instant calculation bug in until and between methods
Browse files Browse the repository at this point in the history
Reviewed-by: naoto
Backport-of: 356667f1559396b09e0b830d92978f2ea7a9300b
  • Loading branch information
Roger Riggs committed May 12, 2023
1 parent 6485fca commit 47b3b4c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/java.base/share/classes/java/time/Instant.java
Original file line number Diff line number Diff line change
Expand Up @@ -1171,20 +1171,30 @@ private long nanosUntil(Instant end) {
}

private long microsUntil(Instant end) {
long secsDiff = Math.subtractExact(end.seconds, seconds);
long totalMicros = Math.multiplyExact(secsDiff, MICROS_PER_SECOND);
return Math.addExact(totalMicros, (end.nanos - nanos) / 1000);
long microsDiff = Math.multiplyExact(end.seconds - seconds, MICROS_PER_SECOND);
int nanosDiff = end.nanos - nanos;
if (microsDiff > 0 && nanosDiff < 0) {
return (microsDiff - 1_000_000) + (nanosDiff + 1_000_000_000) / 1_000;
} else if (microsDiff < 0 && nanosDiff > 0) {
return (microsDiff + 1_000_000) + (nanosDiff - 1_000_000_000) / 1_000;
}
return Math.addExact(microsDiff, nanosDiff / 1_000);
}

private long millisUntil(Instant end) {
long secsDiff = Math.subtractExact(end.seconds, seconds);
long totalMillis = Math.multiplyExact(secsDiff, MILLIS_PER_SECOND);
return Math.addExact(totalMillis, (end.nanos - nanos) / 1000_000);
long millisDiff = Math.multiplyExact(end.seconds - seconds, MILLIS_PER_SECOND);
int nanosDiff = end.nanos - nanos;
if (millisDiff > 0 && nanosDiff < 0) {
return (millisDiff - 1_000) + (nanosDiff + 1_000_000_000) / 1_000_000;
} else if (millisDiff < 0 && nanosDiff > 0) {
return (millisDiff + 1_000) + (nanosDiff - 1_000_000_000) / 1_000_000;
}
return Math.addExact(millisDiff, nanosDiff / 1_000_000);
}

private long secondsUntil(Instant end) {
long secsDiff = Math.subtractExact(end.seconds, seconds);
long nanosDiff = end.nanos - nanos;
int nanosDiff = end.nanos - nanos;
if (secsDiff > 0 && nanosDiff < 0) {
secsDiff--;
} else if (secsDiff < 0 && nanosDiff > 0) {
Expand Down
62 changes: 61 additions & 1 deletion test/jdk/java/time/tck/java/time/TCKInstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
/**
* Test Instant.
*
* @bug 8133022
* @bug 8133022 8307466
*/
@Test
public class TCKInstant extends AbstractDateTimeTest {
Expand Down Expand Up @@ -1754,6 +1754,66 @@ Object[][] data_periodUntilUnit() {
{5, 650, 7, 50, SECONDS, 1},
{5, 650, 8, 50, SECONDS, 2},

{5, 650_000_000, 5, 650_999_666, MILLIS, 0L},
{5, 999_777, 6, 0, MILLIS, 999L},
{5, 999_888, 6, 1000000, MILLIS, 1000L},

{5, 650_000_000, 3, 650_000_000, MILLIS, -2_000L},
{5, 650_000_000, 4, 650_000_000, MILLIS, -1_000L},
{5, 650_000_000, 5, 650_000_000, MILLIS, 0},
{5, 650_000_000, 6, 650_000_000, MILLIS, 1_000L},
{5, 650_000_000, 7, 650_000_000, MILLIS, 2_000L},

{5, 650_000_000, 3, 0, MILLIS, -2_650L},
{5, 650_000_000, 4, 0, MILLIS, -1_650L},
{5, 650_000_000, 5, 0, MILLIS, -650L},
{5, 650_000_000, 6, 0, MILLIS, 350L},
{5, 650_000_000, 7, 0, MILLIS, 1_350L},

{5, 650_000_000, -1, 950_000_000, MILLIS, -5_700L},
{5, 650_000_000, 0, 950_000_000, MILLIS, -4_700L},
{5, 650_000_000, 3, 950_000_000, MILLIS, -1_700L},
{5, 650_000_000, 4, 950_000_000, MILLIS, -700L},
{5, 650_000_000, 5, 950_000_000, MILLIS, 300L},
{5, 650_000_000, 6, 950_000_000, MILLIS, 1_300L},
{5, 650_000_000, 7, 950_000_000, MILLIS, 2_300L},

{5, 650_000_000, 4, 50_000_000, MILLIS, -1_600L},
{5, 650_000_000, 5, 50_000_000, MILLIS, -600L},
{5, 650_000_000, 6, 50_000_000, MILLIS, 400L},
{5, 650_000_000, 7, 50_000_000, MILLIS, 1_400L},
{5, 650_000_000, 8, 50_000_000, MILLIS, 2_400L},

{5, 650_000_000, 5, 650_999_000, MICROS, 999L},
{0, 999_000, 1, 0, MICROS, 999_001L},
{0, 999_000, 1, 1000000, MICROS, 1000_001L},

{5, 650_000_000, 3, 650_000_000, MICROS, -2_000_000L},
{5, 650_000_000, 4, 650_000_000, MICROS, -1_000_000L},
{5, 650_000_000, 5, 650_000_000, MICROS, 0},
{5, 650_000_000, 6, 650_000_000, MICROS, 1_000_000L},
{5, 650_000_000, 7, 650_000_000, MICROS, 2_000_000L},

{5, 650_000_000, 3, 0, MICROS, -2_650_000L},
{5, 650_000_000, 4, 0, MICROS, -1_650_000L},
{5, 650_000_000, 5, 0, MICROS, -650_000L},
{5, 650_000_000, 6, 0, MICROS, 350_000L},
{5, 650_000_000, 7, 0, MICROS, 1_350_000L},

{5, 650_000_000, -1, 950_000_000, MICROS, -5_700_000L},
{5, 650_000_000, 0, 950_000_000, MICROS, -4_700_000L},
{5, 650_000_000, 3, 950_000_000, MICROS, -1_700_000L},
{5, 650_000_000, 4, 950_000_000, MICROS, -700_000L},
{5, 650_000_000, 5, 950_000_000, MICROS, 300_000L},
{5, 650_000_000, 6, 950_000_000, MICROS, 1_300_000L},
{5, 650_000_000, 7, 950_000_000, MICROS, 2_300_000L},

{5, 650_000_000, 4, 50_000_000, MICROS, -1_600_000L},
{5, 650_000_000, 5, 50_000_000, MICROS, -600_000L},
{5, 650_000_000, 6, 50_000_000, MICROS, 400_000L},
{5, 650_000_000, 7, 50_000_000, MICROS, 1_400_000L},
{5, 650_000_000, 8, 50_000_000, MICROS, 2_400_000L},

{5, 650_000_000, -1, 650_000_000, NANOS, -6_000_000_000L},
{5, 650_000_000, 0, 650_000_000, NANOS, -5_000_000_000L},
{5, 650_000_000, 3, 650_000_000, NANOS, -2_000_000_000L},
Expand Down

1 comment on commit 47b3b4c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.