Skip to content

Commit 8b94514

Browse files
committed
Issue python#14720: sqlite3: Convert datetime microseconds correctly
Patch by Lowe Thiderman
1 parent ed909bc commit 8b94514

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

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(timepart_full[1])
70+
microseconds = int('{:0<6}'.format(timepart_full[1].decode()))
7171
else:
7272
microseconds = 0
7373

Lib/sqlite3/test/regression.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#-*- coding: ISO-8859-1 -*-
1+
#-*- coding: iso-8859-1 -*-
22
# pysqlite2/test/regression.py: pysqlite regression tests
33
#
44
# Copyright (C) 2006-2010 Gerhard Häring <gh@ghaering.de>
@@ -302,6 +302,23 @@ def foo():
302302
cur.executemany("insert into b (baz) values (?)",
303303
((i,) for i in foo()))
304304

305+
def CheckConvertTimestampMicrosecondPadding(self):
306+
"""
307+
http://bugs.python.org/issue14720
308+
309+
The microsecond parsing of convert_timestamp() should pad with zeros,
310+
since the microsecond string "456" actually represents "456000".
311+
"""
312+
313+
con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
314+
cur = con.cursor()
315+
cur.execute("CREATE TABLE t (x TIMESTAMP)")
316+
cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.456')")
317+
cur.execute("SELECT * FROM t")
318+
date = cur.fetchall()[0][0]
319+
320+
self.assertEqual(date, datetime.datetime(2012, 4, 4, 15, 6, 0, 456000))
321+
305322

306323
def suite():
307324
regression_suite = unittest.makeSuite(RegressionTests, "Check")

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ Anatoly Techtonik
10681068
Mikhail Terekhov
10691069
Richard M. Tew
10701070
Tobias Thelen
1071+
Lowe Thiderman
10711072
Nicolas M. Thiéry
10721073
James Thomas
10731074
Robin Thomas

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ Core and Builtins
227227
Library
228228
-------
229229

230+
- Issue #14720: sqlite3: Convert datetime microseconds correctly.
231+
Patch by Lowe Thiderman.
232+
230233
- Issue #17225: JSON decoder now counts columns in the first line starting
231234
with 1, as in other lines.
232235

0 commit comments

Comments
 (0)