Implement Ord trait for Timespec #4519
Merged
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.
Note that
Timespec
does not perform any range checking and this change does not add any!This implementation of
Ord
assumes that pre-epochTimespec
s have negativesec
and positivensec
fields. Linux's and Darwin'sstruct timespec
functions handle pre-epoch timestamps with this "two steps back, half step forward" representation, though I cannot find any documentation that actually puts this in writing. This means that (say) -1.2 seconds is represented byTimespec { sec: -2_i64, nsec: 800_000_000_i32 }
, not something likeTimespec { sec: -1_i64, nsec: -200_000_000_i32 }
orTimespec { sec: -1_i64, nsec: 200_000_000_i32 }
.If we wish to codify this implementation detail, we could make
Timespec nsec
unsigned and add range checks assertingnsec <= 999_999_999_u32
. btw,struct Tm
also lacks range checking and uses signed integers for fields that can't be negative.