format_str = get_date_format(format=format, locale=locale).pattern.lower()
year_idx = format_str.index('y')
- month_idx = format_str.index('m')
+ month_idx = format_str.find('m')
if month_idx < 0:
month_idx = format_str.index('l')
day_idx = format_str.index('d')
# TODO: try ISO format first?
format_str = get_time_format(format=format, locale=locale).pattern.lower()
- hour_idx = format_str.index('h')
+ hour_idx = format_str.find('h')
if hour_idx < 0:
hour_idx = format_str.index('k')
min_idx = format_str.index('m')
import pytest
from babel import Locale, dates
-from babel.dates import NO_INHERITANCE_MARKER, UTC, _localize
+from babel.dates import NO_INHERITANCE_MARKER, UTC, _localize, parse_pattern
from babel.util import FixedOffsetTimezone
assert dates.parse_time(input, locale='en_US') == expected
+def test_parse_time_alternate_characters(monkeypatch):
+ # 'K' can be used as an alternative to 'H'
+ def get_time_format(*args, **kwargs):
+ return parse_pattern('KK:mm:ss')
+ monkeypatch.setattr(dates, 'get_time_format', get_time_format)
+
+ assert dates.parse_time('9:30') == time(9, 30)
+
+
+def test_parse_date_alternate_characters(monkeypatch):
+ # 'l' can be used as an alternative to 'm'
+ def get_date_format(*args, **kwargs):
+ return parse_pattern('yyyy-ll-dd')
+ monkeypatch.setattr(dates, 'get_time_format', get_date_format)
+
+ assert dates.parse_date('2024-10-20') == date(2024, 10, 20)
+
+
@pytest.mark.parametrize('case', ['', 'a', 'aaa'])
@pytest.mark.parametrize('func', [dates.parse_date, dates.parse_time])
def test_parse_errors(case, func):