Skip to content

Commit 86b8f89

Browse files
committed
Alternative adapt parse_date to handle ISO dates
Revert "Adapt parse_date to handle ISO dates" This reverts commit bd0fce8, and provides an alternative to ISO format.
1 parent bd0fce8 commit 86b8f89

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

babel/dates.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,12 @@ def parse_date(string, locale=LC_TIME, format='medium'):
11651165
if not numbers:
11661166
raise ParseError("No numbers were found in input")
11671167

1168-
# TODO: try ISO format first?
1168+
# we try ISO-alike format first, meaning similar to YYYY-MM-DD
1169+
if len(numbers) == 3:
1170+
ints = list(map(int, numbers))
1171+
if ints[0] > 31 and ints[1] <= 12 and ints[2] <= 31:
1172+
return date(*ints)
1173+
11691174
format_str = get_date_format(format=format, locale=locale).pattern.lower()
11701175
year_idx = format_str.index('y')
11711176
month_idx = format_str.index('m')
@@ -1181,19 +1186,12 @@ def parse_date(string, locale=LC_TIME, format='medium'):
11811186
# names, both in the requested locale, and english
11821187

11831188
year = numbers[indexes['Y']]
1184-
day = numbers[indexes['D']]
1185-
month = numbers[indexes['M']]
11861189
if len(year) == 2:
1187-
# check if we don't have an ISO kind of format
1188-
if len(day) == 4: # day first locale
1189-
year, month, day = day, month, year
1190-
elif len(month) == 4: # month first locale
1191-
year, month, day = month, day, year
1192-
else:
1193-
year = 2000 + int(year)
1194-
year = int(year)
1195-
month = int(month)
1196-
day = int(day)
1190+
year = 2000 + int(year)
1191+
else:
1192+
year = int(year)
1193+
month = int(numbers[indexes['M']])
1194+
day = int(numbers[indexes['D']])
11971195
if month > 12:
11981196
month, day = day, month
11991197
return date(year, month, day)

0 commit comments

Comments
 (0)