mirror of
https://github.com/nushell/nushell.git
synced 2025-04-20 11:18:20 +02:00
Issue #12289, can be closed when this is merged # Description Currently, the ``into datetime`` command's signature indicates that it supports input as record, but it was actually not supported. This PR implements this feature. # User-Facing Changes ``into datetime``'s signature changed (see comments) **Happy paths** Note: I'm in +02:00 timezone. ```nushell > date now | into record | into datetime Fri, 4 Apr 2025 18:32:34 +0200 (now) > {year: 2025, month: 12, day: 6, second: 59} | into datetime | into record ╭─────────────┬────────╮ │ year │ 2025 │ │ month │ 12 │ │ day │ 6 │ │ hour │ 0 │ │ minute │ 0 │ │ second │ 59 │ │ millisecond │ 0 │ │ microsecond │ 0 │ │ nanosecond │ 0 │ │ timezone │ +02:00 │ ╰─────────────┴────────╯ > {day: 6, second: 59, timezone: '-06:00'} | into datetime | into record ╭─────────────┬────────╮ │ year │ 2025 │ │ month │ 4 │ │ day │ 6 │ │ hour │ 0 │ │ minute │ 0 │ │ second │ 59 │ │ millisecond │ 0 │ │ microsecond │ 0 │ │ nanosecond │ 0 │ │ timezone │ -06:00 │ ╰─────────────┴────────╯ ``` **Edge cases** ```nushell {} | into datetime Fri, 4 Apr 2025 18:35:19 +0200 (now) ``` **Error paths** - A key has a wrong type ```nushell > {month: 12, year: '2023'} | into datetime Error: nu:🐚:only_supports_this_input_type × Input type not supported. ╭─[entry #8:1:19] 1 │ {month: 12, year: '2023'} | into datetime · ───┬── ──────┬────── · │ ╰── only int input data is supported · ╰── input type: string ╰──── ``` ```nushell > {month: 12, year: 2023, timezone: 100} | into datetime Error: nu:🐚:only_supports_this_input_type × Input type not supported. ╭─[entry #10:1:35] 1 │ {month: 12, year: 2023, timezone: 100} | into datetime · ─┬─ ──────┬────── · │ ╰── only string input data is supported · ╰── input type: int ╰──── ``` - Key has the right type but value invalid (e.g. month=13, or day=0) ```nushell > {month: 13, year: 2023} | into datetime Error: nu:🐚:incorrect_value × Incorrect value. ╭─[entry #9:1:1] 1 │ {month: 13, year: 2023} | into datetime · ───────────┬─────────── ──────┬────── · │ ╰── one of more values are incorrect and do not represent valid date · ╰── encountered here ╰──── ``` ```nushell > {hour: 1, minute: 1, second: 70} | into datetime Error: nu:🐚:incorrect_value × Incorrect value. ╭─[entry #3:1:1] 1 │ {hour: 1, minute: 1, second: 70} | into datetime · ────────────────┬─────────────── ──────┬────── · │ ╰── one of more values are incorrect and do not represent valid time · ╰── encountered here ╰──── ``` - Timezone has right type but is invalid ```nushell > {month: 12, year: 2023, timezone: "+100:00"} | into datetime Error: nu:🐚:incorrect_value × Incorrect value. ╭─[entry #11:1:35] 1 │ {month: 12, year: 2023, timezone: "+100:00"} | into datetime · ────┬──── ──────┬────── · │ ╰── encountered here · ╰── invalid timezone ╰──── ``` - Record contains an invalid key ```nushell > {month: 12, year: 2023, unknown: 1} | into datetime Error: nu:🐚:unsupported_input × Unsupported input ╭─[entry #12:1:1] 1 │ {month: 12, year: 2023, unknown: 1} | into datetime · ─────────────────┬───────────────── ──────┬────── · │ ╰── Column 'unknown' is not valid for a structured datetime. Allowed columns are: year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, timezone · ╰── value originates from here ╰──── ``` - If several issues are present, the user can get the error msg for only one, though ```nushell > {month: 20, year: '2023'} | into datetime Error: nu:🐚:only_supports_this_input_type × Input type not supported. ╭─[entry #7:1:19] 1 │ {month: 20, year: '2023'} | into datetime · ───┬── ──────┬────── · │ ╰── only int input data is supported · ╰── input type: string ╰ ``` # Tests + Formatting Tests added Fmt + clippy OK # After Submitting Maybe indicate that in the release notes I added an example in the command, so the documentation will be automatically updated.
103 lines
2.9 KiB
Rust
103 lines
2.9 KiB
Rust
use nu_test_support::nu;
|
|
|
|
// Tests happy paths
|
|
|
|
#[test]
|
|
fn into_datetime_from_record() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5, millisecond: 6, microsecond: 7, nanosecond: 8, timezone: '+01:00'} | into datetime | into record"#
|
|
);
|
|
let expected = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5, millisecond: 6, microsecond: 7, nanosecond: 8, timezone: '+01:00'}"#
|
|
);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_defaults() {
|
|
let actual = nu!(r#"{year: 2025, timezone: '+02:00'} | into datetime | into record"#);
|
|
let expected = nu!(
|
|
r#"{year: 2025, month: 1, day: 1, hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0, timezone: '+02:00'}"#
|
|
);
|
|
|
|
assert_eq!(expected.out, actual.out);
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_round_trip() {
|
|
let actual = nu!(
|
|
r#"(1743348798 | into datetime | into record | into datetime | into int) == 1743348798"#
|
|
);
|
|
|
|
assert!(actual.out.contains("true"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_table_column() {
|
|
let actual = nu!(r#"[[date]; ["2022-01-01"] ["2023-01-01"]] | into datetime date"#);
|
|
|
|
assert!(actual.out.contains(" ago"));
|
|
}
|
|
|
|
// Tests error paths
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_wrong_type() {
|
|
let actual = nu!(r#"{year: '2023'} | into datetime"#);
|
|
|
|
assert!(actual
|
|
.err
|
|
.contains("nu::shell::only_supports_this_input_type"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_invalid_date_time_values() {
|
|
let actual = nu!(r#"{year: 2023, month: 13} | into datetime"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_invalid_timezone() {
|
|
let actual = nu!(r#"{year: 2023, timezone: '+100:00'} | into datetime"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::incorrect_value"));
|
|
}
|
|
|
|
// Tests invalid usage
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_fails_with_unknown_key() {
|
|
let actual = nu!(r#"{year: 2023, unknown: 1} | into datetime"#);
|
|
|
|
assert!(actual.err.contains("nu::shell::unsupported_input"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_incompatible_with_format_flag() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --format ''"#
|
|
);
|
|
|
|
assert!(actual.err.contains("nu::shell::incompatible_parameters"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_incompatible_with_timezone_flag() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --timezone UTC"#
|
|
);
|
|
|
|
assert!(actual.err.contains("nu::shell::incompatible_parameters"));
|
|
}
|
|
|
|
#[test]
|
|
fn into_datetime_from_record_incompatible_with_offset_flag() {
|
|
let actual = nu!(
|
|
r#"{year: 2023, month: 1, day: 2, hour: 3, minute: 4, second: 5} | into datetime --offset 1"#
|
|
);
|
|
|
|
assert!(actual.err.contains("nu::shell::incompatible_parameters"));
|
|
}
|