Skip to content

Commit ce949b2

Browse files
committed
Issue #14720: sqlite3: Convert datetime microseconds correctly
Patch by Lowe Thiderman
1 parent 4718f24 commit ce949b2

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

Lib/sqlite3/dbapi2.py

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

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-2007 Gerhard Häring <gh@ghaering.de>
@@ -285,6 +285,23 @@ def foo():
285285
cur.executemany("insert into b (baz) values (?)",
286286
((i,) for i in foo()))
287287

288+
def CheckConvertTimestampMicrosecondPadding(self):
289+
"""
290+
http://bugs.python.org/issue14720
291+
292+
The microsecond parsing of convert_timestamp() should pad with zeros,
293+
since the microsecond string "456" actually represents "456000".
294+
"""
295+
296+
con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
297+
cur = con.cursor()
298+
cur.execute("CREATE TABLE t (x TIMESTAMP)")
299+
cur.execute("INSERT INTO t (x) VALUES ('2012-04-04 15:06:00.456')")
300+
cur.execute("SELECT * FROM t")
301+
date = cur.fetchall()[0][0]
302+
303+
self.assertEqual(date, datetime.datetime(2012, 4, 4, 15, 6, 0, 456000))
304+
288305

289306
def suite():
290307
regression_suite = unittest.makeSuite(RegressionTests, "Check")

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ Anatoly Techtonik
990990
Mikhail Terekhov
991991
Richard M. Tew
992992
Tobias Thelen
993+
Lowe Thiderman
993994
Nicolas M. Thiéry
994995
James Thomas
995996
Robin Thomas

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ Core and Builtins
208208
Library
209209
-------
210210

211+
- Issue #14720: sqlite3: Convert datetime microseconds correctly.
212+
Patch by Lowe Thiderman.
213+
211214
- Issue #17225: JSON decoder now counts columns in the first line starting
212215
with 1, as in other lines.
213216

0 commit comments

Comments
 (0)