fix: add unix_epoch_second() with floor semantics for negative timestamps#285
fix: add unix_epoch_second() with floor semantics for negative timestamps#2850xSoftBoi wants to merge 4 commits intouutils:mainfrom
Conversation
…amps jiff::Timestamp::as_second() uses truncation toward zero, so a timestamp of -1.5s returns -1. GNU date's %s format uses floor (toward negative infinity), returning -2 for the same value. Add ParsedDateTime::unix_epoch_second() which corrects for this by detecting a negative subsec_nanosecond and subtracting 1 from the truncated second. Also add subsec_nanosecond() that re-normalizes to the non-negative [0, 1_000_000_000) range matching the GNU timespec convention. The uutils date command can use these methods to produce correct %s output for negative fractional epoch inputs like @-1.5. Fixes uutils#283 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Merging this PR will degrade performance by 4.41%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | parse_weekday |
30 µs | 31.4 µs | -4.41% |
Comparing 0xSoftBoi:fix/negative-fractional-epoch-floor (234b720) with main (7440dd9)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #285 +/- ##
==========================================
+ Coverage 99.30% 99.32% +0.01%
==========================================
Files 20 20
Lines 3894 3982 +88
Branches 122 124 +2
==========================================
+ Hits 3867 3955 +88
Misses 26 26
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Seems that 2 lines aren't covered by the test |
|
Review bump here as well. The main actionable feedback seems to be the two uncovered lines plus the CodSpeed warning. I’m happy to tighten that up if needed, but I’d also appreciate a maintainer pass on whether the GNU-compatible floor behavior is the right shape for the library before I keep iterating. |
The let-else with panic! left one line reachable only on test failure, which showed up as uncovered on codecov/patch. Use matches! + assert! to reach the same invariant with 100% patch coverage.
|
Addressed the coverage feedback: the remaining uncovered line on the patch was the panic arm of the let-else in the extended accessor test. Swapped it for a matches! + assert! pair so the invariant is still checked but the patch lands at 100%. CI should now be green modulo the CodSpeed perf flag. |
Summary
jiff::Timestamp::as_second()uses truncation toward zero, so@-1.5gives-1instead of the GNU-expected-2(floor toward negative infinity)ParsedDateTime::unix_epoch_second()that corrects for negative subsecond components by subtracting 1 from the truncated second whensubsec_nanosecond() < 0ParsedDateTime::subsec_nanosecond()that re-normalizes to the non-negative[0, 1_000_000_000)range matching the GNUtimespecconventiondatecommand can use these methods to produce correct%soutputFixes #283
Test plan
cargo test)@-1.5→-2,@-893375784.554767216→-893375785