chronoutil
chronoutil copied to clipboard
Serialize RelativeDuration with serde
I'd like to be able to serialize a RelativeDuration with serde. Because sometimes I want to save a duration to disk. I can easily enough work around it with an enum holding days or months for my case, but it would be easier if RelativeDuration was serializable already.
Perhaps if extra deps are not desirable it could be behind a feature?
If there is interest in this I would consider doing a PR.
Well I think that even with the [serde]
feature, Chrono don't provide a serialize impl for their Duration
type, so I'm hesitant to add it here. Stack overflow leads me to a 3rd party lib for that https://docs.rs/serde_with/latest/serde_with/struct.DurationSeconds.html
Having said that, I don't agree with the serialization format they are using there. Really it should be the ISO8601 standard for durations https://en.wikipedia.org/wiki/ISO_8601#Durations
If you were to implement a subset of ISO 8601 notation under a [serde]
feature flag then I would accept that. That means, for example, that RelativeDuration::months(1) + Duration::milliseconds(10)
would serialize to P1M0.01S
Ah yes, it wasn't until after I wrote the wishlist that I discovered as you say that Chrono don't provide a serialize impl for their Duration type, even with the [serde] feature. In the end for my applicaton I have just saved 'days' and 'months' values and convert them from/to RelativeDurations when serializing/unseriaalizing, it was easier than thinking about the proper way to do it in chronoutil.
If I reassess (or if there were more interest from others) later, I may look at a PR again and your notes are helpful. Thanks