chronotope / chrono

Date and time library for Rust
Other
3.25k stars 515 forks source link

[Feature Request] Support parsing ISO 8601 Duration strings #579

Open praveentiru opened 3 years ago

praveentiru commented 3 years ago

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 commented 3 years ago

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.

KamilaBorowska commented 2 years ago

I think implementing FromStr trait for Duration would make sense.

meh commented 2 years ago

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

djc commented 1 year ago

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?

esheppa commented 1 year ago

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.

djc commented 1 year ago

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.

KamilaBorowska commented 1 year ago

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.

djc commented 1 year ago

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.

KamilaBorowska commented 1 year ago

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

djc commented 1 year ago

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

esheppa commented 1 year ago

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

Ownezx commented 1 year ago

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.

djc commented 1 year ago

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

Ownezx commented 1 year ago

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 commented 1 year ago

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

djc commented 1 year ago

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

michaelmior commented 9 months ago

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.