]> granicus.if.org Git - python/commitdiff
Issue #14720: sqlite3: Convert datetime microseconds correctly
authorPetri Lehtinen <petri@digip.org>
Sat, 23 Feb 2013 18:05:09 +0000 (19:05 +0100)
committerPetri Lehtinen <petri@digip.org>
Sat, 23 Feb 2013 18:05:09 +0000 (19:05 +0100)
Patch by Lowe Thiderman

Lib/sqlite3/dbapi2.py
Lib/sqlite3/test/regression.py
Misc/ACKS
Misc/NEWS

index 7eb28e87b1b0177d3b18f7d421d1e7f6dc1a349c..be7a50a65bdfb18185a31175b4ed22f25621a4dc 100644 (file)
@@ -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
 
index eec2fcde95148a3727a918be98fed2cb90b9128e..06de982b66d1318f6881d668eed200d836b66055 100644 (file)
@@ -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")
index 93c568c7c0e31f1efda795881aed73be2934ed7c..c488644113c7f8a76f3762a87b53930d98ad0836 100644 (file)
--- 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
index 3a9d50b7bc68aa0c35a38c2c2684abc10f560af0..7a51f8ac818abc61f62d4cbcbb4fb77d104c27cb 100644 (file)
--- 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.