Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
if len(datestrsplit) == 3:
if len(datestrsplit[0]) == 4 and len(datestrsplit[1]) == 2 and len(datestrsplit[2]) == 2:
return DateResolution.Day
#Check case 2
#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
}