]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix DATETIMEOFFSET formatting to preserve fractional seconds.
authorGord Thompson <gord@gordthompson.com>
Tue, 31 Dec 2019 17:42:09 +0000 (12:42 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 1 Jan 2020 00:29:56 +0000 (19:29 -0500)
Fixed issue where a timezone-aware ``datetime`` value being converted to
string for use as a parameter value of a :class:`.mssql.DATETIMEOFFSET`
column was omitting the fractional seconds.

Fixes: #5045
Closes: #5046
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5046
Pull-request-sha: 99dc7b23f69b7b068795a02d20b88bf352c7dcd7

Change-Id: I971629466fe0675536bbdf55693f0c1821dfb3cc
(cherry picked from commit 0d316e42f0645283c222f050c403c9c8d20463ec)

doc/build/changelog/unreleased_13/5045.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mssql/pyodbc.py
test/dialect/mssql/test_types.py

diff --git a/doc/build/changelog/unreleased_13/5045.rst b/doc/build/changelog/unreleased_13/5045.rst
new file mode 100644 (file)
index 0000000..020e723
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, mssql
+    :tickets: 5045
+
+    Fixed issue where a timezone-aware ``datetime`` value being converted to
+    string for use as a parameter value of a :class:`.mssql.DATETIMEOFFSET`
+    column was omitting the fractional seconds.
index 9ce6861c06a48a429a5cfeae5d2e1ca6ec01d9ce..c3304a70559cffb64aa241e8dbea1c2a6bca842e 100644 (file)
@@ -235,9 +235,9 @@ class _ODBCDateTimeOffset(DATETIMEOFFSET):
     def bind_processor(self, dialect):
         def process(value):
             """Convert to string format required by T-SQL."""
-            dto_string = value.strftime("%Y-%m-%d %H:%M:%S %z")
+            dto_string = value.strftime("%Y-%m-%d %H:%M:%S.%f %z")
             # offset needs a colon, e.g., -0700 -> -07:00
-            return dto_string[:23] + ":" + dto_string[23:]
+            return dto_string[:30] + ":" + dto_string[30:]
 
         return process
 
index f837d3add0a27f8ed6d958a573740ded5a4ca85c..3cf9d9036dd91bd1e14a9f03ff713ae2fc679ac1 100644 (file)
@@ -735,7 +735,7 @@ class TypeRoundTripTest(
             11,
             2,
             32,
-            0,
+            123456,
             util.timezone(datetime.timedelta(hours=1)),
         )
         t.insert().execute(