From: Petri Lehtinen <petri@digip.org>
Date: Sat, 23 Feb 2013 18:05:09 +0000 (+0100)
Subject: Issue #14720: sqlite3: Convert datetime microseconds correctly
X-Git-Tag: v2.7.4rc1~81
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9e14755b46c48207b44be92093ca2eae6ba22fac;p=python

Issue #14720: sqlite3: Convert datetime microseconds correctly

Patch by Lowe Thiderman
---

diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py
index 7eb28e87b1..be7a50a65b 100644
--- a/Lib/sqlite3/dbapi2.py
+++ b/Lib/sqlite3/dbapi2.py
@@ -68,7 +68,7 @@ def register_adapters_and_converters():
         timepart_full = timepart.split(".")
         hours, minutes, seconds = map(int, timepart_full[0].split(":"))
         if len(timepart_full) == 2:
-            microseconds = int(timepart_full[1])
+            microseconds = int('{:0<6}'.format(timepart_full[1].decode()))
         else:
             microseconds = 0
 
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index eec2fcde95..06de982b66 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
 # pysqlite2/test/regression.py: pysqlite regression tests
 #
 # Copyright (C) 2006-2007 Gerhard Häring <gh@ghaering.de>
@@ -285,6 +285,23 @@ class RegressionTests(unittest.TestCase):
             cur.executemany("insert into b (baz) values (?)",
                             ((i,) for i in foo()))
 
+    def CheckConvertTimestampMicrosecondPadding(self):
+        """
+        http://bugs.python.org/issue14720
+
+        The microsecond parsing of convert_timestamp() should pad with zeros,
+        since the microsecond string "456" actually represents "456000".
+        """
+
+        con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
+        cur = con.cursor()
+        cur.execute("CREATE TABLE t (x TIMESTAMP)")
+        cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.456')")
+        cur.execute("SELECT * FROM t")
+        date = cur.fetchall()[0][0]
+
+        self.assertEqual(date, datetime.datetime(2012, 4, 4, 15, 6, 0, 456000))
+
 
 def suite():
     regression_suite = unittest.makeSuite(RegressionTests, "Check")
diff --git a/Misc/ACKS b/Misc/ACKS
index 93c568c7c0..c488644113 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -990,6 +990,7 @@ Anatoly Techtonik
 Mikhail Terekhov
 Richard M. Tew
 Tobias Thelen
+Lowe Thiderman
 Nicolas M. Thiéry
 James Thomas
 Robin Thomas
diff --git a/Misc/NEWS b/Misc/NEWS
index 3a9d50b7bc..7a51f8ac81 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -208,6 +208,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14720: sqlite3: Convert datetime microseconds correctly.
+  Patch by Lowe Thiderman.
+
 - Issue #17225: JSON decoder now counts columns in the first line starting
   with 1, as in other lines.