I need to convert
FROM
a list of str
s
TO
a list of
- either a
datetime.datetime
- or a
datetime.datetime
plus adatetime.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
dateutil.parser
pyiso8601
isodate
arrow
ciso8601
iso8601utils
(claims to support time intervals, but does only some)maya
(offers the functionality, but the implementation is flawed)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
- Should I write my own string parser with something like
map(dateutil.parser.parser, str_.split("/"))
? - Is there any python package capable of parsing time intervals of the
ISO_8601
? - I've seen solutions that use database engines for conversion of
str
todatetime.datetime
, would this be an option for time intervals?