-
Notifications
You must be signed in to change notification settings - Fork 562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request: consistent handling of timezone-aware datetime
parameter values (with datetimeoffset columns)
#810
Comments
I would very much like them to be consistent. I'm just not sure what the right answer is. Is the DB always in UTC and we should therefore convert it? That seems a bit dangerous since there is no way for us to really know that. (Unless the DB has a type that tells us that. I could see making PostgreSQL's I'm open to it if it won't break a bunch of people. |
The first fix that comes to mind is that if the datetime object is timezone-aware then render the parameter value as a string in RFC 3339 format. I just tested against SQL Server and both select cast('1985-04-12T23:20:50.52Z' as datetimeoffset) as foo and select cast('1996-12-19T16:39:57-08:00' as datetimeoffset) as foo seemed to work fine. |
I’m making assumptions here… assuming you know the type of db engine you’re connecting to, you should always send datetime objects (not to be confused with python date objects) as datetimeoffest when the tz is not null and send it as datetime2 of the tz is missing when you know you’re connected to mssql. Sql server does implicit conversion of datetimeoffset to datetime and datetime2, so no regressions introduced there. mssql table value parameters have a schema so there’s no guesssing as to what it should be sent as but I’m sure callers will encounter gotchas with sending python datetime objects in various tz offsets to a tvp that has a datetime2 column. If that’s too much work, maybe you can give us an “input converter”, so we can control how values are sent. We already have an output converter that does the opposite. |
I'm using this as a workaround for the moment which converts datetime objects with a timezone to a string
|
Further to #134, and related to pandas-dev/pandas#30884 I'm adding this as a "wish list" item.
Python version 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]
pyodbc version 4.0.30
driver: msodbcsql17.dll, version: 17.05.0002
connection string:
DRIVER=ODBC Driver 17 for SQL Server;SERVER=localhost,49242;DATABASE=mydb;Trusted_Connection=Yes;UseFMTONLY=Yes;
When a timezone-aware
datetime
objectis passed as a parameter value …
execute
, andexecutemany
withfast_executemany = False
… drop the timezone component:
while
executemany
withfast_executemany = True
inserts the correct valueSince the latter example shows that it "can be done", it would be nice if the handling of such parameter values was consistent.
The text was updated successfully, but these errors were encountered: