time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.06k stars 261 forks source link

PrimitiveDateTime does not parse with insufficient information #649

Closed GunnarMorrigan closed 5 months ago

GunnarMorrigan commented 5 months ago

Hi,

The following string is not parsable "Sat 08Jul23 04:00" to a primitive date time object while all information needed is present.

example here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f26bd7626d3f9e413ac94f44fedeab09

All information required is present, day month year hour and minute. Thus this should work fine?

Side note: parsing also does not work if the weekday and month are in all caps

GunnarMorrigan commented 5 months ago

Caused by these TryFrom impl for Date not considering year_last_two option.

In these lines: https://github.com/time-rs/time/blob/bb397df38e5316d767ca3f3bd148e506b7af5587/time/src/parsing/parsed.rs#L758C1-L780C47

jhpratt commented 5 months ago

The error is correct. There is not sufficient information to parse a date, as only the last two digits of the year are known. As a result, it is ambiguous.

Side note: parsing also does not work if the weekday and month are in all caps

You need to explicitly permit case-insensitive matching. This is done with a case_sensitive:false in both weekday and month as indicated in the format description documentation.

snspinn commented 4 months ago

The source of confusion for me around the [year] component was due to the docs:

Note that when parsing, if only the last two digits of the year are present, the value returned may not be what was expected — if the return is successful at all (it's not guaranteed).

On first reading I interpreted this to mean that I could get back 1923 when I want 2023. On second reading this sentence is ambiguous about whether parsing with repr:last_two is supported or not. Maybe this could be worded more clearly.

jhpratt commented 4 months ago

That description is a long-winded way of saying there are no guarantees. Either Ok or Err can be returned, and in the former it could use any century.