summaryrefslogtreecommitdiff
path: root/Lib/sqlite3
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2013-02-23 19:10:29 +0100
committerPetri Lehtinen <petri@digip.org>2013-02-23 19:10:29 +0100
commit60eac9ec0a47c428fdf902241e7871583273cd1e (patch)
treee84967eb3e6a0c5b06cdd2034542166e7e4af77e /Lib/sqlite3
parent40ec524a65afcb34d3ca53e322214cecbdf860d2 (diff)
parent967033e899de87d8fb6b051a7e81c8225748065d (diff)
downloadcpython-60eac9ec0a47c428fdf902241e7871583273cd1e.tar.gz
Issue #14720: sqlite3: Convert datetime microseconds correctly
Diffstat (limited to 'Lib/sqlite3')
-rw-r--r--Lib/sqlite3/dbapi2.py2
-rw-r--r--Lib/sqlite3/test/regression.py17
2 files changed, 18 insertions, 1 deletions
diff --git a/Lib/sqlite3/dbapi2.py b/Lib/sqlite3/dbapi2.py
index 6c121a5c06..1048992402 100644
--- a/Lib/sqlite3/dbapi2.py
+++ b/Lib/sqlite3/dbapi2.py
@@ -67,7 +67,7 @@ def register_adapters_and_converters():
timepart_full = timepart.split(b".")
hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
if len(timepart_full) == 2:
- microseconds = int(timepart_full[1])
+ microseconds = int('{:0<6}'.format(timepart_full[1].decode()))
else:
microseconds = 0
diff --git a/Lib/sqlite3/test/regression.py b/Lib/sqlite3/test/regression.py
index 9d7b27639a..87d2cce3ec 100644
--- a/Lib/sqlite3/test/regression.py
+++ b/Lib/sqlite3/test/regression.py
@@ -302,6 +302,23 @@ class RegressionTests(unittest.TestCase):
cur.executemany("insert into b (baz) values (?)",
((i,) for i in foo()))
+ def CheckConvertTimestampMicrosecondPadding(self):
+ """
+ http://bugs.python.org/issue14720
+
+ The microsecond parsing of convert_timestamp() should pad with zeros,
+ since the microsecond string "456" actually represents "456000".
+ """
+
+ con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES)
+ cur = con.cursor()
+ 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))
+
def suite():
regression_suite = unittest.makeSuite(RegressionTests, "Check")