You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
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
+
matchNaiveDateTime::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
+
}
379
403
}
380
404
},
381
405
@@ -457,7 +481,7 @@ mod tests {
457
481
}
458
482
459
483
#[test]
460
-
fntakes_a_date_format(){
484
+
fntakes_a_date_format_with_timezone(){
461
485
let date_str = Value::test_string("16.11.1984 8:00 am +0000");
462
486
let fmt_options = Some(DatetimeFormat("%d.%m.%Y %H:%M %P %z".to_string()));
463
487
let args = Arguments{
@@ -473,6 +497,26 @@ mod tests {
473
497
assert_eq!(actual, expected)
474
498
}
475
499
500
+
#[test]
501
+
fntakes_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());
0 commit comments