nushell/crates/nu-command/src
Loïc Riegel 39edd7e080
Bugfix: datetime parsing and local timezones (#15544)
Hi,
This PR should close 3 issues
- [DMY date format is parsed inconsistently
#14123](https://github.com/nushell/nushell/issues/14123)
- [into datetime doesnt't work with --format and ignores user's locale
#11015](https://github.com/nushell/nushell/issues/11015)
- [into datetime: iinconsistent and incrrect behaviour regarding
timezones #13823](https://github.com/nushell/nushell/issues/13823)


# Description
- Allow to parse only dates or only times with --format
- Use local timezone depending on the input. Ex: I'm in France, so show
dates with +0100 in winter and +0200 in summer.

```nushell
# Concerning #13823

> "2020-01-01 12:00" | into datetime
Wed, 1 Jan 2020 12:00:00 +0100 (5 years ago)
# OK, it's my timezone in winter time

> "2020-06-01 12:00" | into datetime
Mon, 1 Jun 2020 12:00:00 +0200 (4 years ago)
# OK, it's my timezone in summertime

> ("2024-10-27 12:00" | into datetime) - ("2024-10-27 00:00" | into datetime)
13hr
# Ok, because we switched from summer to winter time on 2025-10-27, so there are actually 13h between midnight and noon

> "2020-01-01 12:00" | into datetime --format "%Y-%m-%d %H:%M"
Wed, 1 Jan 2020 12:00:00 +0100 (5 years ago)
# OK: timezone is assumed to be local, and +0100 is my timezone in winter

# Concerning #14123 and #11015
# Flexible parsing still works like before, which could be counter-intuitive, but it's flexible parsing
# with one difference: the timezone is local
> '12-01-2001' | into datetime
Sat, 1 Dec 2001 00:00:00 +0100 (23 years ago)
# OK, +0100 is my timezone in winter time. If I run it with nushell 0.103.0 in summer time, I get +0200
> '13-01-2001' | into datetime
Sat, 13 Jan 2001 00:00:00 +0100 (24 years ago)

## If you want, you can use the --format option to parse a date or a time (before, it had to be a date + time)
## Notice here again the timezone is correct depending on winter/summer time
~> "06.03.2023" | into datetime -f "%d.%m.%Y"
Mon, 6 Mar 2023 00:00:00 +0100 (2 years ago)
~> "06.03.2023" | into datetime -f "%m.%d.%Y"
Sat, 3 Jun 2023 00:00:00 +0200 (2 years ago)
> "10:00" | into datetime --format "%H:%M"
Thu, 10 Apr 2025 10:00:00 +0200 (9 hours ago)
```

# User-Facing Changes
See above

# Tests + Formatting


# After Submitting
I'll down something for the release notes, if this is merged in time 😄
2025-04-11 07:48:39 -05:00
..
bytes fix range bugs in str substring, str index-of, slice, bytes at (#14863) 2025-01-30 06:50:01 -06:00
charting Add Filesize type (#14369) 2024-11-29 21:24:17 +00:00
conversions Bugfix: datetime parsing and local timezones (#15544) 2025-04-11 07:48:39 -05:00
database Refactor I/O Errors (#14927) 2025-01-28 16:03:31 -06:00
date Bugfix: datetime parsing and local timezones (#15544) 2025-04-11 07:48:39 -05:00
debug Fix clippy (#15489) 2025-04-06 09:49:28 +08:00
env Parse XML documents with DTDs by default, and add --disallow-dtd flag (#15272) 2025-03-12 08:09:55 -05:00
experimental Don't collect job output (#15365) 2025-03-20 09:49:12 -04:00
filesystem Fix future clippy lints (#15519) 2025-04-08 08:51:12 +08:00
filters Fix future clippy lints (#15519) 2025-04-08 08:51:12 +08:00
formats Disallow DTD by default in from xml (#15325) 2025-03-17 14:16:17 +01:00
generators seq date: generalize to allow any duration for --increment argument (#14903) 2025-01-25 13:24:39 -06:00
hash add binary as input to hash commands (#13923) 2024-09-25 16:47:52 +08:00
help Rework operator type errors (#14429) 2025-02-12 20:03:40 -08:00
math Fix clippy (#15489) 2025-04-06 09:49:28 +08:00
misc More precise ErrorKind::NotFound errors (#15149) 2025-02-22 11:42:44 -05:00
network Fix clippy (#15489) 2025-04-06 09:49:28 +08:00
path Replace some PipelineMismatch by OnlySupportsThisInputType by shell error (#15447) 2025-04-07 12:25:27 +02:00
platform refactor: rename subcommand structs (#15309) 2025-03-14 02:00:35 +01:00
random Update rand and rand_chacha to 0.9 (#15463) 2025-04-01 07:15:39 -05:00
removed refactor: rename subcommand structs (#15309) 2025-03-14 02:00:35 +01:00
shells Jobs (#14883) 2025-02-25 12:09:52 -05:00
stor Run-time pipeline input typechecking tweaks (#14922) 2025-02-02 15:51:47 -05:00
strings Replace some PipelineMismatch by OnlySupportsThisInputType by shell error (#15447) 2025-04-07 12:25:27 +02:00
system Consider PATH when running command is nuscript in windows (#15486) 2025-04-04 06:35:36 -05:00
viewers Refactor I/O Errors (#14927) 2025-01-28 16:03:31 -06:00
default_context.rs Move human date parsing into new command date from-human (#15495) 2025-04-07 07:44:55 -05:00
example_test.rs Implementing ByteStream interuption on infinite stream (#13552) 2025-01-11 13:28:08 -08:00
lib.rs Start to Add WASM Support Again (#14418) 2024-11-30 07:57:11 -06:00
progress_bar.rs Cut down unnecessary lint allows (#14335) 2024-11-15 19:24:39 +01:00
sort_utils.rs Remove deprecated commands (#14726) 2025-01-07 07:37:51 +08:00