5

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_))

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?
Milla Well
  • 3,193
  • 3
  • 35
  • 50
  • does this help? https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date – user1558604 Dec 12 '19 at 16:03
  • @user1558604 thank you for the suggestion, this post pointed me to five more python packages that I tried out (I updated the list of non-working packages). But there is no solution in that question working for me, as far as I can tell. – Milla Well Dec 13 '19 at 09:35

0 Answers0