python parse `ISO_8601` time or time interval

Mil*_*ell 6 python datetime parsing iso

I need to convert

FROM

a list of strs

TO

a list of

  • either a datetime.datetime
  • or a datetime.datetime plus a datetime.timedelta.

The input list contains strings in ISO_8601 (wikipedia) format. The strings can either be a date or a time interval. The solution for this, that I came up with is the following:

import dateutil.parser

result = []
for str_ in input_list:
    if not is_time_interval_str(str_):
        result.append(dateutil.parser.parse(str_))
    else:
        result.append(parse_time_interval(str_))
Run Code Online (Sandbox Code Playgroud)

What I am stuck with is the two functions is_time_interval_str and parse_time_interval. I have looked for python packages that implement parsing of the time intervals but I couldn't find any yet. I have checked

  1. dateutil.parser
  2. pyiso8601
  3. isodate
  4. arrow
  5. ciso8601
  6. iso8601utils (claims to support time intervals, but does only some)
  7. maya (offers the functionality, but the implementation is flawed)
  8. pendulum (claims to support time intervals, but does only some)

Some may be capable of parsing durations like PnYnMnDTnHnMnS but none is able to parse time intervals like for example <start>/<end>. 6.,7. and 8. work partially also with <start>/<end> but none of them works with a partial <end> description. (example '2007-01-10/27')

I considered writing my own string parser but it feels that such fundamental thing like the implementation of the ISO_8601 should be incorporated by one of the above packages.

Questions

  1. Should I write my own string parser with something like map(dateutil.parser.parser, str_.split("/"))?
  2. Is there any python package capable of parsing time intervals of the ISO_8601?
  3. I've seen solutions that use database engines for conversion of str to datetime.datetime, would this be an option for time intervals?