Skip to content

fix: add unix_epoch_second() with floor semantics for negative timestamps - #285

Merged
sylvestre merged 4 commits into
uutils:mainfrom
0xSoftBoi:fix/negative-fractional-epoch-floor
Jul 5, 2026
Merged

fix: add unix_epoch_second() with floor semantics for negative timestamps#285
sylvestre merged 4 commits into
uutils:mainfrom
0xSoftBoi:fix/negative-fractional-epoch-floor

Conversation

@0xSoftBoi

Copy link
Copy Markdown
Contributor

Summary

  • jiff::Timestamp::as_second() uses truncation toward zero, so @-1.5 gives -1 instead of the GNU-expected -2 (floor toward negative infinity)
  • Added ParsedDateTime::unix_epoch_second() that corrects for negative subsecond components by subtracting 1 from the truncated second when subsec_nanosecond() < 0
  • Added ParsedDateTime::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

Fixes#283

Test plan

  • 7 new tests covering positive, negative, fractional, zero, and large-value epoch timestamps
  • All 382 existing tests pass (cargo test)
  • Verified the exact examples from the issue: @-1.5-2, @-893375784.554767216-893375785

@codspeed-hq

codspeed-hqBot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 21 untouched benchmarks


Comparing 0xSoftBoi:fix/negative-fractional-epoch-floor (27b7360) with main (4b412e0)

Open in CodSpeed

@codecov

codecovBot commented Apr 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.32%. Comparing base (4b412e0) to head (27b7360).

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 
FlagCoverage Δ
macos_latest99.32% <100.00%> (+0.01%)⬆️
ubuntu_latest99.32% <100.00%> (+0.01%)⬆️
windows_latest13.73% <0.00%> (-0.32%)⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sylvestre

Copy link
Copy Markdown
Contributor

Seems that 2 lines aren't covered by the test

@tomagsx

Copy link
Copy Markdown

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.

@0xSoftBoi

Copy link
Copy Markdown
ContributorAuthor

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.

0xSoftBoiand others added 4 commits June 7, 2026 14:17
…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.
Fixesuutils#283
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
@0xSoftBoi
0xSoftBoiforce-pushed the fix/negative-fractional-epoch-floor branch from 55931c7 to 27b7360CompareJune 7, 2026 18:19
@sylvestre
sylvestre merged commit cb1cb02 into uutils:mainJul 5, 2026
21 checks passed
@0xSoftBoi
0xSoftBoi deleted the fix/negative-fractional-epoch-floor branch July 16, 2026 02:52
Sign up for freeto 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.

date input: negative fractional epoch (@-1.5) truncates instead of floors

3 participants

@0xSoftBoi@sylvestre@tomagsx