[Python-checkins] cpython (merge 3.3 -> default): Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior

petri.lehtinen python-checkins at python.org
Tue Feb 26 20:47:35 CET 2013


http://hg.python.org/cpython/rev/0db66afbd746
changeset: 82406:0db66afbd746
parent: 82402:0d55fb0217f1
parent: 82405:17673a8c7083
user: Petri Lehtinen <petri at digip.org>
date: Tue Feb 26 21:46:12 2013 +0200
summary:
 Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior
files:
 Doc/library/sqlite3.rst | 4 ++++
 Lib/sqlite3/dbapi2.py | 2 +-
 Lib/sqlite3/test/regression.py | 13 +++++++++++--
 3 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst
--- a/Doc/library/sqlite3.rst
+++ b/Doc/library/sqlite3.rst
@@ -842,6 +842,10 @@
 
 .. 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
--- a/Lib/sqlite3/dbapi2.py
+++ b/Lib/sqlite3/dbapi2.py
@@ -67,7 +67,7 @@
 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
 
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -313,11 +313,20 @@
 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():
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /