Skip to content

Commit f80c0ca

Browse files
mariocj89abalkin
authored andcommitted
Fix when parsing tz offsets microseconds shorter than 6 (#4781)
As the remainder was directly parsed as an int, strings like .600 were parsed as 600 microseconds rather than milliseconds.
1 parent d4864c6 commit f80c0ca

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Lib/_strptime.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
470470
minutes = int(z[3:5])
471471
seconds = int(z[5:7] or 0)
472472
gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
473-
gmtoff_fraction = int(z[8:] or 0)
473+
gmtoff_remainder = z[8:]
474+
# Pad to always return microseconds.
475+
gmtoff_remainder_padding = "0" * (6 - len(gmtoff_remainder))
476+
gmtoff_fraction = int(gmtoff_remainder + gmtoff_remainder_padding)
474477
if z.startswith("-"):
475478
gmtoff = -gmtoff
476479
gmtoff_fraction = -gmtoff_fraction

Lib/test/test_strptime.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ def test_offset(self):
345345
(*_, offset), _, offset_fraction = _strptime._strptime("-01:30:30.000001", "%z")
346346
self.assertEqual(offset, -(one_hour + half_hour + half_minute))
347347
self.assertEqual(offset_fraction, -1)
348+
(*_, offset), _, offset_fraction = _strptime._strptime("+01:30:30.001", "%z")
349+
self.assertEqual(offset, one_hour + half_hour + half_minute)
350+
self.assertEqual(offset_fraction, 1000)
348351
(*_, offset), _, offset_fraction = _strptime._strptime("Z", "%z")
349352
self.assertEqual(offset, 0)
350353
self.assertEqual(offset_fraction, 0)

0 commit comments

Comments
 (0)