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
5 changes: 5 additions & 0 deletions Doc/library/plistlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ This module defines the following functions:
.. versionchanged:: 3.13
The keyword-only parameter *aware_datetime* has been added.

.. versionchanged:: next
``<date>`` values that omit smaller units (for example ``2024-06Z``)
are now accepted, with the omitted components defaulting to the start
of the period. Previously such values raised :exc:`TypeError`.


.. function:: loads(data, *, fmt=None, dict_type=dict, aware_datetime=False)

Expand Down
10 changes: 4 additions & 6 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,11 @@ def _decode_base64(s):

def _date_from_string(s, aware_datetime):
order = ('year', 'month', 'day', 'hour', 'minute', 'second')
# Smaller units may be omitted; default them to the start of the period.
defaults = (1, 1, 1, 0, 0, 0)
gd = _dateParser.match(s).groupdict()
lst = []
for key in order:
val = gd[key]
if val is None:
break
lst.append(int(val))
lst = [int(val) if (val := gd[key]) is not None else default
for key, default in zip(order, defaults)]
if aware_datetime:
return datetime.datetime(*lst, tzinfo=datetime.UTC)
return datetime.datetime(*lst)
Expand Down
18 changes: 18 additions & 0 deletions Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,24 @@ def test_load_aware_datetime(self):
aware_datetime=True)
self.assertEqual(dt.tzinfo, datetime.UTC)

def test_load_partial_datetime(self):
# Smaller units may be omitted; missing components default to the
# start of the period.
for data, expected in [
(b"<plist><date>2024Z</date></plist>",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if 2024Z is a valid ISO 8601 format. While year only (YYYY) format is ISO, I think timezone designators (in this case Zulu time) can only be attached to complete time value, not a standalone year.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, Z only belongs on a full time value, so 2024Z isn't strictly valid ISO 8601. Whether to accept partial dates at all is still being discussed in the issue. If accepted, I'll drop the Z from the examples.

datetime.datetime(2024, 1, 1)),
(b"<plist><date>2024-06Z</date></plist>",
datetime.datetime(2024, 6, 1)),
(b"<plist><date>2024-06-07Z</date></plist>",
datetime.datetime(2024, 6, 7)),
(b"<plist><date>2024-06-07T08Z</date></plist>",
datetime.datetime(2024, 6, 7, 8)),
(b"<plist><date>2024-06-07T08:09Z</date></plist>",
datetime.datetime(2024, 6, 7, 8, 9)),
]:
with self.subTest(data=data):
self.assertEqual(plistlib.loads(data), expected)

@unittest.skipUnless("America/Los_Angeles" in zoneinfo.available_timezones(),
"Can't find timezone datebase")
def test_dump_aware_datetime(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :mod:`plistlib` raising a confusing :exc:`TypeError` when loading a
``<date>`` that omits smaller units (for example ``2024-06Z`` or ``2024Z``).
The omitted components now default to the start of the period.
Loading