mirror of
https://github.com/nushell/nushell.git
synced 2025-01-11 00:38:23 +01:00
fix date format (#5619)
This commit is contained in:
parent
a8db4f0b0e
commit
ef322a24c5
@ -1,10 +1,13 @@
|
|||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
|
use chrono::{DateTime, TimeZone};
|
||||||
|
|
||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
||||||
};
|
};
|
||||||
|
use std::fmt::{Display, Write};
|
||||||
|
|
||||||
use super::utils::parse_date_from_string;
|
use super::utils::parse_date_from_string;
|
||||||
|
|
||||||
@ -50,11 +53,11 @@ impl Command for SubCommand {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let format = call.opt::<String>(engine_state, stack, 0)?;
|
let format = call.opt::<Spanned<String>>(engine_state, stack, 0)?;
|
||||||
|
|
||||||
input.map(
|
input.map(
|
||||||
move |value| match &format {
|
move |value| match &format {
|
||||||
Some(format) => format_helper(value, format.as_str(), head),
|
Some(format) => format_helper(value, format.item.as_str(), format.span, head),
|
||||||
None => format_helper_rfc2822(value, head),
|
None => format_helper_rfc2822(value, head),
|
||||||
},
|
},
|
||||||
engine_state.ctrlc.clone(),
|
engine_state.ctrlc.clone(),
|
||||||
@ -90,34 +93,41 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_helper(value: Value, formatter: &str, span: Span) -> Value {
|
fn format_from<Tz: TimeZone>(date_time: DateTime<Tz>, formatter: &str, span: Span) -> Value
|
||||||
|
where
|
||||||
|
Tz::Offset: Display,
|
||||||
|
{
|
||||||
|
let mut formatter_buf = String::new();
|
||||||
|
let format = date_time.format(formatter);
|
||||||
|
|
||||||
|
match formatter_buf.write_fmt(format_args!("{}", format)) {
|
||||||
|
Ok(_) => Value::String {
|
||||||
|
val: formatter_buf,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
Err(_) => Value::Error {
|
||||||
|
error: ShellError::UnsupportedInput("invalid format".to_string(), span),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_helper(value: Value, formatter: &str, formatter_span: Span, head_span: Span) -> Value {
|
||||||
match value {
|
match value {
|
||||||
Value::Date { val, span: _ } => Value::String {
|
Value::Date { val, .. } => format_from(val, formatter, formatter_span),
|
||||||
val: val.format(formatter).to_string(),
|
Value::String { val, .. } => {
|
||||||
span,
|
let dt = parse_date_from_string(&val, formatter_span);
|
||||||
},
|
|
||||||
Value::String {
|
|
||||||
val,
|
|
||||||
span: val_span,
|
|
||||||
} => {
|
|
||||||
let dt = parse_date_from_string(&val, val_span);
|
|
||||||
match dt {
|
match dt {
|
||||||
Ok(x) => Value::String {
|
Ok(x) => format_from(x, formatter, formatter_span),
|
||||||
val: x.format(formatter).to_string(),
|
|
||||||
span,
|
|
||||||
},
|
|
||||||
Err(e) => e,
|
Err(e) => e,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::Nothing { span: _ } => {
|
Value::Nothing { .. } => {
|
||||||
let dt = Local::now();
|
let dt = Local::now();
|
||||||
Value::String {
|
format_from(dt, formatter, formatter_span)
|
||||||
val: dt.with_timezone(dt.offset()).format(formatter).to_string(),
|
|
||||||
span,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => Value::Error {
|
_ => Value::Error {
|
||||||
error: ShellError::DatetimeParseError(span),
|
error: ShellError::DatetimeParseError(head_span),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
crates/nu-command/tests/commands/date/format.rs
Normal file
14
crates/nu-command/tests/commands/date/format.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use nu_test_support::{nu, pipeline};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn formatter_not_valid() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
date format '%N'
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(actual.err.contains("invalid format"));
|
||||||
|
}
|
1
crates/nu-command/tests/commands/date/mod.rs
Normal file
1
crates/nu-command/tests/commands/date/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
mod format;
|
@ -6,6 +6,7 @@ mod cal;
|
|||||||
mod cd;
|
mod cd;
|
||||||
mod compact;
|
mod compact;
|
||||||
mod cp;
|
mod cp;
|
||||||
|
mod date;
|
||||||
mod def;
|
mod def;
|
||||||
mod default;
|
mod default;
|
||||||
mod drop;
|
mod drop;
|
||||||
|
Loading…
Reference in New Issue
Block a user