]> granicus.if.org Git - python/commitdiff
Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior
authorPetri Lehtinen <petri@digip.org>
Tue, 26 Feb 2013 19:32:02 +0000 (21:32 +0200)
committerPetri Lehtinen <petri@digip.org>
Tue, 26 Feb 2013 19:38:17 +0000 (21:38 +0200)
Doc/library/sqlite3.rst
Lib/sqlite3/dbapi2.py
Lib/sqlite3/test/regression.py

index 0d7baef165df0caffc2dafaf31fda57a3b4eed4e..eeb966624a298dbec2052d4616f2bf8453d7f4fb 100644 (file)
@@ -814,6 +814,10 @@ The following example demonstrates this.
 
 .. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
 
+If a timestamp stored in SQLite has a fractional part longer than 6
+numbers, its value will be truncated to microsecond precision by the
+timestamp converter.
+
 
 .. _sqlite3-controlling-transactions:
 
index 1048992402de790a69e3bd39b2b2b53376cdbb7d..9a0b76645e1e702b828de884dfbcbd0a02c7c49a 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('{:0<6}'.format(timepart_full[1].decode()))
+            microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
         else:
             microseconds = 0
 
index 87d2cce3ec3172b262c6d608e6901e624036e61a..5e2fbf94351fe16895c3041a42fed4dfcc24c46a 100644 (file)
@@ -313,11 +313,20 @@ class RegressionTests(unittest.TestCase):
         con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
         cur = con.cursor()
         cur.execute("CREATE TABLE t (x TIMESTAMP)")
+
+        # Microseconds should be 456000
         cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.456')")
+
+        # Microseconds should be truncated to 123456
+        cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.123456789')")
+
         cur.execute("SELECT * FROM t")
-        date = cur.fetchall()[0][0]
+        values = [x[0] for x in cur.fetchall()]
 
-        self.assertEqual(date, datetime.datetime(2012, 4, 4, 15, 6, 0, 456000))
+        self.assertEqual(values, [
+            datetime.datetime(2012, 4, 4, 15, 6, 0, 456000),
+            datetime.datetime(2012, 4, 4, 15, 6, 0, 123456),
+        ])
 
 
 def suite():