Skip to content

ls: honor LC_ALL for locale time-style dates - #16987

Open
kutluhaneth46 wants to merge 3 commits into
uutils:mainfrom
kutluhaneth46:fix/ls-locale-time-style
Open

kutluhaneth46 wants to merge 3 commits into
uutils:mainfrom
kutluhaneth46:fix/ls-locale-time-style

Conversation

@kutluhaneth46

Copy link
Copy Markdown

Fixes #16979.

ls -l always printed English month-first dates. GNU uses the locale for both the month abbreviation and the day/month order (18 nov. in French, 18. Nov in German Swiss).

I wire i18n-datetime into ls, pick the recent/older format from nl_langinfo(D_FMT) when LC_TIME is not C/POSIX, and replace %b with padded ABMON names (periods kept, widths aligned like GNU abformat).

Added a unix test that checks fr_FR.UTF-8, de_CH.UTF-8, and C when those locales are installed.

Default -l dates ignored the locale. Infer day/month order from
D_FMT and substitute padded ABMON month names so fr_FR and de_CH
match GNU ls with working LC_TIME catalogs.
Comment thread tests/by-util/test_ls.rs Outdated
at.touch("stamp");
let file = at.make_file("stamp");
// Fixed recent timestamp so the locale date column is predictable.
let stamp = SystemTime::UNIX_EPOCH + Duration::from_secs(1_794_991_440); // 2026-11-18 08:44 UTC

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this date stops being "recent" around May 2027 and the test will then fail, could you please base it on now() instead?

let Some(abbr) = months.get(month_idx) else {
return Cow::Borrowed(format);
};
Cow::Owned(format.replace("%b", abbr).replace("%h", abbr))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this breaks --time-style=+%%b, which should print a literal %b
please handle %% and add a test for it

not(target_os = "cygwin"),
not(target_os = "redox")
))]
fn padded_locale_abmons_inner() -> Option<[String; 12]> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost the same code as get_locale_months_inner, could be dedup, no?

use core::ffi::CStr;

unsafe {
libc::setlocale(libc::LC_TIME, c"".as_ptr());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a SAFETY comment on these unsafe blocks (here and below)

(Some(day), Some(month)) if day < month => {
let after_day = &d_fmt[day..];
if after_day.starts_with("%d.") || after_day.starts_with("%e.") {
("%e. %b %H:%M", "%e. %b %Y")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the older form has one space and the others have two, so the columns won't line up between old and recent files, no?

Handle %% in month-token expansion, keep older-form year spacing
aligned with recent HH:MM, share ABMON reads, add SAFETY notes,
and base the locale ls test on now().
@kutluhaneth46

Copy link
Copy Markdown
Author

@sylvestre Thanks for the review. I pushed a follow-up that:

  1. bases the locale test on now()
  2. keeps %%b as a literal %b (with a test)
  3. dedups the Unix ABMON read path
  4. adds SAFETY notes on the locale unsafe blocks
  5. uses two spaces before %Y in the day-first older form so columns stay aligned with HH:MM

Merge identical match arms and use contains for abmon widths.
Accept an optional day-dot in the fr_FR locale ls assertion so
FreeBSD's D_FMT shape matches.

This branch has not been deployed

No deployments
Sign up for free to 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.

ls: -l ignores LC_ALL for date

2 participants