Skip to content

Poor error message when attempting to format NaiveDate with unsupported specifiers #1660

Open
@friendlymatthew

Description

@friendlymatthew

When attempting to format a NaiveDate with time-related/unsupported specifiers, the error message is not informative.

For example:

use chrono::NaiveDate;

fn main() {
    let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
    let _ = d.format("%Y-%m-%dT%H:%M:%S%.3f").to_string(); // uses time specifiers (i.e. %H, %M, etc...)
    let _ = d.format("%Y-%m-%k").to_string(); // This is just hogwash, %k isn't a time or date specifier!
}

Both outputs:

  Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/fmtdate`
thread 'main' panicked at /Users/matthew/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/string.rs:2688:14:

a Display implementation returned an error unexpectedly: Error

The error message could be improved indicating time specifiers or unsupported specifiers cannot be used with NaiveDate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions