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
11 changes: 11 additions & 0 deletions src/pptx/oxml/coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/parts/test_coreprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down