Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_time raises when a format doesn't contain seconds #1129

Open
tomasr8 opened this issue Sep 22, 2024 · 1 comment
Open

parse_time raises when a format doesn't contain seconds #1129

tomasr8 opened this issue Sep 22, 2024 · 1 comment

Comments

@tomasr8
Copy link
Member

tomasr8 commented Sep 22, 2024

Overview Description

parse_time raises a ValueError when the given time format doesn't contain seconds e.g. H:mm.

Steps to Reproduce

>>> from babel.dates import parse_time
>>> parse_time('9:30', 'cs_CZ', 'short')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/dev/babel/babel/dates.py", line 1292, in parse_time
    sec_idx = format_str.index('s')
              ^^^^^^^^^^^^^^^^^^^^^
ValueError: substring not found

This happens because the parse_time function expects the format to have seconds, but some locales do not have those.
For example this is the CLDR definition for the short format in Czech:

<timeFormatLength type="short">
	<timeFormat>
		<pattern>H:mm</pattern>
		<datetimeSkeleton>Hmm</datetimeSkeleton>
	</timeFormat>
</timeFormatLength>

I think the obvious fix is to make the seconds optional and default to zero.

@tomasr8
Copy link
Member Author

tomasr8 commented Sep 23, 2024

@akx Let me know if this is worth fixing, happy to send a PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant