mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
Allow duration to be added to date (#14295)
# Description Fixes #14294 - Turned out to be a whole lot easier than I expected, but please double-check me on this, since it's an area I haven't been in before. # User-Facing Changes Allow date to be added to a duration type. # Tests + Formatting Tests added: * Duration + Date is allowed * Duration - Date is not allowed
This commit is contained in:
parent
a3c145432e
commit
f0cb2dafbb
@ -88,6 +88,7 @@ pub fn math_result_type(
|
|||||||
(Type::Float, Type::Number) => (Type::Number, None),
|
(Type::Float, Type::Number) => (Type::Number, None),
|
||||||
(Type::String, Type::String) => (Type::String, None),
|
(Type::String, Type::String) => (Type::String, None),
|
||||||
(Type::Date, Type::Duration) => (Type::Date, None),
|
(Type::Date, Type::Duration) => (Type::Date, None),
|
||||||
|
(Type::Duration, Type::Date) => (Type::Date, None),
|
||||||
(Type::Duration, Type::Duration) => (Type::Duration, None),
|
(Type::Duration, Type::Duration) => (Type::Duration, None),
|
||||||
(Type::Filesize, Type::Filesize) => (Type::Filesize, None),
|
(Type::Filesize, Type::Filesize) => (Type::Filesize, None),
|
||||||
|
|
||||||
|
@ -2444,6 +2444,17 @@ impl Value {
|
|||||||
Ok(Value::string(lhs.to_string() + rhs, span))
|
Ok(Value::string(lhs.to_string() + rhs, span))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(Value::Duration { val: lhs, .. }, Value::Date { val: rhs, .. }) => {
|
||||||
|
if let Some(val) = rhs.checked_add_signed(chrono::Duration::nanoseconds(*lhs)) {
|
||||||
|
Ok(Value::date(val, span))
|
||||||
|
} else {
|
||||||
|
Err(ShellError::OperatorOverflow {
|
||||||
|
msg: "addition operation overflowed".into(),
|
||||||
|
span,
|
||||||
|
help: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
(Value::Date { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
(Value::Date { val: lhs, .. }, Value::Duration { val: rhs, .. }) => {
|
||||||
if let Some(val) = lhs.checked_add_signed(chrono::Duration::nanoseconds(*rhs)) {
|
if let Some(val) = lhs.checked_add_signed(chrono::Duration::nanoseconds(*rhs)) {
|
||||||
Ok(Value::date(val, span))
|
Ok(Value::date(val, span))
|
||||||
|
@ -37,6 +37,11 @@ fn date_minus_duration() -> TestResult {
|
|||||||
run_test(input, expected)
|
run_test(input, expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn duration_minus_date_not_supported() -> TestResult {
|
||||||
|
fail_test("2day - 2023-04-22", "doesn't support these values")
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn date_plus_duration() -> TestResult {
|
fn date_plus_duration() -> TestResult {
|
||||||
let input = "2023-04-18 + 2day | format date %Y-%m-%d";
|
let input = "2023-04-18 + 2day | format date %Y-%m-%d";
|
||||||
@ -44,6 +49,13 @@ fn date_plus_duration() -> TestResult {
|
|||||||
run_test(input, expected)
|
run_test(input, expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn duration_plus_date() -> TestResult {
|
||||||
|
let input = "2024-11-10T00:00:00-00:00 + 4hr | format date";
|
||||||
|
let expected = "Sun, 10 Nov 2024 04:00:00 +0000";
|
||||||
|
run_test(input, expected)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn block_not_first_class_def() -> TestResult {
|
fn block_not_first_class_def() -> TestResult {
|
||||||
fail_test(
|
fail_test(
|
||||||
|
Loading…
Reference in New Issue
Block a user