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

Serialize RelativeDuration with serde #2

Open
alexkingnz opened this issue Jun 5, 2022 · 2 comments
Open

Serialize RelativeDuration with serde #2

alexkingnz opened this issue Jun 5, 2022 · 2 comments

Comments

@alexkingnz
Copy link

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.

@olliemath
Copy link
Owner

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

@alexkingnz
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants