[Python-checkins] Fix when parsing tz offsets microseconds shorter than 6 (#4781)

Alexander Belopolsky webhook-mailer at python.org
Tue Jan 9 16:37:29 EST 2018


https://github.com/python/cpython/commit/f80c0ca13330112fe4d8018609c085ef556cb5bf
commit: f80c0ca13330112fe4d8018609c085ef556cb5bf
branch: master
author: Mario Corchero <mariocj89 at gmail.com>
committer: Alexander Belopolsky <abalkin at users.noreply.github.com>
date: 2018-01-09T16:37:26-05:00
summary:

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.

files:
M Lib/_strptime.py
M Lib/test/test_strptime.py

diff --git a/Lib/_strptime.py b/Lib/_strptime.py
index f5195af90c8..1be04850acf 100644
--- a/Lib/_strptime.py
+++ b/Lib/_strptime.py
@@ -470,7 +470,10 @@ def _strptime(data_string, format="%a %b %d %H:%M:%S %Y"):
                 minutes = int(z[3:5])
                 seconds = int(z[5:7] or 0)
                 gmtoff = (hours * 60 * 60) + (minutes * 60) + seconds
-                gmtoff_fraction = int(z[8:] or 0)
+                gmtoff_remainder = z[8:]
+                # Pad to always return microseconds.
+                gmtoff_remainder_padding = "0" * (6 - len(gmtoff_remainder))
+                gmtoff_fraction = int(gmtoff_remainder + gmtoff_remainder_padding)
                 if z.startswith("-"):
                     gmtoff = -gmtoff
                     gmtoff_fraction = -gmtoff_fraction
diff --git a/Lib/test/test_strptime.py b/Lib/test/test_strptime.py
index 1251886779d..af71008bec5 100644
--- a/Lib/test/test_strptime.py
+++ b/Lib/test/test_strptime.py
@@ -345,6 +345,9 @@ def test_offset(self):
         (*_, offset), _, offset_fraction = _strptime._strptime("-01:30:30.000001", "%z")
         self.assertEqual(offset, -(one_hour + half_hour + half_minute))
         self.assertEqual(offset_fraction, -1)
+        (*_, offset), _, offset_fraction = _strptime._strptime("+01:30:30.001", "%z")
+        self.assertEqual(offset, one_hour + half_hour + half_minute)
+        self.assertEqual(offset_fraction, 1000)
         (*_, offset), _, offset_fraction = _strptime._strptime("Z", "%z")
         self.assertEqual(offset, 0)
         self.assertEqual(offset_fraction, 0)



More information about the Python-checkins mailing list