Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f79409

Browse files
committed
Issue #14720: Enhance sqlite3 microsecond conversion, document its behavior
1 parent 7aaa1ef commit 5f79409

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

Doc/library/sqlite3.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,10 @@ The following example demonstrates this.
814814

815815
.. literalinclude:: ../includes/sqlite3/pysqlite_datetime.py
816816

817+
If a timestamp stored in SQLite has a fractional part longer than 6
818+
numbers, its value will be truncated to microsecond precision by the
819+
timestamp converter.
820+
817821

818822
.. _sqlite3-controlling-transactions:
819823

Lib/sqlite3/dbapi2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def convert_timestamp(val):
6767
timepart_full = timepart.split(b".")
6868
hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
6969
if len(timepart_full) == 2:
70-
microseconds = int('{:0<6}'.format(timepart_full[1].decode()))
70+
microseconds = int('{:0<6.6}'.format(timepart_full[1].decode()))
7171
else:
7272
microseconds = 0
7373

Lib/sqlite3/test/regression.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,20 @@ def CheckConvertTimestampMicrosecondPadding(self):
313313
con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
314314
cur = con.cursor()
315315
cur.execute("CREATE TABLE t (x TIMESTAMP)")
316+
317+
# Microseconds should be 456000
316318
cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.456')")
319+
320+
# Microseconds should be truncated to 123456
321+
cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.123456789')")
322+
317323
cur.execute("SELECT * FROM t")
318-
date = cur.fetchall()[0][0]
324+
values = [x[0] for x in cur.fetchall()]
319325

320-
self.assertEqual(date, datetime.datetime(2012, 4, 4, 15, 6, 0, 456000))
326+
self.assertEqual(values, [
327+
datetime.datetime(2012, 4, 4, 15, 6, 0, 456000),
328+
datetime.datetime(2012, 4, 4, 15, 6, 0, 123456),
329+
])
321330

322331

323332
def suite():

0 commit comments

Comments
 (0)