Skip to content

Duration Java 9 methods #410

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

Merged
merged 6 commits into from
Nov 16, 2022
Merged

Duration Java 9 methods #410

merged 6 commits into from
Nov 16, 2022

Conversation

cquiroz
Copy link
Owner

@cquiroz cquiroz commented Nov 11, 2022

Partially fixes #254

* the number of seconds parts in the duration, may be negative
* @since 9
*/
def toSecondsPart: Int = (toSeconds / SECONDS_PER_MINUTE).toInt

Choose a reason for hiding this comment

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

Hmm, shouldn't this be using % to get the remainder instead of / ? Why doesn't the test suite catch this?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oh right, it say so on the docs 😄 the test suite is not good enough

Copy link
Owner Author

Choose a reason for hiding this comment

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

Great catch I added a fix

* the number of hours part in the duration, may be negative
* @since 9
*/
def toHoursPart: Int = (toHours / HOURS_PER_DAY).toInt
Copy link
Owner Author

Choose a reason for hiding this comment

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

They should all be remainders except toMillis

@cquiroz
Copy link
Owner Author

cquiroz commented Nov 12, 2022

@armanbilge I amended the implementation and added new tests to surface what was working wrong earlier in case you want to take a look

Comment on lines 1227 to 1231
* @return
* the nanoseconds within the second part of the length of the duration, from 0 to 999,999,999
* @since 9
*/
def toNanosPart: Int = (nanos / Duration.NANOS_PER_SECOND).toInt

Choose a reason for hiding this comment

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

Sorry, I still don't understand the calculation here. According to the constructor, nanos should always be between 0 and 999,999,999. So why do we need to do any calculation here?

* @param seconds
* the length of the duration in seconds, positive or negative
* @param nanos
* the nanoseconds within the second, from 0 to 999,999,999
*/
@SerialVersionUID(3078945930695997490L)
final class Duration private (private val seconds: Long, private val nanos: Int)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Right, let's delete toNanosPart and release, If somebody needs it PRs are welcome

Choose a reason for hiding this comment

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

Hm, I don't think it needed to be deleted. I think the correct implementation is to just return nanos here :)

Copy link

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@cquiroz cquiroz closed this Nov 13, 2022
@cquiroz cquiroz reopened this Nov 13, 2022
@cquiroz cquiroz closed this Nov 14, 2022
@cquiroz cquiroz reopened this Nov 14, 2022
@mergify mergify bot mentioned this pull request Nov 16, 2022
@cquiroz cquiroz merged commit 9ab921a into master Nov 16, 2022
@cquiroz cquiroz deleted the duration-9-methods branch November 16, 2022 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing methods for JDK9+
2 participants