Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hasezoey opened this issue May 28, 2023 · 2 comments
Open

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

hasezoey opened this issue May 28, 2023 · 2 comments

Comments

@hasezoey
Copy link

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:

if let (Some(d), Some(t), Some(&(_, off))) = (date, time, off) {

because off is None and results in the following error:
None => return Err(fmt::Error), // insufficient arguments for given format

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:

  • add a implicit zero timezone
  • change the current error into something that has a message
  • return a error before it even gets to this point explaining that RFC3339 is not supported for NaiveDateTime
jtmoon79 added a commit to jtmoon79/chrono that referenced this issue May 28, 2023
jtmoon79 added a commit to jtmoon79/chrono that referenced this issue May 28, 2023
jtmoon79 added a commit to jtmoon79/chrono that referenced this issue May 28, 2023
jtmoon79 added a commit to jtmoon79/chrono that referenced this issue May 28, 2023
@jtmoon79
Copy link
Contributor

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
Copy link
Collaborator

#902 Should give a better error message here. It will have to wait until 0.5 though, as that is a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants