Skip to content

Commit

Permalink
Adapt parse_date to handle ISO dates in ASCII format
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzolf committed Jun 20, 2022
1 parent 558f26c commit 80f043f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions babel/dates.py
Expand Up @@ -1179,13 +1179,18 @@ class ParseError(ValueError):
def parse_date(string, locale=LC_TIME, format='medium'):
"""Parse a date from a string.
This function uses the date format for the locale as a hint to determine
the order in which the date fields appear in the string.
This function first tries to interpret the string as ISO-8601
date format, then uses the date format for the locale as a hint to
determine the order in which the date fields appear in the string.
>>> parse_date('4/1/04', locale='en_US')
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 @@ -1195,7 +1200,16 @@ 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-8601 format first, meaning similar to formats
# extended YYYY-MM-DD or basic YYYYMMDD
iso_alike = re.match(r'^(\d{4})-?([01]\d)-?([0-3]\d)$',
string, flags=re.ASCII) # allow only ASCII digits
if iso_alike:
try:
return date(*map(int, iso_alike.groups()))
except ValueError:
pass # a locale format might fit better, so let's continue

format_str = get_date_format(format=format, locale=locale).pattern.lower()
year_idx = format_str.index('y')
month_idx = format_str.index('m')
Expand Down

0 comments on commit 80f043f

Please sign in to comment.