mirror of
https://github.com/python/cpython.git
synced 2025-10-22 22:53:06 +00:00
Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior
This commit is contained in:
commit
6401ad66a7
3 changed files with 18 additions and 5 deletions
|
@ -830,6 +830,10 @@ The following example demonstrates this.
|
||||||
|
|
||||||
.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
|
.. 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:
|
.. _sqlite3-controlling-transactions:
|
||||||
|
|
||||||
|
|
|
@ -67,7 +67,7 @@ def register_adapters_and_converters():
|
||||||
timepart_full = timepart.split(b".")
|
timepart_full = timepart.split(b".")
|
||||||
hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
|
hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
|
||||||
if len(timepart_full) == 2:
|
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:
|
else:
|
||||||
microseconds = 0
|
microseconds = 0
|
||||||
|
|
||||||
|
|
|
@ -313,11 +313,20 @@ class RegressionTests(unittest.TestCase):
|
||||||
con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
|
con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("CREATE TABLE t (x TIMESTAMP)")
|
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))
|
# 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")
|
||||||
|
values = [x[0] for x in cur.fetchall()]
|
||||||
|
|
||||||
|
self.assertEqual(values, [
|
||||||
|
datetime.datetime(2012, 4, 4, 15, 6, 0, 456000),
|
||||||
|
datetime.datetime(2012, 4, 4, 15, 6, 0, 123456),
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue