Skip to content

Commit

Permalink
Alternative adapt parse_date to handle ISO dates
Browse files Browse the repository at this point in the history
Revert "Adapt parse_date to handle ISO dates"

This reverts commit bd0fce8,
and provides an alternative to ISO format.
  • Loading branch information
ericzolf committed Mar 13, 2022
1 parent bd0fce8 commit 8fe352f
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions babel/dates.py
Expand Up @@ -1165,7 +1165,12 @@ def parse_date(string, locale=LC_TIME, format='medium'):
if not numbers:
raise ParseError("No numbers were found in input")

# TODO: try ISO format first?
# we try ISO format first YYYY-MM-DD
if len(numbers) == 3:
ints = list(map(int, numbers))
if ints[0] > 31 and ints[1] <= 12 and ints[2] <= 31:
return date(*ints)

format_str = get_date_format(format=format, locale=locale).pattern.lower()
year_idx = format_str.index('y')
month_idx = format_str.index('m')
Expand All @@ -1181,19 +1186,12 @@ 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:
# 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)
year = 2000 + int(year)
else:
year = int(year)
month = int(numbers[indexes['M']])
day = int(numbers[indexes['D']])
if month > 12:
month, day = day, month
return date(year, month, day)
Expand Down

0 comments on commit 8fe352f

Please sign in to comment.