chrono
chrono copied to clipboard
[Feature Request] Support parsing ISO 8601 Duration strings
The crate supports parsing of date strings but, there is no equivalent support for parsing duration strings. This is request to support parsing of duration strings. Description here -> https://en.wikipedia.org/wiki/ISO_8601#Durations. RFC Spec - https://datatracker.ietf.org/doc/html/rfc3339#appendix-A
I am open to work on this issue but, would need direction/guidance to do the same.
I was poking around the code base. I can do parsing using Parsed using similar approach as implementation of FromStr trait for DateTime. But, I am not sure what would be the right way to expose this functionality.
Look forward to feedback.
I think implementing FromStr trait for Duration would make sense.
Is anyone working on this? I kind of need it.
I have a use case for this. I'm thinking we might add a type like this:
struct CalendarPeriod {
years: u32,
months: Months,
days: Days,
time: core::time::Duration,
}
With a serde implementation. @esheppa what do you think?
I like the struct you suggest, but this part from wiki concerns me:
The smallest value used may also have a decimal fraction,[37] as in "P0.5Y" to indicate half a year.
A pragmatic option may be that we only support integer values everywhere but in the seconds part, however it would be interesting to understand whether that is overly restrictive.
Oh, good catch. I can't think of any strategy for figuring out how restrictive that would be. Starting with integer values only seems like a decent approach to get at least some support in.
ISO 8601 has a lot of features that aren't particularly useful. Personally I rather would look into supporting RFC 3339 for durations, which is a practical subset of it. In particular, its duration parts are integers only.
Huh, Wikipedia has this:
IETF RFC 3339[45] defines a profile of ISO 8601 for use in Internet protocols and standards. It explicitly excludes durations and dates before the common era.
So it looks like RFC 3339 does in fact include durations? I'm happy to bill our support as only for the RFC 3339 subset of durations. It does look like I missed a weeks field in my proposed definition above.
Nevermind, it doesn't provide durations, I just got confused by CTRL+F in RFC 3339 providing a result in "ISO 8601 Collected ABNF" section with a grammar that doesn't support decimal durations, however it is "informational only".
However, it seems hopeful that the informational RFC 3339 grammar for duration doesn't allow decimals at all.
We could call the support "RFC 3339 inspired" :D
Yeah some support for something like that would be great. I'm currently trying to use chrono for ics file parsing and being able to parse the duration in these files (the duration have a format like so P15DT5H0M20S) would be great.
Having something like the DateTime::parse_from_str for duration would be golden.
@Ownezx would you be interested in working on a PR along the lines of the discussion above?
I'd have to look at the source for DateTime::parse_from_str. But I'm rather new to rust and to contributing to open source project.
Can't give any garantee but I can give it a try for sure.
Just to make things clear, you'd want me to play with the TimeDelta struct or with the time::duration struct?
It would be a new type, somewhat like the CalendarPeriod type I defined in an earlier comment.
Note that JSON Schema explicitly uses ISO 8601 format durations. Since arrow-json already uses chrono, adding support for parsing ISO 8601 durations would be an easy way to add support for durations to Arrow's JSON reader.