(original) (raw)

changeset: 82403:eb45fd74db34 branch: 2.7 parent: 82395:92003d9aae0e user: Petri Lehtinen petri@digip.org date: Tue Feb 26 21:32:02 2013 +0200 files: Doc/library/sqlite3.rst Lib/sqlite3/dbapi2.py Lib/sqlite3/test/regression.py description: Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior diff -r 92003d9aae0e -r eb45fd74db34 Doc/library/sqlite3.rst --- a/Doc/library/sqlite3.rst Tue Feb 26 12:37:07 2013 +0000 +++ b/Doc/library/sqlite3.rst Tue Feb 26 21:32:02 2013 +0200 @@ -832,6 +832,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 -r 92003d9aae0e -r eb45fd74db34 Lib/sqlite3/dbapi2.py --- a/Lib/sqlite3/dbapi2.py Tue Feb 26 12:37:07 2013 +0000 +++ b/Lib/sqlite3/dbapi2.py Tue Feb 26 21:32:02 2013 +0200 @@ -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 @@ 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 -r 92003d9aae0e -r eb45fd74db34 Lib/sqlite3/test/regression.py --- a/Lib/sqlite3/test/regression.py Tue Feb 26 12:37:07 2013 +0000 +++ b/Lib/sqlite3/test/regression.py Tue Feb 26 21:32:02 2013 +0200 @@ -296,11 +296,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(): /gh@ghaering.de/petri@digip.org