mirror of
https://github.com/nushell/nushell.git
synced 2025-04-21 11:48:28 +02:00
Closes #13972 # Description First commit: a hotfix concerning my last PR #15544! I had a ``unwrap_or_default`` that resulted in all years before ~1800 being considered as "now", because the ``num_nanoseconds()`` overflowed. Cc @fdncred Second: about #13972 Negative years are not allowed with RFC 2822 formatting, so I fallback RTC 3339 in such cases. If you want you might Rebase and Merge, and not squash. # User-Facing Changes On master 🔴 : ```nu ~> {year: 1900} | into datetime Mon, 1 Jan 1900 00:00:00 +0200 (125 years ago) # OK ~> {year: 1000} | into datetime Wed, 1 Jan 1000 00:00:00 +0200 (now) # NOT OK: now? ~> {year: -1000} | into datetime -1000-01-01T00:00:00+02:00 (now) # NOT OK: now? ~> {year: -1000} | into datetime | format date Error: × Main thread panicked. ├─▶ at C:\Users\RIL1RT\.cargo\registry\src\index.crates.io-6f17d22bba15001f\chrono-0.4.39\src\datetime\mod.rs:626:14 ╰─▶ writing rfc2822 datetime to string should never fail: Error help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace. # NOT OK: panics ``` On this branch 🟢 : ```nu ~> {year: 1900} | into datetime Mon, 1 Jan 1900 00:00:00 +0200 (in 125 years) ~> {year: 1000} | into datetime Wed, 1 Jan 1000 00:00:00 +0200 (1025 years ago) ~> {year: -1000} | into datetime -1000-01-01T00:00:00+02:00 (3025 years ago) ~> {year: -1000} | into datetime | format date -1000-01-01T00:00:00+02:00 ~> '3000 years ago' | date from-human | format date -0975-04-11T18:18:24.301641100+02:00 ``` # Tests + Formatting # After Submitting Nothing required IMO
113 lines
3.3 KiB
Rust
113 lines
3.3 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_very_old() {
|
|
let actual = nu!(r#"{year: -100, timezone: '+02:00'} | into datetime | into record"#);
|
|
let expected = nu!(
|
|
r#"{year: -100, 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_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"));
|
|
}
|