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

chrono Date<Utc>.format("%s") panics with unclear message #575

Closed
doivosevic opened this issue Jul 18, 2021 · 7 comments
Closed

chrono Date<Utc>.format("%s") panics with unclear message #575

doivosevic opened this issue Jul 18, 2021 · 7 comments

Comments

@doivosevic
Copy link

panics with panicked at 'a Display implementation returned an error unexpectedly: Error', /build/rustc-1.51.0-src/library/alloc/src/string.rs:2213:14

I feel like there should be a better panic for this case and I guess some other format values have similar panics. This is especially problematic since format doesn't return Result so I wouldn't expect this to panic nor can I handle it gracefully

#[test]
fn test_format_timestamp_chrono() {
    let now = Utc::now().date(); // just Date panics on %s
    now.format("%s").to_string();
}
@t1ooo
Copy link

t1ooo commented Jul 20, 2021

Can we just throw panic with clear error message instead of returning fmt::Error?

return Err(fmt::Error); // insufficient arguments for given format

@retrhelo
Copy link

I think a good idea is to have now.format("%s") returns Result instead, but this will change the API... Another solution I think is making now.format("%s").to_string() returns an empty String, which I think is also reasonable. I am working on the later solution to make a PR.

@hustcer
Copy link

hustcer commented May 6, 2022

I have run into this as well, can it be fixed in the next release? thx

@retrhelo
Copy link

retrhelo commented May 6, 2022

I have run into this as well, can it be fixed in the next release? thx

I wonder if my PR solves your problem? It's made a few months ago, so some conflicts have to be solved before it gets merged :(.

@hustcer
Copy link

hustcer commented May 6, 2022

Yes, it does, Thanks for your contribution

@esheppa
Copy link
Collaborator

esheppa commented Jul 24, 2022

Hi @hustcer - please see #614 (comment) for some alternative solutions available in the currently released version of chrono.

@pitdicker
Copy link
Collaborator

We have multiple issues that track the issues out formatting code can panic. I'm closing this one since the Date type is deprecated. #1127 is the best issue to watch.

@pitdicker pitdicker closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants