From d4069ee0d6ce4666d5491901395de254ac55d3c6 Mon Sep 17 00:00:00 2001 From: "Tomas R." Date: Sat, 19 Oct 2024 14:01:58 +0200 Subject: [PATCH] Bugfix: Replace str.index with str.find (#1130) --- babel/dates.py | 4 ++-- tests/test_dates.py | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/babel/dates.py b/babel/dates.py index 3373e063..4b2cad64 100644 --- a/babel/dates.py +++ b/babel/dates.py @@ -1232,7 +1232,7 @@ def parse_date( 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') @@ -1277,7 +1277,7 @@ def parse_time( # 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') diff --git a/tests/test_dates.py b/tests/test_dates.py index 31ac7822..58e82add 100644 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -17,7 +17,7 @@ import freezegun 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 @@ -666,6 +666,24 @@ def test_parse_time(input, expected): 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): -- 2.47.2