Skip to content

Commit

Permalink
Adapt parse_date to handle ISO dates
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzolf committed Feb 20, 2022
1 parent 4f8c7f6 commit bd0fce8
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions babel/dates.py
Expand Up @@ -1152,6 +1152,10 @@ def parse_date(string, locale=LC_TIME, format='medium'):
datetime.date(2004, 4, 1)
>>> parse_date('01.04.2004', locale='de_DE')
datetime.date(2004, 4, 1)
>>> parse_date('2004-04-01', locale='en_US')
datetime.date(2004, 4, 1)
>>> parse_date('2004-04-01', locale='de_DE')
datetime.date(2004, 4, 1)
:param string: the string containing the date
:param locale: a `Locale` object or a locale identifier
Expand All @@ -1177,12 +1181,19 @@ def parse_date(string, locale=LC_TIME, format='medium'):
# names, both in the requested locale, and english

year = numbers[indexes['Y']]
day = numbers[indexes['D']]
month = numbers[indexes['M']]
if len(year) == 2:
year = 2000 + int(year)
else:
year = int(year)
month = int(numbers[indexes['M']])
day = int(numbers[indexes['D']])
# check if we don't have an ISO kind of format
if len(day) == 4: # day first locale
year, month, day = day, month, year
elif len(month) == 4: # month first locale
year, month, day = month, day, year
else:
year = 2000 + int(year)
year = int(year)
month = int(month)
day = int(day)
if month > 12:
month, day = day, month
return date(year, month, day)
Expand Down

0 comments on commit bd0fce8

Please sign in to comment.