Skip to content

Commit

Permalink
span: add Span::to_duration and TryFrom impls for Duration
Browse files Browse the repository at this point in the history
This PR dips our toes into the water of integration with
`std::time::Duration`. I do somewhat expect there to be more integration
points, particularly in the datetime arithmetic APIs, but this at least
paves a path for folks to _correctly_ convert between a `Duration` and a
`Span`. The error cases for the new APIs will ensure you can never
misuse these APIs.

This adds a few new APIs:

* `Span::is_positive`, since we already have `Span::is_negative` and
  `Span::is_zero`.
* `Span::signum`, since it's signed and it's sometimes convenient.
* `TryFrom<std::time::Duration> for Span`.
* `TryFrom<Span> for std::time::Duration` that returns an error if
  the span is negative or has non-zero units bigger than days.
* `Span::to_duration` that requires a relative date and can convert any
  positive span into a `Duration` (modulo overflow).

Partially addresses #21

Fixes #40
  • Loading branch information
BurntSushi committed Jul 26, 2024
1 parent 7f1d3c3 commit 3aa202c
Showing 1 changed file with 459 additions and 1 deletion.
Loading

0 comments on commit 3aa202c

Please sign in to comment.