Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _parse_duration_combined(durationstr, relative):
#Period of the form PT<time>
#Split the string in to its component parts
datepart, timepart = durationstr[1:].split('T') #We skip the 'P'
datevalue = parse_date(datepart)
timevalue = parse_time(timepart)
if relative is True:
return dateutil.relativedelta.relativedelta(years=datevalue.year, months=datevalue.month, days=datevalue.day, hours=timevalue.hour, minutes=timevalue.minute, seconds=timevalue.second, microseconds=timevalue.microsecond)
else:
totaldays = datevalue.year * 365 + datevalue.month * 30 + datevalue.day
return datetime.timedelta(days=totaldays, hours=timevalue.hour, minutes=timevalue.minute, seconds=timevalue.second, microseconds=timevalue.microsecond)
</time>
def parse_datetime(isodatetimestr, delimiter='T'):
#Given a string in ISO 8601 date time format, return a datetime.datetime
#object that corresponds to the given date time.
#By default, the ISO 8601 specified T delimiter is used to split the
#date and time (T<time>). Fixed offset tzdata will be included
#if UTC offset is given in the input string.
isodatestr, isotimestr = isodatetimestr.split(delimiter)
datepart = parse_date(isodatestr)
timepart = parse_time(isotimestr)
return datetime.datetime.combine(datepart, timepart)
</time>
#See if we need to upconvert to datetime to preserve resolution
if secondpart.find(datetimedelimiter) != -1:
return (startdate, datetime.combine(startdate, datetime.min.time()) + duration)
else:
return (startdate, startdate + duration)
else:
#/
if firstpart.find(datetimedelimiter) != -1 and secondpart.find(datetimedelimiter) != -1:
#Both parts are datetimes
return (parse_datetime(firstpart, delimiter=datetimedelimiter), parse_datetime(secondpart, delimiter=datetimedelimiter))
elif firstpart.find(datetimedelimiter) != -1 and secondpart.find(datetimedelimiter) == -1:
#First part is a datetime, second part is a date
return (parse_datetime(firstpart, delimiter=datetimedelimiter), parse_date(secondpart))
elif firstpart.find(datetimedelimiter) == -1 and secondpart.find(datetimedelimiter) != -1:
#First part is a date, second part is a datetime
return (parse_date(firstpart), parse_datetime(secondpart, delimiter=datetimedelimiter))
else:
#Both parts are dates
return (parse_date(firstpart), parse_date(secondpart))
return (enddate, datetime.combine(enddate, datetime.min.time()) - duration)
else:
return (enddate, enddate - duration)
elif secondpart[0] == 'P':
#/
#We need to figure out if is a date, or a datetime
if firstpart.find(datetimedelimiter) != -1:
# is a datetime
duration = parse_duration(secondpart, relative=relative)
startdatetime = parse_datetime(firstpart, delimiter=datetimedelimiter)
return (startdatetime, startdatetime + duration)
else:
# must just be a date
duration = parse_duration(secondpart, relative=relative)
startdate = parse_date(firstpart)
#See if we need to upconvert to datetime to preserve resolution
if secondpart.find(datetimedelimiter) != -1:
return (startdate, datetime.combine(startdate, datetime.min.time()) + duration)
else:
return (startdate, startdate + duration)
else:
#/
if firstpart.find(datetimedelimiter) != -1 and secondpart.find(datetimedelimiter) != -1:
#Both parts are datetimes
return (parse_datetime(firstpart, delimiter=datetimedelimiter), parse_datetime(secondpart, delimiter=datetimedelimiter))
elif firstpart.find(datetimedelimiter) != -1 and secondpart.find(datetimedelimiter) == -1:
#First part is a datetime, second part is a date
return (parse_datetime(firstpart, delimiter=datetimedelimiter), parse_date(secondpart))
elif firstpart.find(datetimedelimiter) == -1 and secondpart.find(datetimedelimiter) != -1:
#First part is a date, second part is a datetime
else:
return (startdate, startdate + duration)
else:
#/
if firstpart.find(datetimedelimiter) != -1 and secondpart.find(datetimedelimiter) != -1:
#Both parts are datetimes
return (parse_datetime(firstpart, delimiter=datetimedelimiter), parse_datetime(secondpart, delimiter=datetimedelimiter))
elif firstpart.find(datetimedelimiter) != -1 and secondpart.find(datetimedelimiter) == -1:
#First part is a datetime, second part is a date
return (parse_datetime(firstpart, delimiter=datetimedelimiter), parse_date(secondpart))
elif firstpart.find(datetimedelimiter) == -1 and secondpart.find(datetimedelimiter) != -1:
#First part is a date, second part is a datetime
return (parse_date(firstpart), parse_datetime(secondpart, delimiter=datetimedelimiter))
else:
#Both parts are dates
return (parse_date(firstpart), parse_date(secondpart))
#Notice that these are not returned 'in order' (earlier to later), this
#is to maintain consistency with parsing / durations, as
#well as making repeating interval code cleaner. Users who desire
#durations to be in order can use the 'sorted' operator.
#We need to figure out if is a date, or a datetime
if secondpart.find(datetimedelimiter) != -1:
# is a datetime
duration = parse_duration(firstpart, relative=relative)
enddatetime = parse_datetime(secondpart, delimiter=datetimedelimiter)
return (enddatetime, enddatetime - duration)
else:
# must just be a date
duration = parse_duration(firstpart, relative=relative)
enddate = parse_date(secondpart)
#See if we need to upconvert to datetime to preserve resolution
if firstpart.find(datetimedelimiter) != -1:
return (enddate, datetime.combine(enddate, datetime.min.time()) - duration)
else:
return (enddate, enddate - duration)
elif secondpart[0] == 'P':
#/
#We need to figure out if is a date, or a datetime
if firstpart.find(datetimedelimiter) != -1:
# is a datetime
duration = parse_duration(secondpart, relative=relative)
startdatetime = parse_datetime(firstpart, delimiter=datetimedelimiter)
return (startdatetime, startdatetime + duration)
else: