]> granicus.if.org Git - python/commitdiff
Fix when parsing tz offsets microseconds shorter than 6 (#4781)
authorMario Corchero <mariocj89@gmail.com>
Tue, 9 Jan 2018 21:37:26 +0000 (21:37 +0000)
committerAlexander Belopolsky <abalkin@users.noreply.github.com>
Tue, 9 Jan 2018 21:37:26 +0000 (16:37 -0500)
As the remainder was directly parsed as an int, strings like
.600 were parsed as 600 microseconds rather than milliseconds.

Lib/_strptime.py
Lib/test/test_strptime.py

index f5195af90c8a4d56ab6effeddf1465bfed2a18c3..1be04850acf6e98d6ecc6af0a9cd9bf215e6a238 100644 (file)
@@ -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
index 1251886779d20764b98d4a97e2dfb6733fc47e43..af71008bec537eb14f4bcd9c14b04a31c5b806eb 100644 (file)
@@ -345,6 +345,9 @@ class StrptimeTests(unittest.TestCase):
         (*_, 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)