zeta12ti / parse_duration

Parses a duration from a string.
MIT License
17 stars 2 forks source link

ISO8601 Duration Parsing #17

Open mlevkov opened 4 years ago

mlevkov commented 4 years ago

Hello,

I would like to know if it is possible to parse iso8601 duration time format? I've tried to parse "P0Y0M0DT0H5M52.352S" or "P0Y0M0DT0H0M14.976S" without success.

Regards, Maxim

zeta12ti commented 4 years ago

Right now, this crate focuses on looser formatting. To parse iso8601, we'd need to be able to recognize that the given string is in iso8601 and parse it differently. This is necessary for three reasons.

First, iso8601 units are always a single character. This allows things like DT to be parsed without ambiguity (there's no DT unit). Outside of the context of iso8601, we can use units like seconds or years.

Second, it would have to use context to determine what a unit means. Right now, units can be listed in any order and duplicates are combined (i.e., "123s 321s" is parsed as 444 seconds). To parse iso8601, we'd have to be able to parse the first M as months and the second as minutes.

Lastly, iso8601 appears to be quiet on how to convert days, months and years to seconds/nanoseconds (if you have a better source on this, feel free to speak up). In various implementations that I've seen, the converter either used the current date and a DateTime-like converter to figure out what the duration is in seconds, or they simply chose a fixed duration for years, months and days. This crate currently does the latter, but I'd rather not claim iso8601 compliance on this subject. It seems like iso8601 requires durations to keep information about years, months and days, rather than ever converting to seconds.

For these reasons, I think an different crate would be appropriate. iso8601_duration might meet your needs.