Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion babel/messages/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ def _has_python_brace_format(string: str) -> bool:
return field_name_seen


def _parse_datetime_header(value: str) -> datetime.datetime:
def _parse_datetime_header(value: str) -> datetime.datetime | None:
# Some tools (e.g. Poedit) leave the header's value blank instead of
# eliding the header, or leaving the "YEAR-MO-DA HO:MI+ZONE" placeholder.
if not value.strip():
return None

match = re.match(r'^(?P<datetime>.*?)(?P<tzoffset>[+-]\d{4})?$', value)

dt = datetime.datetime.strptime(match.group('datetime'), '%Y-%m-%d %H:%M')
Expand Down
10 changes: 10 additions & 0 deletions tests/messages/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import pickle
from io import StringIO

import pytest

from babel.dates import UTC, format_datetime
from babel.messages import catalog, pofile
from babel.util import FixedOffsetTimezone
Expand Down Expand Up @@ -556,6 +558,14 @@ def test_datetime_parsing():
assert val2.tzinfo is None


@pytest.mark.parametrize('value', ['', ' '])
def test_datetime_parsing_blank_value_returns_none(value):
# Some tools (e.g. Poedit) leave the header blank instead of eliding
# it or using the "YEAR-MO-DA HO:MI+ZONE" placeholder; this used to
# raise ValueError from strptime instead of being treated as unset.
assert catalog._parse_datetime_header(value) is None


def test_update_catalog_comments():
# Based on https://web.archive.org/web/20100710131029/http://babel.edgewall.org/attachment/ticket/163/cat-update-comments.py

Expand Down