]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Make seconds optional in `parse_time` time formats (#1141)
authorTomas R. <tomas.roun8@gmail.com>
Sun, 20 Oct 2024 06:50:36 +0000 (08:50 +0200)
committerGitHub <noreply@github.com>
Sun, 20 Oct 2024 06:50:36 +0000 (09:50 +0300)
babel/dates.py
tests/test_dates.py

index 30321b76779c623d03827b074b6952b2993290a7..2f1b116e4c67ab238e565e8ca9722b330b923df6 100644 (file)
@@ -17,6 +17,7 @@
 
 from __future__ import annotations
 
+import math
 import re
 import warnings
 from functools import lru_cache
@@ -1281,7 +1282,9 @@ def parse_time(
     if hour_idx < 0:
         hour_idx = format_str.index('k')
     min_idx = format_str.index('m')
-    sec_idx = format_str.index('s')
+    # format might not contain seconds
+    if (sec_idx := format_str.find('s')) < 0:
+        sec_idx = math.inf
 
     indexes = sorted([(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')])
     indexes = {item[1]: idx for idx, item in enumerate(indexes)}
index 549929be7b6690676123e66cf5e8a35811b69de9..fef68ae738a62158a3a5f9a0cace6f140a2f62b7 100644 (file)
@@ -679,6 +679,14 @@ def test_parse_time(input, expected):
     assert dates.parse_time(input, locale='en_US') == expected
 
 
+def test_parse_time_no_seconds_in_format():
+    # parse time using a time format which does not include seconds
+    locale = 'cs_CZ'
+    fmt = 'short'
+    assert dates.get_time_format(format=fmt, locale=locale).pattern == 'H:mm'
+    assert dates.parse_time('9:30', locale=locale, format=fmt) == time(9, 30)
+
+
 def test_parse_time_alternate_characters(monkeypatch):
     # 'K' can be used as an alternative to 'H'
     def get_time_format(*args, **kwargs):