chrono
chrono copied to clipboard
chrono Date<Utc>.format("%s") panics with unclear message
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();
}
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
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.
I have run into this as well, can it be fixed in the next release? thx
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 :(.
Yes, it does, Thanks for your contribution
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
.
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.