New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8273369: Computing micros between two instants unexpectedly overflows for some cases #5396
Closed
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
09d9b25
8273369: Computing micros between two instants unexpectedly overflows…
naotoj 4b874ff
Added a constant for nanos per micro.
naotoj 7d567e8
Corrected the constant.
naotoj bbd6abc
Moved the constant to LocalTime for consistency.
naotoj ccf73bf
Added comments for the test.
naotoj 1f6fd47
Corrected the calculation for MILLIS as well.
naotoj File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2012, 2021, 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 | ||
|
@@ -60,13 +60,15 @@ | |
package test.java.time; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
import org.testng.annotations.Test; | ||
import org.testng.annotations.DataProvider; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
/** | ||
* Test Instant. | ||
* @bug 8273369 | ||
*/ | ||
@Test | ||
public class TestInstant extends AbstractTest { | ||
|
@@ -96,4 +98,16 @@ public void test_epochMillis(String name, long millis) { | |
assertEquals(millis, m, name); | ||
} | ||
|
||
/** | ||
* Checks whether Instant.until() returning microseconds does not throw | ||
* an ArithmeticException for Instants apart for more than Long.MAX_VALUE | ||
* nanoseconds. | ||
*/ | ||
@Test | ||
public void test_microsUntil() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment about the test might be helpful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. Added some comments to the test. |
||
var nanoMax = Instant.EPOCH.plusNanos(Long.MAX_VALUE); | ||
var totalMicros = Instant.EPOCH.until(nanoMax, ChronoUnit.MICROS); | ||
var plusOneMicro = Instant.EPOCH.until(nanoMax.plusNanos(1000), ChronoUnit.MICROS); | ||
assertEquals(plusOneMicro - totalMicros, 1L); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you define NANOS_PER_MICRO, the others conversions use predefined constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defined it as a private field in
Instant
.