]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Adapt parse_date to handle ISO dates in ASCII format
authorEric L <ewl+git@lavar.de>
Sun, 20 Feb 2022 09:35:01 +0000 (10:35 +0100)
committerAarni Koskela <akx@iki.fi>
Mon, 31 Oct 2022 17:30:05 +0000 (19:30 +0200)
babel/dates.py

index a30cac9d116ad46fde073a17aa88a37789aaec6f..8228bef888a7bc8745b07467db15dba741dac760 100644 (file)
@@ -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
@@ -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')