Skip to content

Commit

Permalink
pythongh-53502: Fix plistlib.dump() for naive datetime with aware_dat…
Browse files Browse the repository at this point in the history
…etime option
  • Loading branch information
serhiy-storchaka committed Jan 2, 2024
1 parent b2566d8 commit c064c1e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Lib/plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def _date_from_string(s, aware_datetime):


def _date_to_string(d, aware_datetime):
if aware_datetime and d.tzinfo is not None:
if aware_datetime:
d = d.astimezone(datetime.UTC)
return '%04d-%02d-%02dT%02d:%02d:%02dZ' % (
d.year, d.month, d.day,
Expand Down Expand Up @@ -791,7 +791,7 @@ def _write_object(self, value):
self._fp.write(struct.pack('>Bd', 0x23, value))

elif isinstance(value, datetime.datetime):
if self._aware_datetime and value.tzinfo is not None:
if self._aware_datetime:
dt = value.astimezone(datetime.UTC)
offset = dt - datetime.datetime(2001, 1, 1, tzinfo=datetime.UTC)
f = offset.total_seconds()
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_plistlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ def test_dump_naive_datetime_with_aware_datetime_option(self):
for fmt in ALL_FORMATS:
s = plistlib.dumps(dt, fmt=fmt, aware_datetime=True)
parsed = plistlib.loads(s, aware_datetime=False)
self.assertEqual(parsed, dt)
expected = dt.astimezone(datetime.UTC).replace(tzinfo=None)
self.assertEqual(parsed, expected)


class TestBinaryPlistlib(unittest.TestCase):
Expand Down

0 comments on commit c064c1e

Please sign in to comment.