Skip to content

Commit 8a5e4bd

Browse files
j4321akx
andcommitted
Add format argument to parse_*
``parse_date()`` does not allow the user to specify the date's format and the 'medium' format is used by default in the call to ``get_date_format()`` on line 1144. This results in a failure to parse the date in short format for the locale 'sv_SE'. This commit adds the format argument to avoid this failure. The default value is set to 'medium' to preserve the old behavior. Fixes #657 Co-authored-by: Aarni Koskela <akx@iki.fi>
1 parent 335884a commit 8a5e4bd

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

babel/dates.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ class ParseError(ValueError):
11421142
pass
11431143

11441144

1145-
def parse_date(string, locale=LC_TIME):
1145+
def parse_date(string, locale=LC_TIME, format='medium'):
11461146
"""Parse a date from a string.
11471147
11481148
This function uses the date format for the locale as a hint to determine
@@ -1155,18 +1155,19 @@ def parse_date(string, locale=LC_TIME):
11551155
11561156
:param string: the string containing the date
11571157
:param locale: a `Locale` object or a locale identifier
1158+
:param format: the format to use (see ``get_date_format``)
11581159
"""
11591160
numbers = re.findall(r'(\d+)', string)
11601161
if not numbers:
11611162
raise ParseError("No numbers were found in input")
11621163

11631164
# TODO: try ISO format first?
1164-
format = get_date_format(locale=locale).pattern.lower()
1165-
year_idx = format.index('y')
1166-
month_idx = format.index('m')
1165+
format_str = get_date_format(format=format, locale=locale).pattern.lower()
1166+
year_idx = format_str.index('y')
1167+
month_idx = format_str.index('m')
11671168
if month_idx < 0:
1168-
month_idx = format.index('l')
1169-
day_idx = format.index('d')
1169+
month_idx = format_str.index('l')
1170+
day_idx = format_str.index('d')
11701171

11711172
indexes = [(year_idx, 'Y'), (month_idx, 'M'), (day_idx, 'D')]
11721173
indexes.sort()
@@ -1187,7 +1188,7 @@ def parse_date(string, locale=LC_TIME):
11871188
return date(year, month, day)
11881189

11891190

1190-
def parse_time(string, locale=LC_TIME):
1191+
def parse_time(string, locale=LC_TIME, format='medium'):
11911192
"""Parse a time from a string.
11921193
11931194
This function uses the time format for the locale as a hint to determine
@@ -1198,6 +1199,7 @@ def parse_time(string, locale=LC_TIME):
11981199
11991200
:param string: the string containing the time
12001201
:param locale: a `Locale` object or a locale identifier
1202+
:param format: the format to use (see ``get_time_format``)
12011203
:return: the parsed time
12021204
:rtype: `time`
12031205
"""
@@ -1206,12 +1208,12 @@ def parse_time(string, locale=LC_TIME):
12061208
raise ParseError("No numbers were found in input")
12071209

12081210
# TODO: try ISO format first?
1209-
format = get_time_format(locale=locale).pattern.lower()
1210-
hour_idx = format.index('h')
1211+
format_str = get_time_format(format=format, locale=locale).pattern.lower()
1212+
hour_idx = format_str.index('h')
12111213
if hour_idx < 0:
1212-
hour_idx = format.index('k')
1213-
min_idx = format.index('m')
1214-
sec_idx = format.index('s')
1214+
hour_idx = format_str.index('k')
1215+
min_idx = format_str.index('m')
1216+
sec_idx = format_str.index('s')
12151217

12161218
indexes = [(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')]
12171219
indexes.sort()
@@ -1222,7 +1224,7 @@ def parse_time(string, locale=LC_TIME):
12221224
# Check if the format specifies a period to be used;
12231225
# if it does, look for 'pm' to figure out an offset.
12241226
hour_offset = 0
1225-
if 'a' in format:
1227+
if 'a' in format_str:
12261228
if 'pm' in string.lower():
12271229
hour_offset = 12
12281230

tests/test_dates.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ def test_format_timedelta():
775775
def test_parse_date():
776776
assert dates.parse_date('4/1/04', locale='en_US') == date(2004, 4, 1)
777777
assert dates.parse_date('01.04.2004', locale='de_DE') == date(2004, 4, 1)
778+
assert dates.parse_date('2004-04-01', locale='sv_SE', format='short') == date(2004, 4, 1)
778779

779780

780781
@pytest.mark.parametrize('input, expected', [

0 commit comments

Comments
 (0)