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.
This adds a new
SignedDuration::round
method that behaves likeSpan
,but for time (non-calendar) units only.
For the most part, we just copy
Span::round
as-is, but without thelargest
unit knob (which doesn't make sense for something likeSignedDuration
) or therelative
datetime knob (which isn't neededfor a duration with all invariant units, as is the case for
SignedDuration
). We can even reuse the same implementation forSpan
rounding: we convert the duration to a 128-bit signed integer of
nanoseconds, do the rounding and then convert back (and check for
overflow).
Note that we specifically don't both with adding rounding functionality
built into APIs like
Zoned::duration_since
orcivil::DateTime::duration_until
, unlike what we do forSpan
, sincecomposition works better for
SignedDuration
than it does forSpan
.Namely, in order to round a
Span
with calendar units, you need toprovide a relative datetime. By building rounding into the datetime
arithmetic routines, we absolve the caller of needing to manage this
carefully across two distinct operations. Since
SignedDuration
onlysupports invariant units, there is no purpose to providing a relative
datetime and rounding is thus fully orthogonal. Moreover, if we
overloaded methods like
Zoned::duration_since
, then they would go frominfallible to fallible, which is kind of a bummer.
Closes #187