Skip to content

Commit e56504a

Browse files
committed
Parse UTC designators in from_format offset tokens
1 parent e06dace commit e56504a

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎src/pendulum/formatting/formatter.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ def _get_parsed_value(
594594
elif token in ["X", "x"]:
595595
parsed["timestamp"] = parsed_token
596596
elif token in ["ZZ", "Z"]:
597+
if value in ["Z", "z"]:
598+
value = "+00:00"
599+
597600
negative = bool(value.startswith("-"))
598601
tz = value[1:]
599602
if ":" not in tz:

‎tests/datetime/test_from_format.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ def test_from_format_with_timezone():
3737
assert d.timezone_name == "Europe/London"
3838

3939

40+
@pytest.mark.parametrize("token", ["Z", "ZZ"])
41+
@pytest.mark.parametrize("offset", ["Z", "z", "+00:00", "+0000"])
42+
@pytest.mark.parametrize("default_tz", ["UTC", "Europe/Paris"])
43+
def test_from_format_with_utc_designator(token, offset, default_tz):
44+
d = pendulum.from_format(
45+
f"1975-05-21T22:32:11{offset}",
46+
f"YYYY-MM-DD[T]HH:mm:ss{token}",
47+
tz=default_tz,
48+
)
49+
50+
assert_datetime(d, 1975, 5, 21, 22, 32, 11)
51+
assert d.offset == 0
52+
53+
4054
def test_from_format_with_square_bracket_in_timezone():
4155
with pytest.raises(ValueError, match=r"^String does not match format"):
4256
pendulum.from_format(

0 commit comments

Comments
 (0)