Skip to content

Commit

Permalink
8307466: java.time.Instant calculation bug in until and between methods
Browse files Browse the repository at this point in the history
Co-authored-by: Raffaello Giulietti <rgiulietti@openjdk.org>
Reviewed-by: scolebourne, naoto
  • Loading branch information
Roger Riggs and rgiulietti committed May 9, 2023
1 parent 723582c commit 356667f
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 @@ -1169,20 +1169,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

5 comments on commit 356667f

@RogerRiggs
Copy link
Contributor

Choose a reason for hiding this comment

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

@openjdk
Copy link

@openjdk openjdk bot commented on 356667f May 11, 2023

Choose a reason for hiding this comment

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

@RogerRiggs The target repository openjdk/jdk20 is not a valid target for backports.
List of valid target repositories: openjdk/jdk, openjdk/jdk11u, openjdk/jdk11u-dev, openjdk/jdk17u, openjdk/jdk17u-dev, openjdk/jdk19u, openjdk/jdk20u, openjdk/jdk7u, openjdk/jdk8u, openjdk/jdk8u-dev, openjdk/jfx, openjdk/jfx20u, openjdk/shenandoah-jdk8u, openjdk/shenandoah-jdk8u-dev.
Supplying the organization/group prefix is optional.

@RogerRiggs
Copy link
Contributor

Choose a reason for hiding this comment

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

/backport openjdk/jdk20u

@openjdk
Copy link

@openjdk openjdk bot commented on 356667f May 11, 2023

Choose a reason for hiding this comment

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

@RogerRiggs the backport was successfully created on the branch RogerRiggs-backport-356667f1 in my personal fork of openjdk/jdk20u. To create a pull request with this backport targeting openjdk/jdk20u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 356667f1 from the openjdk/jdk repository.

The commit being backported was authored by Roger Riggs on 9 May 2023 and was reviewed by Stephen Colebourne and Naoto Sato.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk20u:

$ git fetch https://github.com/openjdk-bots/jdk20u.git RogerRiggs-backport-356667f1:RogerRiggs-backport-356667f1
$ git checkout RogerRiggs-backport-356667f1
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk20u.git RogerRiggs-backport-356667f1

@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.