make date format supports locale (#6306)

* add --locale flag to make output support locale

* implement again based on nu-utils get_system_locale_string

* add comment
This commit is contained in:
WindSoilder
2022-08-14 21:07:04 +08:00
committed by GitHub
parent 6145f734b7
commit 21770367e2
5 changed files with 82 additions and 6 deletions

View File

@ -1,5 +1,4 @@
use chrono::Local;
use chrono::{DateTime, TimeZone};
use chrono::{DateTime, Local, Locale, TimeZone};
use nu_engine::CallExt;
use nu_protocol::{
@ -7,6 +6,7 @@ use nu_protocol::{
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};
use nu_utils::locale::get_system_locale_string;
use std::fmt::{Display, Write};
use super::utils::parse_date_from_string;
@ -98,7 +98,13 @@ where
Tz::Offset: Display,
{
let mut formatter_buf = String::new();
let format = date_time.format(formatter);
let locale: Locale = get_system_locale_string()
.map(|l| l.replace('-', "_")) // `chrono::Locale` needs something like `xx_xx`, rather than `xx-xx`
.unwrap_or_else(|| String::from("en_US"))
.as_str()
.try_into()
.unwrap_or(Locale::en_US);
let format = date_time.format_localized(formatter, locale);
match formatter_buf.write_fmt(format_args!("{}", format)) {
Ok(_) => Value::String {