Repository navigation
date: take %p and %P from the locale - #15130
Conversation
Expand them to the locale's D_FMT, T_FMT and T_FMT_AMPM, read once, instead of jiff's C formats. A width or flag applies to the whole expansion, as in GNU date, and %x is expanded before an out-of-range year is substituted into the default format. Fixes uutils#10284
Use the locale's AM_STR and PM_STR, read once, instead of jiff's AM and PM, for %p and %P and so for the %p inside %r. A locale without markers, like fr_FR, gets empty ones, as in GNU date.
| }; | ||
| output.push_str(&format[copied..i]); | ||
| // The marker is put into a format, so a `%` in it has to be doubled. | ||
| for c in marker.chars() { |
There was a problem hiding this comment.
could be output.push_str(&marker.replace('%', "%%")) like below, no?
| static LOCALE_AMPM_MARKERS: LazyLock<[&'static str; 2]> = LazyLock::new(|| { | ||
| let mut markers = POSIX_AMPM_MARKERS; | ||
| for (marker, item) in markers.iter_mut().zip([libc::AM_STR, libc::PM_STR]) { | ||
| if let Some(value) = langinfo(item).and_then(|value| String::from_utf8(value).ok()) { |
There was a problem hiding this comment.
with a non UTF-8 locale (zh_CN.GB18030) this silently falls back to AM/PM, is it ok?
There was a problem hiding this comment.
Same as main, which prints PM there too. date doesn't handle non-UTF-8 locales in general: %B and %A come out in UTF-8 under zh_CN.GB18030, where GNU uses GB18030. I can open a separate issue for that.
Merging this PR will degrade performance by 3.28%
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
|
the new test ---- test_date::test_date_format_locale_date_and_time stdout ----
bin: "/home/runner/work/coreutils/coreutils/target/wasm32-wasip1/debug/coreutils.wasm"
run: /home/runner/work/coreutils/coreutils/target/wasm32-wasip1/debug/coreutils.wasm date -d 1997-01-19 20:17:48 +%x
thread 'test_date::test_date_format_locale_date_and_time' (21995) panicked at tests/by-util/test_date.rs:2211:14:
assertion failed: `(left == right)`
Diff < left / right > :
<01/19/97
>01/19/1997
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
|
Thanks, my test. Fix in #15236. |
%pand%Pprinted jiff'sAM/PMin every locale. They now use the locale'sAM_STR/PM_STR, read once withnl_langinfo. Locales without markers (fr_FR, de_DE) get empty ones as in GNU 9.12, and the%pinside%rgoes the same way, so fr_FR%ris08:17:48like GNU. Width and case flags keep their rules (%10p,%^p,%#p). It's one more arm in #11068's format scanner.Based on #11068; until that merges, the diff here shows its commit too.
Closes #15117