Skip to content

Commit d5e00c0

Browse files
hqszfdncred
andauthored
Support default offset with dateformat option (#13289)
# Description Fixes #13280. After apply this patch, we can use non-timezone string + format option `into datetime` cmd # User-Facing Changes AS-IS (before fixing) ``` $ "09.02.2024 11:06:11" | into datetime --format '%m.%d.%Y %T' Error: nu::shell::cant_convert × Can't convert to could not parse as datetime using format '%m.%d.%Y %T'. ╭─[entry #1:1:25] 1 │ "09.02.2024 11:06:11" | into datetime --format '%m.%d.%Y %T' · ──────┬────── · ╰── can't convert input is not enough for unique date and time to could not parse as datetime using format '%m.%d.%Y %T' ╰──── help: you can use `into datetime` without a format string to enable flexible parsing $ "09.02.2024 11:06:11" | into datetime Mon, 2 Sep 2024 11:06:11 +0900 (in 2 months) ``` TO-BE(After fixing) ``` $ "09.02.2024 11:06:11" | into datetime --format '%m.%d.%Y %T' Mon, 2 Sep 2024 20:06:11 +0900 (in 2 months) $ "09.02.2024 11:06:11" | into datetime Mon, 2 Sep 2024 11:06:11 +0900 (in 2 months) ``` # Tests + Formatting If there is agreement on the direction, I will add a test. # After Submitting --------- Co-authored-by: Darren Schroeder <[email protected]>
1 parent 3fae772 commit d5e00c0

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

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

Lines changed: 53 additions & 9 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, NaiveTime, TimeZone, Utc};
2+
use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, NaiveTime, 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::*;
@@ -162,24 +162,37 @@ impl Command for SubCommand {
162162
};
163163
vec![
164164
Example {
165-
description: "Convert any standard timestamp string to datetime",
165+
description: "Convert timestamp string to datetime with timezone offset",
166166
example: "'27.02.2021 1:55 pm +0000' | into datetime",
167167
#[allow(clippy::inconsistent_digit_grouping)]
168168
result: example_result_1(1614434100_000000000),
169169
},
170170
Example {
171-
description: "Convert any standard timestamp string to datetime",
171+
description: "Convert standard timestamp string to datetime with timezone offset",
172172
example: "'2021-02-27T13:55:40.2246+00:00' | into datetime",
173173
#[allow(clippy::inconsistent_digit_grouping)]
174174
result: example_result_1(1614434140_224600000),
175175
},
176176
Example {
177177
description:
178-
"Convert non-standard timestamp string to datetime using a custom format",
178+
"Convert non-standard timestamp string, with timezone offset, to datetime using a custom format",
179179
example: "'20210227_135540+0000' | into datetime --format '%Y%m%d_%H%M%S%z'",
180180
#[allow(clippy::inconsistent_digit_grouping)]
181181
result: example_result_1(1614434140_000000000),
182182
},
183+
Example {
184+
description: "Convert non-standard timestamp string, without timezone offset, to datetime with custom formatting",
185+
example: "'16.11.1984 8:00 am' | into datetime --format '%d.%m.%Y %H:%M %P'",
186+
#[allow(clippy::inconsistent_digit_grouping)]
187+
result: Some(Value::date(
188+
DateTime::from_naive_utc_and_offset(
189+
NaiveDateTime::parse_from_str("16.11.1984 8:00 am", "%d.%m.%Y %H:%M %P")
190+
.expect("date calculation should not fail in test"),
191+
*Local::now().offset(),
192+
),
193+
Span::test_data(),
194+
)),
195+
},
183196
Example {
184197
description:
185198
"Convert nanosecond-precision unix timestamp to a datetime with offset from UTC",
@@ -372,10 +385,21 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
372385
Some(dt) => match DateTime::parse_from_str(val, &dt.0) {
373386
Ok(d) => Value::date ( d, head ),
374387
Err(reason) => {
375-
Value::error (
376-
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()) },
377-
head,
378-
)
388+
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+
),
396+
Err(_) => {
397+
Value::error (
398+
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()) },
399+
head,
400+
)
401+
}
402+
}
379403
}
380404
},
381405

@@ -457,7 +481,7 @@ mod tests {
457481
}
458482

459483
#[test]
460-
fn takes_a_date_format() {
484+
fn takes_a_date_format_with_timezone() {
461485
let date_str = Value::test_string("16.11.1984 8:00 am +0000");
462486
let fmt_options = Some(DatetimeFormat("%d.%m.%Y %H:%M %P %z".to_string()));
463487
let args = Arguments {
@@ -473,6 +497,26 @@ mod tests {
473497
assert_eq!(actual, expected)
474498
}
475499

500+
#[test]
501+
fn takes_a_date_format_without_timezone() {
502+
let date_str = Value::test_string("16.11.1984 8:00 am");
503+
let fmt_options = Some(DatetimeFormat("%d.%m.%Y %H:%M %P".to_string()));
504+
let args = Arguments {
505+
zone_options: None,
506+
format_options: fmt_options,
507+
cell_paths: None,
508+
};
509+
let actual = action(&date_str, &args, Span::test_data());
510+
let expected = Value::date(
511+
DateTime::from_naive_utc_and_offset(
512+
NaiveDateTime::parse_from_str("16.11.1984 8:00 am", "%d.%m.%Y %H:%M %P").unwrap(),
513+
*Local::now().offset(),
514+
),
515+
Span::test_data(),
516+
);
517+
assert_eq!(actual, expected)
518+
}
519+
476520
#[test]
477521
fn takes_iso8601_date_format() {
478522
let date_str = Value::test_string("2020-08-04T16:39:18+00:00");

0 commit comments

Comments
 (0)