From 86b8f89edadba9df44f0161579f7424de36fce21 Mon Sep 17 00:00:00 2001 From: Eric L Date: Sun, 13 Mar 2022 12:40:08 +0100 Subject: [PATCH] Alternative adapt parse_date to handle ISO dates Revert "Adapt parse_date to handle ISO dates" This reverts commit bd0fce8f1919598aa4f71f419792fc4e4f7ccf58, and provides an alternative to ISO format. --- babel/dates.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/babel/dates.py b/babel/dates.py index 134397584..9e8c5179b 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -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-alike format first, meaning similar to 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') @@ -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)