From a23e96c94568078c645b7635192f9715f3d9853e Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Tue, 1 Apr 2025 07:18:11 -0500 Subject: [PATCH] update human-date-parser to 3.0 (#15426) # Description There's been much debate about whether to keep human-date-parser in `into datetime`. We saw recently that a new version of the crate was released that addressed some of our concerns. This PR is to make it easier to test those fixes. # User-Facing Changes # Tests + Formatting # After Submitting --- Cargo.lock | 27 +++++++++++++++++-- Cargo.toml | 2 +- .../src/conversions/into/datetime.rs | 26 ++++++++++++++++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e0f5c0f90..abd0032bb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2386,12 +2386,13 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] name = "human-date-parser" -version = "0.2.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1116cf4debfe770c12168458321c4a8591b71c4c19f7100de07c84cf81701c63" +checksum = "406f83c56de4b2c9183be52ae9a4fefa22c0e0c3d3d7ef80be26eaee11c7110e" dependencies = [ "chrono", "pest", + "pest_consume", "pest_derive", "thiserror 1.0.69", ] @@ -4677,6 +4678,28 @@ dependencies = [ "ucd-trie", ] +[[package]] +name = "pest_consume" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79447402d15d18e7142e14c72f2e63fa3d155be1bc5b70b3ccbb610ac55f536b" +dependencies = [ + "pest", + "pest_consume_macros", + "pest_derive", +] + +[[package]] +name = "pest_consume_macros" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d8630a7a899cb344ec1c16ba0a6b24240029af34bdc0a21f84e411d7f793f29" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "pest_derive" version = "2.7.15" diff --git a/Cargo.toml b/Cargo.toml index 0200139699..c2a4bf7e37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -91,7 +91,7 @@ fancy-regex = "0.14" filesize = "0.2" filetime = "0.2" heck = "0.5.0" -human-date-parser = "0.2.0" +human-date-parser = "0.3.0" indexmap = "2.8" indicatif = "0.17" interprocess = "2.2.0" diff --git a/crates/nu-command/src/conversions/into/datetime.rs b/crates/nu-command/src/conversions/into/datetime.rs index eaac1395e1..1ff2d55522 100644 --- a/crates/nu-command/src/conversions/into/datetime.rs +++ b/crates/nu-command/src/conversions/into/datetime.rs @@ -294,7 +294,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { match parse_date_from_string(&input_val, span) { Ok(date) => return Value::date(date, span), Err(_) => { - if let Ok(date) = from_human_time(&input_val) { + if let Ok(date) = from_human_time(&input_val, Local::now().naive_local()) { match date { ParseResult::Date(date) => { let time = Local::now().time(); @@ -307,7 +307,29 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { return Value::date(dt_fixed, span); } ParseResult::DateTime(date) => { - return Value::date(date.fixed_offset(), span) + let local_offset = *Local::now().offset(); + let dt_fixed = match local_offset.from_local_datetime(&date) { + chrono::LocalResult::Single(dt) => dt, + chrono::LocalResult::Ambiguous(_, _) => { + return Value::error( + ShellError::DatetimeParseError { + msg: "Ambiguous datetime".to_string(), + span, + }, + span, + ); + } + chrono::LocalResult::None => { + return Value::error( + ShellError::DatetimeParseError { + msg: "Invalid datetime".to_string(), + span, + }, + span, + ); + } + }; + return Value::date(dt_fixed, span); } ParseResult::Time(time) => { let date = Local::now().date_naive();