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"), [