From f23a32a28e68df23a038eb07421c4b9aea920b24 Mon Sep 17 00:00:00 2001 From: Twiink Date: Thu, 19 Nov 2026 22:43:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(parsing):=20=E4=BF=9D=E7=95=99=E7=BA=AF?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=8C=BA=E5=81=8F=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs/parsing.md | 2 ++ src/pendulum/parsing/__init__.py | 1 + tests/parsing/test_parsing.py | 11 +++++++++++ tests/test_parsing.py | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/docs/docs/parsing.md b/docs/docs/parsing.md index dd78fd45a..6b9c1215c 100644 --- a/docs/docs/parsing.md +++ b/docs/docs/parsing.md @@ -82,6 +82,7 @@ ParserError: Unable to parse string [31-01-01] ### Time When passing only time information the date will default to today. +An explicit UTC offset is preserved, just as it is for a complete datetime. | String | Output | | --------------------------------- | ------------------------------------------ | @@ -89,6 +90,7 @@ When passing only time information the date will default to today. | 12:04:23 | 2016-12-17T12:04:23+00:00 | | 120423 | 2016-12-17T12:04:23+00:00 | | 12:04:23.45 | 2016-12-17T12:04:23.450000+00:00 | +| 12:04:23+05:30 | 2016-12-17T12:04:23+05:30 | ### Intervals diff --git a/src/pendulum/parsing/__init__.py b/src/pendulum/parsing/__init__.py index d88c089a6..9cc8a1a9e 100644 --- a/src/pendulum/parsing/__init__.py +++ b/src/pendulum/parsing/__init__.py @@ -99,6 +99,7 @@ def _normalize( parsed.minute, parsed.second, parsed.microsecond, + tzinfo=parsed.tzinfo, ) elif isinstance(parsed, date) and not isinstance(parsed, datetime): return datetime(parsed.year, parsed.month, parsed.day) diff --git a/tests/parsing/test_parsing.py b/tests/parsing/test_parsing.py index 8151ce12f..9ca2f5368 100644 --- a/tests/parsing/test_parsing.py +++ b/tests/parsing/test_parsing.py @@ -520,6 +520,17 @@ def test_iso8601_time(): assert parsed.tzinfo is None +@pytest.mark.parametrize("offset", ["+05:30", "-03:45", "+00:00", "Z"]) +def test_iso8601_time_preserves_offset(offset): + text = f"20:12:05.123456{offset}" + now = datetime.datetime(2015, 11, 12) + expected = datetime.datetime.fromisoformat( + f"2015-11-12T{text.replace('Z', '+00:00')}" + ) + + assert parse(text, now=now) == expected + + def test_iso8601_ordinal_invalid(): text = "2012-007-05" diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 34673c40a..e9c8e7f2a 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -1,5 +1,9 @@ from __future__ import annotations +import datetime + +import pytest + import pendulum from tests.conftest import assert_date @@ -48,6 +52,21 @@ def test_parse_with_timezone() -> None: assert dt.offset == 7200 +@pytest.mark.parametrize("offset", ["+05:30", "-03:45", "+00:00", "Z"]) +@pytest.mark.parametrize("tz", ["UTC", "Europe/Paris"]) +def test_parse_time_preserves_offset(offset: str, tz: str) -> None: + text = f"20:12:05.123456{offset}" + expected = datetime.datetime.fromisoformat( + f"2015-11-12T{text.replace('Z', '+00:00')}" + ) + + parsed = pendulum.parse(text, now=datetime.datetime(2015, 11, 12), tz=tz) + + assert isinstance(parsed, pendulum.DateTime) + assert parsed == expected + assert parsed.utcoffset() == expected.utcoffset() + + def test_parse_exact() -> None: text = "2016-10-16T12:34:56.123456+01:30"