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

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

Open
praveentiru opened this issue Aug 3, 2021 · 17 comments
Open

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

praveentiru opened this issue Aug 3, 2021 · 17 comments

Comments

@praveentiru
Copy link

praveentiru commented Aug 3, 2021

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
Copy link
Author

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
Copy link

I think implementing FromStr trait for Duration would make sense.

@meh
Copy link

meh commented Nov 2, 2021

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

@djc
Copy link
Contributor

djc commented Oct 19, 2022

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
Copy link
Collaborator

esheppa commented Oct 19, 2022

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
Copy link
Contributor

djc commented Oct 19, 2022

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
Copy link

KamilaBorowska commented Oct 19, 2022

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
Copy link
Contributor

djc commented Oct 19, 2022

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
Copy link

KamilaBorowska commented Oct 19, 2022

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
Copy link
Contributor

djc commented Oct 20, 2022

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

@esheppa
Copy link
Collaborator

esheppa commented Oct 20, 2022

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

@Ownezx
Copy link

Ownezx commented Nov 11, 2022

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
Copy link
Contributor

djc commented Nov 11, 2022

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

@Ownezx
Copy link

Ownezx commented Nov 11, 2022

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
Copy link

Ownezx commented Nov 11, 2022

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

@djc
Copy link
Contributor

djc commented Nov 14, 2022

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

@michaelmior
Copy link

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.

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

7 participants