mirror of
https://github.com/nushell/nushell.git
synced 2026-12-03 02:27:37 +08:00
fix(format date): resolve %r when the locale's am/pm fallback also carries %O (#18924)
Follow-up to #18918 (which fixed #15266). Two of the affected locales still fail on `main`, and the set of affected locales turned out to be larger than that PR said. Both found while working up the upstream chrono fix @fdncred asked for. # Description ## The bug #18918 left behind `az_IR` and `fa_IR` have an **empty** `t_fmt_ampm`, so #18918 deliberately leaves `%r` alone for chrono to resolve — that carve-out is what keeps `de_DE`/`fr_FR`/`nl_NL` from rendering blank. But chrono's fallback for an empty am/pm form is the locale's `t_fmt`, and for these two that is `%OH:%OM:%OS`. The modifier we just removed comes straight back in: ``` az_IR T_FMT_AMPM="" T_FMT="%OH:%OM:%OS" chrono-today=Err merged=Err proposed=Ok("20:00:12") fa_IR T_FMT_AMPM="" T_FMT="%OH:%OM:%OS" chrono-today=Err merged=Err proposed=Ok("20:00:12") de_DE T_FMT_AMPM="" T_FMT="%T" chrono-today=Ok("20:00:12") merged=Ok("20:00:12") proposed=Ok("20:00:12") fr_FR T_FMT_AMPM="" T_FMT="%T" chrono-today=Ok("20:00:12") merged=Ok("20:00:12") proposed=Ok("20:00:12") nl_NL T_FMT_AMPM="" T_FMT="%T" chrono-today=Ok("20:00:12") merged=Ok("20:00:12") proposed=Ok("20:00:12") ``` So `LC_TIME=fa_IR.UTF-8` still gets `nu::shell::type_mismatch, invalid format` from `date now | format date %r`. The fix is to make chrono's own fallback explicit: when `t_fmt_ampm` is empty, substitute `t_fmt` ourselves, so it goes through the stripping path like everything else. `de_DE`, `fr_FR` and `nl_NL` are byte-identical, since their `t_fmt` is `%T`. ## The affected locale list was wrong #18918 named `th_TH` and `lo_LA` and admitted the set was not enumerated exhaustively. It is nine, from scanning every `LC_TIME` block in `pure-rust-locales` 0.8.2: ``` az_IR: D_FMT=%Oy/%Om/%Od | D_T_FMT=%A %Oe %B %Oy، %OH:%OM:%OS | T_FMT=%OH:%OM:%OS fa_IR: D_FMT=%Oy/%Om/%Od | D_T_FMT=%A %Oe %B %Oy، %OH:%OM:%OS | T_FMT=%OH:%OM:%OS lo_LA: D_FMT=%d/%m/%Ey | D_T_FMT=%a %e %b %Ey, %H:%M:%S lzh_TW: D_FMT=%OC%Oy年%B%Od日 | D_T_FMT=%OC%Oy年%B%Od日 (%A) %OH時%OM分%OS秒 | T_FMT=%OH時%OM分%OS秒 | T_FMT_AMPM=%p %OI時%OM分%OS秒 mnw_MM: D_FMT=%OC%Oy %b %Od %A | D_T_FMT=%OC%Oy %b %Od %A %OI:%OM:%OS %Op %Z | T_FMT=%OI:%OM:%OS %p my_MM: D_FMT=%OC%Oy %b %Od %A | D_T_FMT=%OC%Oy %b %Od %A %OI:%OM:%OS %Op %Z | T_FMT=%OI:%OM:%OS %p or_IN: D_FMT=%Od-%Om-%Oy | D_T_FMT=%Oe %B %Oy %OI:%OM:%OS %p %Z | T_FMT=%OI:%OM:%OS %p shn_MM: D_FMT=%OC%Oy %b %Od %A | D_T_FMT=%OC%Oy %b %Od %A %OI:%OM:%OS %Op %Z | T_FMT=%OH:%OM:%OS %p th_TH: D_FMT=%d/%m/%Ey | D_T_FMT=%a %e %b %Ey, %H:%M:%S ``` The good news is that the existing approach already covers the seven that were never named — chrono has `%C`, `%e`, `%I`, `%p` and the rest, so stripping is enough. `lzh_TW` `%c` renders `2021年十月22日 (週五) 20時00分12秒`, `my_MM` `%X` renders `08:00:12 ညနေ`. Only the two `%r` cases above needed code. A test now pins all nine so the next locale table bump cannot quietly break one. # User-Facing Changes `format date` with `%r` under `LC_TIME=az_IR` or `fa_IR` renders instead of erroring. Nothing else changes: every other locale, including the empty-am/pm ones, produces the same bytes as before. # Tests + Formatting - `resolves_the_am_pm_fallback_when_it_also_carries_a_modifier` — the new case, `az_IR`/`fa_IR`. - `falls_back_to_the_plain_time_format_when_the_locale_has_no_am_pm` — the old `de_DE`/`fr_FR` test, updated for the new resolution and still asserting the rendered output is unchanged. - `renders_every_locale_that_carries_an_alternative_modifier` — `%x`, `%X`, `%c`, `%r` under all nine. Mutation-checked rather than trusted green. Dropping the fallback turns three tests red with the reported error: ``` test strings::format::date::test::resolves_the_am_pm_fallback_when_it_also_carries_a_modifier ... FAILED test strings::format::date::test::falls_back_to_the_plain_time_format_when_the_locale_has_no_am_pm ... FAILED test strings::format::date::test::renders_every_locale_that_carries_an_alternative_modifier ... FAILED az_IR %r did not render: Error(TypeMismatch { err_message: "invalid format", span: Span(TEST) }) ``` Green: ``` $ cargo test -p nu-command --lib test result: ok. 814 passed; 0 failed; 2 ignored; 0 filtered out; across 4 groups $ cargo test -p nu-command --test tests test result: ok. 2930 passed; 0 failed; 35 ignored; 0 filtered out; across 29 groups ``` `cargo fmt --all -- --check` and `cargo clippy -p nu-command --all-targets` are clean. Linux, Rust stable, `nu-command` only. I did not run the full workspace suite or the Windows/macOS paths. # After Submitting The upstream fix @fdncred asked for is chronotope/chrono#1816 — one line in chrono's `parse_next_item` that skips `%E`/`%O` per POSIX, with the same nine locales tested. If that lands and nushell picks up the release, this whole `resolve_locale_specifiers` layer and the `pure-rust-locales` direct dependency can be deleted. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: aron-intframe <intedu2024@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
aron-intframe
Claude Opus 5
parent
9c36ea5b6b
commit
d6a9f6bc26
@@ -229,14 +229,25 @@ fn expand_locale_specifiers(formatter: &str, locale: Locale) -> String {
|
||||
Some('x') => Some(locale_match!(locale => LC_TIME::D_FMT)),
|
||||
Some('X') => Some(locale_match!(locale => LC_TIME::T_FMT)),
|
||||
Some('c') => Some(locale_match!(locale => LC_TIME::D_T_FMT)),
|
||||
Some('r') => Some(locale_match!(locale => LC_TIME::T_FMT_AMPM)),
|
||||
Some('r') => {
|
||||
// A locale can have no am/pm form at all — `t_fmt_ampm` is ""
|
||||
// for `de_DE`, `fr_FR`, `nl_NL`, `az_IR` and `fa_IR`. chrono
|
||||
// falls back to the plain time format there, so do that same
|
||||
// substitution here: handing `%r` back to chrono would let its
|
||||
// fallback reintroduce the modifier we are removing, which is
|
||||
// what left `az_IR`/`fa_IR` (`t_fmt` = `%OH:%OM:%OS`) failing.
|
||||
let ampm = locale_match!(locale => LC_TIME::T_FMT_AMPM);
|
||||
Some(if ampm.is_empty() {
|
||||
locale_match!(locale => LC_TIME::T_FMT)
|
||||
} else {
|
||||
ampm
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// A locale can leave one of these empty — `de_DE`, `fr_FR` and `nl_NL`
|
||||
// all have no am/pm form, so `t_fmt_ampm` is "". Substituting that would
|
||||
// erase the output, where chrono falls back to the plain time format.
|
||||
// Leave the specifier alone and let chrono do what it already does.
|
||||
// An empty expansion would erase the output, so leave the specifier
|
||||
// alone and let chrono resolve it.
|
||||
match expansion {
|
||||
Some(fmt) if !fmt.is_empty() => {
|
||||
chars.next();
|
||||
@@ -439,13 +450,13 @@ mod test {
|
||||
assert_eq!(resolve_locale_specifiers("%Od", Locale::en_US), "%d");
|
||||
}
|
||||
|
||||
/// The regression this nearly shipped with: `de_DE`, `fr_FR` and `nl_NL`
|
||||
/// have no am/pm time format, and substituting the empty string for `%r`
|
||||
/// erased the output instead of leaving chrono its own fallback.
|
||||
/// `de_DE`, `fr_FR` and `nl_NL` have no am/pm time format, so `t_fmt_ampm`
|
||||
/// is "". Substituting the empty string for `%r` would erase the output;
|
||||
/// the plain time format is what chrono itself falls back to.
|
||||
#[test]
|
||||
fn leaves_a_specifier_alone_when_the_locale_has_no_string_for_it() {
|
||||
assert_eq!(resolve_locale_specifiers("%r", Locale::de_DE), "%r");
|
||||
assert_eq!(resolve_locale_specifiers("%r", Locale::fr_FR), "%r");
|
||||
fn falls_back_to_the_plain_time_format_when_the_locale_has_no_am_pm() {
|
||||
assert_eq!(resolve_locale_specifiers("%r", Locale::de_DE), "%T");
|
||||
assert_eq!(resolve_locale_specifiers("%r", Locale::fr_FR), "%T");
|
||||
assert_eq!(
|
||||
resolve_locale_specifiers("%r", Locale::en_US),
|
||||
"%I:%M:%S %p"
|
||||
@@ -459,10 +470,59 @@ mod test {
|
||||
assert_eq!(
|
||||
format_from(dt, "%r", span, Locale::de_DE),
|
||||
Value::string("13:45:00", span),
|
||||
"an empty am/pm format must fall through to chrono, not blank the output"
|
||||
"an empty am/pm format must not blank the output"
|
||||
);
|
||||
}
|
||||
|
||||
/// `az_IR` and `fa_IR` have both an empty `t_fmt_ampm` *and* a `t_fmt` that
|
||||
/// carries `%O`. Handing `%r` back to chrono let its own fallback pull that
|
||||
/// modifier in again, so those two still failed after #18918.
|
||||
#[test]
|
||||
fn resolves_the_am_pm_fallback_when_it_also_carries_a_modifier() {
|
||||
assert_eq!(resolve_locale_specifiers("%r", Locale::az_IR), "%H:%M:%S");
|
||||
assert_eq!(resolve_locale_specifiers("%r", Locale::fa_IR), "%H:%M:%S");
|
||||
|
||||
let dt = Utc
|
||||
.with_ymd_and_hms(2026, 8, 27, 13, 45, 0)
|
||||
.single()
|
||||
.expect("valid date");
|
||||
let span = Span::test_data();
|
||||
assert_eq!(
|
||||
format_from(dt, "%r", span, Locale::az_IR),
|
||||
Value::string("13:45:00", span)
|
||||
);
|
||||
}
|
||||
|
||||
/// Every locale whose `LC_TIME` strings carry `%E` or `%O` must render.
|
||||
#[test]
|
||||
fn renders_every_locale_that_carries_an_alternative_modifier() {
|
||||
let dt = Utc
|
||||
.with_ymd_and_hms(2026, 8, 27, 13, 45, 0)
|
||||
.single()
|
||||
.expect("valid date");
|
||||
let span = Span::test_data();
|
||||
|
||||
for locale in [
|
||||
Locale::az_IR,
|
||||
Locale::fa_IR,
|
||||
Locale::lo_LA,
|
||||
Locale::lzh_TW,
|
||||
Locale::mnw_MM,
|
||||
Locale::my_MM,
|
||||
Locale::or_IN,
|
||||
Locale::shn_MM,
|
||||
Locale::th_TH,
|
||||
] {
|
||||
for formatter in ["%x", "%X", "%c", "%r"] {
|
||||
let value = format_from(dt, formatter, span, locale);
|
||||
assert!(
|
||||
matches!(value, Value::String { .. }),
|
||||
"{locale:?} {formatter} did not render: {value:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_literal_percent_and_unknown_specifiers_alone() {
|
||||
assert_eq!(resolve_locale_specifiers("%%x", Locale::th_TH), "%%x");
|
||||
|
||||
Reference in New Issue
Block a user