chronotope / chrono

Date and time library for Rust
Other
3.3k stars 523 forks source link

formatting `NaiveDateTime` as `RFC3339` gives a bad-explaining error #1096

Open hasezoey opened 1 year ago

hasezoey commented 1 year ago

Currently when trying to format a NaiveDateTime with RFC3339 (or to be more specific .format("%+")), there a very generic error:

thread 'main' panicked at 'a Display implementation returned an error unexpectedly: Error', /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/alloc/src/string.rs:2533:14
stack backtrace:
   0: rust_begin_unwind
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/std/src/panicking.rs:579:5
   1: core::panicking::panic_fmt
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/panicking.rs:64:14
   2: core::result::unwrap_failed
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/result.rs:1750:5
   3: core::result::Result<T,E>::expect
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/result.rs:1047:23
   4: <T as alloc::string::ToString>::to_string
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/alloc/src/string.rs:2532:9
   5: bin::main
             at ./crates/bin/src/main.rs:10:10

no mention of that it cannot format because no timezone is provided

from my testing the error comes from: https://github.com/chronotope/chrono/blob/083d3bf196537dfdbce6c29543734022f68e61a4/src/format/mod.rs#L687 because off is None and results in the following error: https://github.com/chronotope/chrono/blob/083d3bf196537dfdbce6c29543734022f68e61a4/src/format/mod.rs#L697

versions: rustc rustc 1.69.0 (84c898d65 2023-04-16) chrono 0.4.24

reproduction code:

let test = NaiveDateTime::from_timestamp_millis(0).unwrap();
println!("test {}", test.format("%+"));

possible fixes:

jtmoon79 commented 1 year ago

I was able to reproduce this issue on branch 0.4.x and branch main.

I added this user-provided regression test in PR #1097

pitdicker commented 1 year ago

https://github.com/chronotope/chrono/pull/902 Should give a better error message here. It will have to wait until 0.5 though, as that is a breaking change.