Skip to content

Commit 4e9b60f

Browse files
fdncredNotTheDr01ds
authored andcommitted
update human-date-parser conversion to use local timezone (nushell#14266)
# Description This PR tries to fix nushell#14195 by setting the local time and timezone after conversion without changing the time. ### Before ```nushell ❯ 'in 10 minutes' | into datetime Tue, 5 Nov 2024 12:59:58 -0600 (in 9 minutes) ❯ 'yesterday' | into datetime Sun, 3 Nov 2024 18:00:00 -0600 (2 days ago) ❯ 'tomorrow' | into datetime Tue, 5 Nov 2024 18:00:00 -0600 (in 5 hours) ❯ 'today' | into datetime Mon, 4 Nov 2024 18:00:00 -0600 (18 hours ago) ``` ### After (these are correct) ```nushell ❯ 'in 10 minutes' | into datetime Tue, 5 Nov 2024 12:58:44 -0600 (in 9 minutes) ❯ 'yesterday' | into datetime Mon, 4 Nov 2024 12:49:04 -0600 (a day ago) ❯ 'tomorrow' | into datetime Wed, 6 Nov 2024 12:49:20 -0600 (in a day) ❯ 'today' | into datetime Tue, 5 Nov 2024 12:52:06 -0600 (now) ``` # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
1 parent 1b50909 commit 4e9b60f

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

crates/nu-command/src/conversions/into/datetime.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{generate_strftime_list, parse_date_from_string};
2-
use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, NaiveTime, TimeZone, Utc};
2+
use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, TimeZone, Utc};
33
use human_date_parser::{from_human_time, ParseResult};
44
use nu_cmd_base::input_handler::{operate, CmdArgument};
55
use nu_engine::command_prelude::*;
@@ -275,12 +275,13 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
275275
if let Ok(date) = from_human_time(&input_val) {
276276
match date {
277277
ParseResult::Date(date) => {
278-
let time = NaiveTime::from_hms_opt(0, 0, 0).expect("valid time");
278+
let time = Local::now().time();
279279
let combined = date.and_time(time);
280-
let dt_fixed = DateTime::from_naive_utc_and_offset(
281-
combined,
282-
*Local::now().offset(),
283-
);
280+
let local_offset = *Local::now().offset();
281+
let dt_fixed =
282+
TimeZone::from_local_datetime(&local_offset, &combined)
283+
.single()
284+
.unwrap_or_default();
284285
return Value::date(dt_fixed, span);
285286
}
286287
ParseResult::DateTime(date) => {
@@ -289,10 +290,11 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
289290
ParseResult::Time(time) => {
290291
let date = Local::now().date_naive();
291292
let combined = date.and_time(time);
292-
let dt_fixed = DateTime::from_naive_utc_and_offset(
293-
combined,
294-
*Local::now().offset(),
295-
);
293+
let local_offset = *Local::now().offset();
294+
let dt_fixed =
295+
TimeZone::from_local_datetime(&local_offset, &combined)
296+
.single()
297+
.unwrap_or_default();
296298
return Value::date(dt_fixed, span);
297299
}
298300
}
@@ -386,13 +388,15 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
386388
Ok(d) => Value::date ( d, head ),
387389
Err(reason) => {
388390
match NaiveDateTime::parse_from_str(val, &dt.0) {
389-
Ok(d) => Value::date (
390-
DateTime::from_naive_utc_and_offset(
391-
d,
392-
*Local::now().offset(),
393-
),
394-
head,
395-
),
391+
Ok(d) => {
392+
let local_offset = *Local::now().offset();
393+
let dt_fixed =
394+
TimeZone::from_local_datetime(&local_offset, &d)
395+
.single()
396+
.unwrap_or_default();
397+
398+
Value::date (dt_fixed,head)
399+
}
396400
Err(_) => {
397401
Value::error (
398402
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()) },

0 commit comments

Comments
 (0)