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

Skip to content

Commit e460f26

Browse files
committed
Issue #14720: sqlite3: Convert datetime microseconds correctly
2 parents 2005447 + f484efd commit e460f26

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -1192,6 +1192,7 @@ Anatoly Techtonik
11921192
Mikhail Terekhov
11931193
Richard M. Tew
11941194
Tobias Thelen
1195+
Lowe Thiderman
11951196
Nicolas M. Thiéry
11961197
James Thomas
11971198
Robin Thomas

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ Core and Builtins
260260
Library
261261
-------
262262

263+
- Issue #14720: sqlite3: Convert datetime microseconds correctly.
264+
Patch by Lowe Thiderman.
265+
263266
- Issue #15132: Allow a list for the defaultTest argument of
264267
unittest.TestProgram. Patch by Jyrki Pulliainen.
265268

0 commit comments

Comments
 (0)