Skip to content

gh-85255: Teach plistlib to load and dump NSDate.distantPast represented as year 0 #134533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,15 @@ def _decode_base64(s):
_dateParser = re.compile(r"(?P<year>\d\d\d\d)(?:-(?P<month>\d\d)(?:-(?P<day>\d\d)(?:T(?P<hour>\d\d)(?::(?P<minute>\d\d)(?::(?P<second>\d\d))?)?)?)?)?Z", re.ASCII)


# NSDate.distantPast is represented in an unparseable format, see #85255.
_distantPast = '0000-12-30T00:00:00Z'


def _date_from_string(s, aware_datetime):
if s == _distantPast:
if aware_datetime:
return datetime.datetime.min.astimezone(datetime.UTC)
return datetime.datetime.min
order = ('year', 'month', 'day', 'hour', 'minute', 'second')
gd = _dateParser.match(s).groupdict()
lst = []
Expand All @@ -155,6 +163,8 @@ def _date_from_string(s, aware_datetime):


def _date_to_string(d, aware_datetime):
if d.year == datetime.datetime.min.year:
return _distantPast
if aware_datetime:
d = d.astimezone(datetime.UTC)
return '%04d-%02d-%02dT%02d:%02d:%02dZ' % (
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,20 @@ def test_dump_naive_datetime_with_aware_datetime_option(self):
expected = dt.astimezone(datetime.UTC).replace(tzinfo=None)
self.assertEqual(parsed, expected)

def test_round_trip_distant_past(self):
# Issue #85255: NSDate.distantPast is represented as year 0.
before = b"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>applicationDate</key>
<date>0000-12-30T00:00:00Z</date>
</dict>
</plist>
"""
after = plistlib.dumps(plistlib.loads(before, fmt=plistlib.FMT_XML))
self.assertEqual(before, after)


class TestBinaryPlistlib(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Teach :mod:`plistlib` to load and dump ``NSDate.distantPast`` represented as
year 0. Patch by John Keith Hohm
Loading