Minor issue with sqlite3 and datetime
Frank Millman
frank at chagford.com
Sun Apr 29 05:32:23 EDT 2012
Hi all
I could not find a mailing list for sqlite3 - hope it is ok to post here.
My problem actually originates with a different problem relating to MS Sql
Server. Python's datetime.datetime object uses a precision of microseconds.
Sql Server's DATETIME type only uses a precision of milliseconds. When I try
to insert a datetime object into a DATETIME column, the microsecond portion
is rejected, but the rest succeeds, so the object is stored to the nearest
second.
My current workaround is, instead of inserting the datetime object directly,
I use the following -
dtm = dtm.isoformat(sep=' ')[:23]
This converts it to a string, and strips off the last three digits of the
microseconds, converting them to milliseconds. It is not elegant, but it
works.
I also use PostgreSQL and sqlite3. I don't want to code different routines
for each one, so I use the same workaround for all three. It works with
PostgreSQL, but not with sqlite3. Here is why.
sqlite3/dbapi2.py contains the following -
def convert_timestamp(val):
[...]
if len(timepart_full) == 2:
microseconds = int(timepart_full[1])
else:
microseconds = 0
It assumes that 'timepart_full[1]' is a string containing 6 digits. After my
workaround, it only contains 3 digits, so it gives the wrong result.
I think that it should right-pad the string with zeroes to bring it up to 6
digits before converting to an int, like this -
microseconds = int('{:0<6}'.format(timepart_full[1]))
Any chance of this being accepted?
Frank Millman
More information about the Python-list
mailing list