fix(time): show dates for files modified before the UNIX epoch#1826
Open
xfocus3 wants to merge 1 commit into
Open
fix(time): show dates for files modified before the UNIX epoch#1826xfocus3 wants to merge 1 commit into
xfocus3 wants to merge 1 commit into
Conversation
`systemtime_to_naivedatetime` called `duration_since(UNIX_EPOCH).ok()?`, which returns `Err` for any timestamp before 1970 and was silently turned into `None`. As a result eza rendered pre-epoch mtimes/atimes/ctimes as `-` instead of a real date, while `ls` shows the correct date. Handle the `Err` case by recovering the negative offset from `err.duration()`, flooring towards negative infinity when there is a sub-second component, and building the timestamp with `DateTime::from_timestamp`, which accepts negative seconds. Add unit tests covering pre-epoch, sub-second pre-epoch, the epoch, and a normal post-epoch time. Closes eza-community#1668
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Files whose modification (or access/change) time is before the UNIX epoch (1970-01-01) render their date as
-in eza, even thoughlsshows the correct date (e.g.1901-12-13 ...). See #1668.Root cause
File::systemtime_to_naivedatetimeinsrc/fs/file.rsdid:SystemTime::duration_sincereturnsErrwhenever the time is earlier than the epoch, and.ok()?silently converted thatErrintoNone. Every pre-epoch timestamp therefore became "no date", which the table renderer prints as-.Fix
Handle the
Errbranch instead of discarding it:Ok(d): behave as before (positive seconds + nanos).Err(e): recover the absolute offset frome.duration(), negate the seconds, and when there is a sub-second part floor towards negative infinity (secs -= 1; nanos = 1_000_000_000 - nanos) so the value matches how Unix timestamps count time before 1970.The timestamp is then built with
chrono::DateTime::from_timestamp(secs, nanos), which already accepts negative seconds, keeping the existingOption<NaiveDateTime>return type and.naive_local()conversion. The change is localized to this one function.lsis treated as the ground-truth behavior.Tests
Added a
#[cfg(test)] mod systemtime_to_naivedatetime_testcovering:UNIX_EPOCH - 2_147_483_648s, year 1901, timestamp-2_147_483_648),-10.25s-> timestamp-11, nanos750_000_000).cargo build,cargo test fs::file,cargo clippy -- -D warnings, andcargo fmt --checkall pass locally on the pinned toolchain (1.90).Closes #1668