Skip to content

Commit

Permalink
into datetime: noop when input is a datetime (nushell#14845)
Browse files Browse the repository at this point in the history
# Description

- Closes nushell#14839

When the input to `into datetime` is a datetime, it will return it like
other `into` commands.
# User-Facing Changes

Before, using `into datetime` with a datetime as input would return an
error, now it will return the input.
# Tests + Formatting

Added test `takes_datetime`.
# After Submitting

Doc file is automatically generated.
  • Loading branch information
Tyarel8 authored Jan 16, 2025
1 parent 6eff420 commit 0587308
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/nu-command/src/conversions/into/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Command for SubCommand {
fn signature(&self) -> Signature {
Signature::build("into datetime")
.input_output_types(vec![
(Type::Date, Type::Date),
(Type::Int, Type::Date),
(Type::String, Type::Date),
(Type::List(Box::new(Type::String)), Type::List(Box::new(Type::Date))),
Expand Down Expand Up @@ -208,6 +209,12 @@ impl Command for SubCommand {
#[allow(clippy::inconsistent_digit_grouping)]
result: example_result_1(1614434140_000000000),
},
Example {
description: "Leave it as it is when the input is already a datetime",
example: "1614434140 * 1_000_000_000 | into datetime | into datetime",
#[allow(clippy::inconsistent_digit_grouping)]
result: example_result_1(1614434140_000000000),
},
Example {
description: "Convert list of timestamps to datetimes",
example: r#"["2023-03-30 10:10:07 -05:00", "2023-05-05 13:43:49 -05:00", "2023-06-05 01:37:42 -05:00"] | into datetime"#,
Expand Down Expand Up @@ -267,6 +274,11 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
let timezone = &args.zone_options;
let dateformat = &args.format_options;

// noop if the input is already a datetime
if matches!(input, Value::Date { .. }) {
return input.clone();
}

// Let's try dtparse first
if matches!(input, Value::String { .. }) && dateformat.is_none() {
let span = input.span();
Expand Down Expand Up @@ -636,6 +648,26 @@ mod tests {
assert_eq!(actual, expected)
}

#[test]
fn takes_datetime() {
let timezone_option = Some(Spanned {
item: Zone::Local,
span: Span::test_data(),
});
let args = Arguments {
zone_options: timezone_option,
format_options: None,
cell_paths: None,
};
let expected = Value::date(
Local.timestamp_opt(1614434140, 0).unwrap().into(),
Span::test_data(),
);
let actual = action(&expected, &args, Span::test_data());

assert_eq!(actual, expected)
}

#[test]
fn takes_timestamp_without_timezone() {
let date_str = Value::test_string("1614434140000000000");
Expand Down

0 comments on commit 0587308

Please sign in to comment.