diff --git a/crates/nu-command/src/conversions/into/datetime.rs b/crates/nu-command/src/conversions/into/datetime.rs index 68fcc31420..fd183b43bd 100644 --- a/crates/nu-command/src/conversions/into/datetime.rs +++ b/crates/nu-command/src/conversions/into/datetime.rs @@ -1,5 +1,5 @@ use crate::{generate_strftime_list, parse_date_from_string}; -use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, NaiveTime, TimeZone, Utc}; +use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, TimeZone, Utc}; use human_date_parser::{from_human_time, ParseResult}; use nu_cmd_base::input_handler::{operate, CmdArgument}; use nu_engine::command_prelude::*; @@ -275,12 +275,13 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { if let Ok(date) = from_human_time(&input_val) { match date { ParseResult::Date(date) => { - let time = NaiveTime::from_hms_opt(0, 0, 0).expect("valid time"); + let time = Local::now().time(); let combined = date.and_time(time); - let dt_fixed = DateTime::from_naive_utc_and_offset( - combined, - *Local::now().offset(), - ); + let local_offset = *Local::now().offset(); + let dt_fixed = + TimeZone::from_local_datetime(&local_offset, &combined) + .single() + .unwrap_or_default(); return Value::date(dt_fixed, span); } ParseResult::DateTime(date) => { @@ -289,10 +290,11 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { ParseResult::Time(time) => { let date = Local::now().date_naive(); let combined = date.and_time(time); - let dt_fixed = DateTime::from_naive_utc_and_offset( - combined, - *Local::now().offset(), - ); + let local_offset = *Local::now().offset(); + let dt_fixed = + TimeZone::from_local_datetime(&local_offset, &combined) + .single() + .unwrap_or_default(); return Value::date(dt_fixed, span); } } @@ -386,13 +388,15 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value { Ok(d) => Value::date ( d, head ), Err(reason) => { match NaiveDateTime::parse_from_str(val, &dt.0) { - Ok(d) => Value::date ( - DateTime::from_naive_utc_and_offset( - d, - *Local::now().offset(), - ), - head, - ), + Ok(d) => { + let local_offset = *Local::now().offset(); + let dt_fixed = + TimeZone::from_local_datetime(&local_offset, &d) + .single() + .unwrap_or_default(); + + Value::date (dt_fixed,head) + } Err(_) => { Value::error ( ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) },