]> 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:56 +0000 (19:05 +0100)
Patch by Lowe Thiderman

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

index 6c121a5c06d43a48e42f5fa359cca373ee12a3d5..1048992402de790a69e3bd39b2b2b53376cdbb7d 100644 (file)
@@ -67,7 +67,7 @@ def register_adapters_and_converters():
         timepart_full = timepart.split(b".")
         hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
         if len(timepart_full) == 2:
-            microseconds = int(timepart_full[1])
+            microseconds = int('{:0<6}'.format(timepart_full[1].decode()))
         else:
             microseconds = 0
 
index c7551e35a1bd6f09ab6f16b1f04702e9c855f088..87d2cce3ec3172b262c6d608e6901e624036e61a 100644 (file)
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+#-*- coding: iso-8859-1 -*-
 # pysqlite2/test/regression.py: pysqlite regression tests
 #
 # Copyright (C) 2006-2010 Gerhard Häring <gh@ghaering.de>
@@ -302,6 +302,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 205f81327d0c83b33a073eb20e65b87876aa5921..bdb39f29e0e29216c34771bd74ba37ad198a9e05 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1068,6 +1068,7 @@ Anatoly Techtonik
 Mikhail Terekhov
 Richard M. Tew
 Tobias Thelen
+Lowe Thiderman
 Nicolas M. Thiéry
 James Thomas
 Robin Thomas
index 4f14dcdb9ee227dcde423236898ae60ca7774fdc..e5c37fae9e7c16db76206807db71861cdbb3bf2a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -227,6 +227,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.