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
26 changes: 26 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,32 @@ def test_fromisoformat_utc(self):

self.assertIs(dt.tzinfo, timezone.utc)

def test_fromisoformat_utc_subsecond_offset(self):
# A UTC offset whose whole-second part is zero but with a non-zero
# microsecond part must be preserved, not collapsed to UTC.
for us in (1, -1, 999999, -999999):
with self.subTest(microseconds=us):
tz = timezone(timedelta(microseconds=us))
dt = self.theclass(2020, 6, 15, 12, 34, 56, tzinfo=tz)
rt = self.theclass.fromisoformat(dt.isoformat())
self.assertEqual(rt.utcoffset(), timedelta(microseconds=us))
self.assertEqual(rt, dt)
self.assertIsNot(rt.tzinfo, timezone.utc)

tz = timezone(timedelta(hours=5, minutes=30, seconds=15,
microseconds=123456))
dt = self.theclass(2020, 6, 15, 12, 34, 56, tzinfo=tz)
rt = self.theclass.fromisoformat(dt.isoformat())
self.assertEqual(rt.utcoffset(), tz.utcoffset(None))
self.assertEqual(rt, dt)

for tstr in ('2020-06-15T12:34:56+00:00',
'2020-06-15T12:34:56+00:00:00.000000',
'2020-06-15T12:34:56Z'):
with self.subTest(tstr=tstr):
self.assertIs(self.theclass.fromisoformat(tstr).tzinfo,
timezone.utc)

def test_fromisoformat_subclass(self):
class DateTimeSubclass(self.theclass):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`datetime.datetime.fromisoformat` in the C implementation dropping
the sub-second part of a UTC offset whose whole-second part is zero, matching
the pure-Python implementation.
2 changes: 1 addition & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,7 @@ tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds)
PyObject *tzinfo;
if (rv == 1) {
// Create a timezone from offset in seconds (0 returns UTC)
if (tzoffset == 0) {
if (tzoffset == 0 && tz_useconds == 0) {
return Py_NewRef(CONST_UTC(NO_STATE));
}

Expand Down
Loading