Skip to content
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

Match 'plus' with the corresponding 'minus' functions #49

Merged
merged 6 commits into from
Sep 21, 2020
Merged

Conversation

dkhalanskyjb
Copy link
Collaborator

Solves #42

* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
*/
public fun Instant.minus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
plus(-value.toLong(), unit)
Copy link
Member

Choose a reason for hiding this comment

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

Better -(value.toLong())

Copy link
Member

Choose a reason for hiding this comment

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

Though it's likely the same, but the order of operations would be more explicit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
*/
public fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant =
plus(-value, unit, timeZone)
Copy link
Member

Choose a reason for hiding this comment

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

Is the negation always overflow-safe? I.e. it certainly can overflow at MIN_VALUE, but would this overflow be compensated later?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, this would be silently wrong. I pushed a fix for this and a similar problem with LocalDate.minus(Int, ...) on JVM.

Copy link
Member

Choose a reason for hiding this comment

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

Could you also ensure that instant.minus(Long.MIN_VALUE, DateTimeUnit.NANOSECOND) > instant?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Since this edge case is a requirement, consistency also required solving a whole new can of worms with DateTimePeriod, which I also did.

@dkhalanskyjb dkhalanskyjb merged commit f9f804f into master Sep 21, 2020
@dkhalanskyjb dkhalanskyjb deleted the minus branch September 21, 2020 10:35
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.

2 participants