Skip to content

Commit d4069ee

Browse files
authored
Bugfix: Replace str.index with str.find (#1130)
1 parent f91754b commit d4069ee

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

babel/dates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ def parse_date(
12321232

12331233
format_str = get_date_format(format=format, locale=locale).pattern.lower()
12341234
year_idx = format_str.index('y')
1235-
month_idx = format_str.index('m')
1235+
month_idx = format_str.find('m')
12361236
if month_idx < 0:
12371237
month_idx = format_str.index('l')
12381238
day_idx = format_str.index('d')
@@ -1277,7 +1277,7 @@ def parse_time(
12771277

12781278
# TODO: try ISO format first?
12791279
format_str = get_time_format(format=format, locale=locale).pattern.lower()
1280-
hour_idx = format_str.index('h')
1280+
hour_idx = format_str.find('h')
12811281
if hour_idx < 0:
12821282
hour_idx = format_str.index('k')
12831283
min_idx = format_str.index('m')

tests/test_dates.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pytest
1818

1919
from babel import Locale, dates
20-
from babel.dates import NO_INHERITANCE_MARKER, UTC, _localize
20+
from babel.dates import NO_INHERITANCE_MARKER, UTC, _localize, parse_pattern
2121
from babel.util import FixedOffsetTimezone
2222

2323

@@ -666,6 +666,24 @@ def test_parse_time(input, expected):
666666
assert dates.parse_time(input, locale='en_US') == expected
667667

668668

669+
def test_parse_time_alternate_characters(monkeypatch):
670+
# 'K' can be used as an alternative to 'H'
671+
def get_time_format(*args, **kwargs):
672+
return parse_pattern('KK:mm:ss')
673+
monkeypatch.setattr(dates, 'get_time_format', get_time_format)
674+
675+
assert dates.parse_time('9:30') == time(9, 30)
676+
677+
678+
def test_parse_date_alternate_characters(monkeypatch):
679+
# 'l' can be used as an alternative to 'm'
680+
def get_date_format(*args, **kwargs):
681+
return parse_pattern('yyyy-ll-dd')
682+
monkeypatch.setattr(dates, 'get_time_format', get_date_format)
683+
684+
assert dates.parse_date('2024-10-20') == date(2024, 10, 20)
685+
686+
669687
@pytest.mark.parametrize('case', ['', 'a', 'aaa'])
670688
@pytest.mark.parametrize('func', [dates.parse_date, dates.parse_time])
671689
def test_parse_errors(case, func):

0 commit comments

Comments
 (0)