summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2013-02-26 19:32:02 (GMT)
committerPetri Lehtinen <petri@digip.org>2013-02-26 19:34:33 (GMT)
commite41a4634c979f6c31b26302568ec249f575fe370 (patch)
tree0019dbc149fb4295cb1797ff260fa6066d37682c
parentba48264bce265a70157181dbd499f99a50ec0931 (diff)
downloadcpython-e41a4634c979f6c31b26302568ec249f575fe370.zip
cpython-e41a4634c979f6c31b26302568ec249f575fe370.tar.gz
cpython-e41a4634c979f6c31b26302568ec249f575fe370.tar.bz2
Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior
-rw-r--r--Doc/library/sqlite3.rst4
-rw-r--r--Lib/sqlite3/dbapi2.py4
-rw-r--r--Lib/sqlite3/test/regression.py13
3 files changed, 17 insertions, 4 deletions
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
index 9b913af..c32bb90 100644
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -832,6 +832,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:
diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py
index be7a50a..00a798b 100644
--- a/Lib/sqlite3/dbapi2.py
+++ b/Lib/sqlite3/dbapi2.py
@@ -1,4 +1,4 @@
-#-*- coding: ISO-8859-1 -*-
+# -*- coding: iso-8859-1 -*-
# pysqlite2/dbapi2.py: the DB-API 2.0 interface
#
# Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
@@ -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('{:0<6}'.format(timepart_full[1].decode()))
+ microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
else:
microseconds = 0
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index 06de982..8a39d59 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -296,11 +296,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():