chrono icon indicating copy to clipboard operation
chrono copied to clipboard

[Feature Request] Support parsing ISO 8601 Duration strings

Open praveentiru opened this issue 4 years ago • 17 comments

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.

praveentiru avatar Aug 03 '21 00:08 praveentiru

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.

praveentiru avatar Aug 03 '21 04:08 praveentiru

I think implementing FromStr trait for Duration would make sense.

sugar700 avatar Oct 19 '21 13:10 sugar700

Is anyone working on this? I kind of need it.

meh avatar Nov 02 '21 14:11 meh

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?

djc avatar Oct 19 '22 08:10 djc

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.

esheppa avatar Oct 19 '22 11:10 esheppa

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.

djc avatar Oct 19 '22 11:10 djc

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.

sugar700 avatar Oct 19 '22 15:10 sugar700

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.

djc avatar Oct 19 '22 15:10 djc

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".

sugar700 avatar Oct 19 '22 18:10 sugar700

However, it seems hopeful that the informational RFC 3339 grammar for duration doesn't allow decimals at all.

djc avatar Oct 20 '22 07:10 djc

We could call the support "RFC 3339 inspired" :D

esheppa avatar Oct 20 '22 10:10 esheppa

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 avatar Nov 11 '22 00:11 Ownezx

@Ownezx would you be interested in working on a PR along the lines of the discussion above?

djc avatar Nov 11 '22 08:11 djc

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.

Ownezx avatar Nov 11 '22 22:11 Ownezx

Just to make things clear, you'd want me to play with the TimeDelta struct or with the time::duration struct?

Ownezx avatar Nov 11 '22 22:11 Ownezx

It would be a new type, somewhat like the CalendarPeriod type I defined in an earlier comment.

djc avatar Nov 14 '22 09:11 djc

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.

michaelmior avatar Nov 20 '23 17:11 michaelmior