From 8db62005425f4f941f3010be8f874981d27f9a3a Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 17:22:55 -0600 Subject: [PATCH] fix(coreprops): parse fractional seconds in W3CDTF timestamps A core-property timestamp with a decimal fraction of the second, such as 2026-01-01T00:30:00.125+05:30, had everything after the 19th character treated as a timezone offset. Since the remainder was never exactly six characters, both the fraction and a numeric offset were silently dropped and the value was read as local time without UTC conversion. Parse the fractional part before examining the offset, truncating to microsecond precision. Fixes #1155 --- src/pptx/oxml/coreprops.py | 11 +++++++++++ tests/parts/test_coreprops.py | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/pptx/oxml/coreprops.py b/src/pptx/oxml/coreprops.py index de6b26b24..0df14ef71 100644 --- a/src/pptx/oxml/coreprops.py +++ b/src/pptx/oxml/coreprops.py @@ -227,6 +227,8 @@ def _offset_dt(cls, datetime: dt.datetime, offset_str: str): _offset_pattern = re.compile(r"([+-])(\d\d):(\d\d)") + _fraction_pattern = re.compile(r"\.(\d+)") + @classmethod def _parse_W3CDTF_to_datetime(cls, w3cdtf_str: str) -> dt.datetime: # valid W3CDTF date cases: @@ -249,6 +251,15 @@ def _parse_W3CDTF_to_datetime(cls, w3cdtf_str: str) -> dt.datetime: if timestamp is None: tmpl = "could not parse W3CDTF datetime string '%s'" raise ValueError(tmpl % w3cdtf_str) + if offset_str.startswith("."): + # W3CDTF allows a decimal fraction of the second before the + # timezone designator; datetime only keeps microsecond precision + fraction_match = cls._fraction_pattern.match(offset_str) + if fraction_match is not None: + digits = fraction_match.group(1) + microseconds = int(digits[:6].ljust(6, "0")) + timestamp = timestamp.replace(microsecond=microseconds) + offset_str = offset_str[fraction_match.end() :] if len(offset_str) == 6: return cls._offset_dt(timestamp, offset_str) return timestamp diff --git a/tests/parts/test_coreprops.py b/tests/parts/test_coreprops.py index 0983218e4..f7f9c20b7 100644 --- a/tests/parts/test_coreprops.py +++ b/tests/parts/test_coreprops.py @@ -78,6 +78,24 @@ def it_knows_the_date_property_values( actual_datetime = getattr(core_properties, prop_name) assert actual_datetime == expected_value + @pytest.mark.parametrize( + ("str_val", "expected_value"), + [ + ("2026-01-01T00:30:00.125+05:30", dt.datetime(2025, 12, 31, 19, 0, 0, 125000)), + ("2026-01-01T00:30:00.123456789-08:00", dt.datetime(2026, 1, 1, 8, 30, 0, 123456)), + ("2026-01-01T00:30:00.5Z", dt.datetime(2026, 1, 1, 0, 30, 0, 500000)), + ("2026-01-01T00:30:00.25", dt.datetime(2026, 1, 1, 0, 30, 0, 250000)), + ("2012-11-17T11:07:40-05:30", dt.datetime(2012, 11, 17, 16, 37, 40)), + ], + ) + def it_reads_date_properties_with_fractional_seconds( + self, str_val: str, expected_value: dt.datetime + ): + coreProperties = self.coreProperties_xml("dcterms:modified", str_val) + core_properties = CorePropertiesPart.load(None, None, None, coreProperties) # type: ignore + + assert core_properties.modified == expected_value + @pytest.mark.parametrize( ("prop_name", "tagname", "value", "str_val", "attrs"), [