Skip to content

Commit e956b28

Browse files
authored
Make seconds optional in parse_time time formats (#1141)
1 parent f1c8633 commit e956b28

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

babel/dates.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from __future__ import annotations
1919

20+
import math
2021
import re
2122
import warnings
2223
from functools import lru_cache
@@ -1281,7 +1282,9 @@ def parse_time(
12811282
if hour_idx < 0:
12821283
hour_idx = format_str.index('k')
12831284
min_idx = format_str.index('m')
1284-
sec_idx = format_str.index('s')
1285+
# format might not contain seconds
1286+
if (sec_idx := format_str.find('s')) < 0:
1287+
sec_idx = math.inf
12851288

12861289
indexes = sorted([(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')])
12871290
indexes = {item[1]: idx for idx, item in enumerate(indexes)}

tests/test_dates.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,14 @@ def test_parse_time(input, expected):
679679
assert dates.parse_time(input, locale='en_US') == expected
680680

681681

682+
def test_parse_time_no_seconds_in_format():
683+
# parse time using a time format which does not include seconds
684+
locale = 'cs_CZ'
685+
fmt = 'short'
686+
assert dates.get_time_format(format=fmt, locale=locale).pattern == 'H:mm'
687+
assert dates.parse_time('9:30', locale=locale, format=fmt) == time(9, 30)
688+
689+
682690
def test_parse_time_alternate_characters(monkeypatch):
683691
# 'K' can be used as an alternative to 'H'
684692
def get_time_format(*args, **kwargs):

0 commit comments

Comments
 (0)