Allow int input when using a formatstring
This commit is contained in:
parent
07e7c8c81f
commit
2ad05d3dd6
|
@ -379,42 +379,47 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
||||||
|
|
||||||
// If input is not a timestamp, try parsing it as a string
|
// If input is not a timestamp, try parsing it as a string
|
||||||
let span = input.span();
|
let span = input.span();
|
||||||
match input {
|
|
||||||
Value::String { val, .. } => {
|
let parse_as_string = |val: &str| {
|
||||||
match dateformat {
|
match dateformat {
|
||||||
Some(dt) => match DateTime::parse_from_str(val, &dt.0) {
|
Some(dt) => match DateTime::parse_from_str(val, &dt.0) {
|
||||||
Ok(d) => Value::date ( d, head ),
|
Ok(d) => Value::date ( d, head ),
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
match NaiveDateTime::parse_from_str(val, &dt.0) {
|
match NaiveDateTime::parse_from_str(val, &dt.0) {
|
||||||
Ok(d) => Value::date (
|
Ok(d) => Value::date (
|
||||||
DateTime::from_naive_utc_and_offset(
|
DateTime::from_naive_utc_and_offset(
|
||||||
d,
|
d,
|
||||||
*Local::now().offset(),
|
*Local::now().offset(),
|
||||||
),
|
|
||||||
head,
|
|
||||||
),
|
),
|
||||||
Err(_) => {
|
head,
|
||||||
Value::error (
|
),
|
||||||
ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) },
|
Err(_) => {
|
||||||
head,
|
Value::error (
|
||||||
)
|
ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) },
|
||||||
}
|
head,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// Tries to automatically parse the date
|
// Tries to automatically parse the date
|
||||||
// (i.e. without a format string)
|
// (i.e. without a format string)
|
||||||
// and assumes the system's local timezone if none is specified
|
// and assumes the system's local timezone if none is specified
|
||||||
None => match parse_date_from_string(val, span) {
|
None => match parse_date_from_string(val, span) {
|
||||||
Ok(date) => Value::date (
|
Ok(date) => Value::date (
|
||||||
date,
|
date,
|
||||||
span,
|
span,
|
||||||
),
|
),
|
||||||
Err(err) => err,
|
Err(err) => err,
|
||||||
},
|
},
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match input {
|
||||||
|
Value::String { val, .. } => parse_as_string(val),
|
||||||
|
Value::Int { val, .. } => parse_as_string(&val.to_string()),
|
||||||
|
|
||||||
// Propagate errors by explicitly matching them before the final case.
|
// Propagate errors by explicitly matching them before the final case.
|
||||||
Value::Error { .. } => input.clone(),
|
Value::Error { .. } => input.clone(),
|
||||||
other => Value::error(
|
other => Value::error(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user