gweis / isodate

ISO 8601 date/time parser
BSD 3-Clause "New" or "Revised" License
148 stars 58 forks source link

Parse time interval #48

Open danvk opened 6 years ago

danvk commented 6 years ago

The Wikipedia article on ISO 8601 mentions time intervals, e.g. "2008-02-15/03-14". Is there a facility in isodate for parsing these?

theferrit32 commented 6 years ago

I'm not sure there is a single function for parsing those complex date interval notations. There are a lot of cases to handle there, and inferring when the second value is a partial datetime, but it could be a nice possible feature enhancement.

For now you can sort of work around it if you can make both values be a full date, time, or datetime string:

s = "2008-02-15/2008-03-14"
interval = isodate.parse_date(s.split("/")[1]) - isodate.parse_date(s.split("/")[0])

The interval variable will be a datetime.timedelta object. If the values in the string are datetimes and not just dates, then use parse_datetime instead of parse_date.

gweis commented 6 years ago

No, time interval is not supported yet.

Could be interesting though, just not sure how to map that onto python native date/time types.

@theferrit32 suggestion is probably a reasonable workaround for now, given that iso time intervals and recurring time intervals can be quite complex to interpret correctly.

danvk commented 6 years ago

The workaround is fine for now, but I agree that a parse_interval method would be a nice enhancement to isodate. Handling those complexities is one of the reasons to use a library like this, after all!