Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise NotImplementedError('ISO 8601 extended year representation not supported.')
isodatestrlen = len(isodatestr)
if isodatestr.find('W') != -1:
#Handle ISO 8601 week date format
hyphens_present = 1 if isodatestr.find('-') != -1 else 0
week_date_len = 7 + hyphens_present
weekday_date_len = 8 + 2*hyphens_present
if isodatestrlen == week_date_len:
#YYYY-Www
#YYYYWww
return DateResolution.Week
elif isodatestrlen == weekday_date_len:
#YYYY-Www-D
#YYYYWwwD
return DateResolution.Weekday
else:
raise ValueError('String is not a valid ISO 8601 week date.')
#If the size of the string of 4 or less, assume its a truncated year representation
if len(isodatestr) <= 4:
return DateResolution.Year
#An ISO string may be a calendar represntation if:
# 1) When split on a hyphen, the sizes of the parts are 4, 2, 2 or 4, 2
# 2) There are no hyphens, and the length is 8
datestrsplit = isodatestr.split('-')
#Check case 1
if len(datestrsplit) == 2:
if len(datestrsplit[0]) == 4 and len(datestrsplit[1]) == 2:
return DateResolution.Month
#year in the week containing the 4th of January
#http://en.wikipedia.org/wiki/ISO_week_date
fourth_jan = datetime.date(isoyear, 1, 4)
#Note the conversion from ISO day (1 - 7) and Python day (0 - 6)
delta = datetime.timedelta(fourth_jan.isoweekday() - 1)
#Return the start of the year
return fourth_jan - delta
_resolution_map = {
DateResolution.Day: _parse_calendar_day,
DateResolution.Ordinal: _parse_ordinal_date,
DateResolution.Month: _parse_calendar_month,
DateResolution.Week: _parse_week,
DateResolution.Weekday: _parse_week_day,
DateResolution.Year: _parse_year
}