Skip to content

Commit 9c19205

Browse files
baldwiccsdebruyn
authored andcommitted
fix nanosecond/microsecond conversion
1 parent bf055bb commit 9c19205

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

dbt/adapters/sqlserver/sql_server_connection_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def byte_array_to_datetime(value: bytes) -> dt.datetime:
263263
hour=tup[3],
264264
minute=tup[4],
265265
second=tup[5],
266-
microsecond=tup[6],
266+
microsecond=tup[6] // 1000, # https://bugs.python.org/issue15443
267267
tzinfo=dt.timezone(dt.timedelta(hours=tup[7], minutes=tup[8])),
268268
)
269269

tests/unit/adapters/sqlserver/test_sql_server_connection_manager.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_bool_to_connection_string_arg(key: str, value: bool, expected: str) ->
8080
assert bool_to_connection_string_arg(key, value) == expected
8181

8282
@pytest.mark.parametrize(
83-
"value, expected", [
83+
"value, expected_datetime, expected_str", [
8484
(
8585
bytes([
8686
0xE5, 0x07, # year: 2022
@@ -89,17 +89,19 @@ def test_bool_to_connection_string_arg(key: str, value: bool, expected: str) ->
8989
0x16, 0x00, # hour: 22
9090
0x16, 0x00, # minute: 22
9191
0x12, 0x00, # second: 18
92-
0x40, 0xE2, 0x01, 0x00, # microsecond: 123456
92+
0x15, 0xCD, 0x5B, 0x07, # microsecond: 123456789
9393
0x02, 0x00, 0x1E, 0x00 # tzinfo: +02:30
9494
]),
95+
dt.datetime(2022, 12, 17, 22, 22, 18, 123456, dt.timezone(dt.timedelta(hours=2, minutes=30))),
9596
"2021-12-17 22:22:18.123456+02:30"
9697
)
9798
]
9899
)
99-
def test_byte_array_to_datetime(value: bytes, expected: dt.datetime) -> None:
100+
def test_byte_array_to_datetime(value: bytes, expected_datetime: dt.datetime, expected_str: str) -> None:
100101
"""
101102
Assert SQL_SS_TIMESTAMPOFFSET_STRUCT bytes convert to string in an expected isoformat
102103
https://docs.python.org/3/library/datetime.html#datetime.datetime.__str__
103104
https://learn.microsoft.com/sql/relational-databases/native-client-odbc-date-time/data-type-support-for-odbc-date-and-time-improvements#sql_ss_timestampoffset_struct
104105
"""
105-
assert str(byte_array_to_datetime(value)) == expected
106+
assert byte_array_to_datetime(value) == expected_datetime
107+
assert str(byte_array_to_datetime(value)) == expected_str

0 commit comments

Comments
 (0)