[python-win32] datetime values in adodbapi

Max Slimmer maxslimmer at gmail.com
Mon Nov 6 17:47:48 EST 2017


Coincidentally I ran into this issue last week. From what I could gather
the MS SQL data type can accept values with no more than millisecond
precision. This is true using parameterized statements through adodbapi and
also T-SQL and implicit conversion from char to datetime. The type accepts
up to seven places (nano seconds).

If you don’t mind losing some precision you can do something like this:

import datetime
import adodbapi
class MyTimeconverter(adodbapi.pythonDateTimeConverter):

    def DateObjectToIsoFormatString(self, obj):
        '''
        Round microsecond part of datetime object to three decimal places.
        '''
        s = super(adodbapi.pythonDateTimeConverter,
self).DateObjectToIsoFormatString(obj)
        try:
            dt, micros = s.rsplit('.', 1)
        except ValueError:
            return s
        micros = str(round(float('.' + micros), 3))[1:5]
        return dt + micros
# Bind datetime.datetime parameters as string
adodbapi.typeMap[datetime.datetime] = adodbapi.adBSTR# Patch on our
datetime converter
adodbapi.adodbapi.dateconverter = MyTimeconverter()

Or maybe you can use datetime2.

—Max III
​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20171106/c4645424/attachment.html>


More information about the python-win32 mailing list