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

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant

Open
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
@@ -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 = []
@@ -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' % (
14 changes: 14 additions & 0 deletions Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
@@ -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):

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
Oops, something went wrong.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant