chrono icon indicating copy to clipboard operation
chrono copied to clipboard

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

Open doivosevic opened this issue 3 years ago • 6 comments

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();
}

doivosevic avatar Jul 18 '21 07:07 doivosevic

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

https://github.com/chronotope/chrono/blob/3467172c31188006147585f6ed3727629d642fed/src/format/mod.rs#L537

t1ooo avatar Jul 20 '21 08:07 t1ooo

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.

retrhelo avatar Oct 29 '21 15:10 retrhelo

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

hustcer avatar May 06 '22 12:05 hustcer

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 :(.

retrhelo avatar May 06 '22 13:05 retrhelo

Yes, it does, Thanks for your contribution

hustcer avatar May 06 '22 13:05 hustcer

Hi @hustcer - please see https://github.com/chronotope/chrono/pull/614#issuecomment-1173070176 for some alternative solutions available in the currently released version of chrono.

esheppa avatar Jul 24 '22 11:07 esheppa

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 avatar Feb 27 '24 19:02 pitdicker