Skip to content

Commit 4ddc11f

Browse files
committed
do not convert to utc
1 parent 49a04c3 commit 4ddc11f

File tree

2 files changed

+5
-35
lines changed

2 files changed

+5
-35
lines changed

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,6 @@ SQLRETURN SQLGetData_wrap(SqlHandlePtr StatementHandle, SQLUSMALLINT colCount, p
28052805
microseconds,
28062806
tzinfo
28072807
);
2808-
py_dt = py_dt.attr("astimezone")(datetime.attr("timezone").attr("utc"));
28092808
row.append(py_dt);
28102809
} else {
28112810
LOG("Error fetching DATETIMEOFFSET for column {}, ret={}", i, ret);
@@ -3318,7 +3317,6 @@ SQLRETURN FetchBatchData(SQLHSTMT hStmt, ColumnBuffers& buffers, py::list& colum
33183317
dtoValue.fraction / 1000, // ns → µs
33193318
tzinfo
33203319
);
3321-
py_dt = py_dt.attr("astimezone")(datetime.attr("timezone").attr("utc"));
33223320
row.append(py_dt);
33233321
} else {
33243322
row.append(py::none());

tests/test_004_cursor.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7887,12 +7887,7 @@ def test_datetimeoffset_read_write(cursor, db_connection):
78877887
assert row is not None
78887888
fetched_id, fetched_dt = row
78897889
assert fetched_dt.tzinfo is not None
7890-
expected_utc = dt.astimezone(timezone.utc)
7891-
fetched_utc = fetched_dt.astimezone(timezone.utc)
7892-
# Ignore sub-microsecond differences
7893-
expected_utc = expected_utc.replace(microsecond=int(expected_utc.microsecond / 1000) * 1000)
7894-
fetched_utc = fetched_utc.replace(microsecond=int(fetched_utc.microsecond / 1000) * 1000)
7895-
assert fetched_utc == expected_utc
7890+
assert fetched_dt == dt
78967891
finally:
78977892
cursor.execute("DROP TABLE IF EXISTS #pytest_datetimeoffset_read_write;")
78987893
db_connection.commit()
@@ -7926,12 +7921,7 @@ def test_datetimeoffset_max_min_offsets(cursor, db_connection):
79267921
assert fetched_id == expected_id, f"ID mismatch: expected {expected_id}, got {fetched_id}"
79277922
assert fetched_dt.tzinfo is not None, f"Fetched datetime object is naive for id {fetched_id}"
79287923

7929-
# Compare in UTC to avoid offset differences
7930-
expected_utc = expected_dt.astimezone(timezone.utc).replace(tzinfo=None)
7931-
fetched_utc = fetched_dt.astimezone(timezone.utc).replace(tzinfo=None)
7932-
assert fetched_utc == expected_utc, (
7933-
f"Value mismatch for id {expected_id}: expected UTC {expected_utc}, got {fetched_utc}"
7934-
)
7924+
assert fetched_dt == expected_dt, f"Value mismatch for id {expected_id}: expected {expected_dt}, got {fetched_dt}"
79357925

79367926
finally:
79377927
cursor.execute("DROP TABLE IF EXISTS #pytest_datetimeoffset_read_write;")
@@ -7986,12 +7976,7 @@ def test_datetimeoffset_dst_transitions(cursor, db_connection):
79867976
assert fetched_id == expected_id, f"ID mismatch: expected {expected_id}, got {fetched_id}"
79877977
assert fetched_dt.tzinfo is not None, f"Fetched datetime object is naive for id {fetched_id}"
79887978

7989-
# Compare UTC time to avoid issues due to offsets changing in DST
7990-
expected_utc = expected_dt.astimezone(timezone.utc).replace(tzinfo=None)
7991-
fetched_utc = fetched_dt.astimezone(timezone.utc).replace(tzinfo=None)
7992-
assert fetched_utc == expected_utc, (
7993-
f"Value mismatch for id {expected_id}: expected UTC {expected_utc}, got {fetched_utc}"
7994-
)
7979+
assert fetched_dt == expected_dt, f"Value mismatch for id {expected_id}: expected {expected_dt}, got {fetched_dt}"
79957980

79967981
finally:
79977982
cursor.execute("DROP TABLE IF EXISTS #pytest_datetimeoffset_dst_transitions;")
@@ -8068,17 +8053,7 @@ def test_datetimeoffset_executemany(cursor, db_connection):
80688053
fetched_id, fetched_dto = rows[i]
80698054
assert fetched_dto.tzinfo is not None, "Fetched datetime object is naive."
80708055

8071-
expected_utc = python_dt.astimezone(timezone.utc).replace(tzinfo=None)
8072-
fetched_utc = fetched_dto.astimezone(timezone.utc).replace(tzinfo=None)
8073-
8074-
# Round microseconds to nearest millisecond for comparison
8075-
expected_utc = expected_utc.replace(microsecond=int(expected_utc.microsecond / 1000) * 1000)
8076-
fetched_utc = fetched_utc.replace(microsecond=int(fetched_utc.microsecond / 1000) * 1000)
8077-
8078-
assert fetched_utc == expected_utc, (
8079-
f"Value mismatch for test case {i}. "
8080-
f"Expected UTC: {expected_utc}, Got UTC: {fetched_utc}"
8081-
)
8056+
assert fetched_dto == python_dt, f"Value mismatch for id {fetched_id}: expected {python_dt}, got {fetched_dto}"
80828057
finally:
80838058
cursor.execute("IF OBJECT_ID('tempdb..#pytest_dto', 'U') IS NOT NULL DROP TABLE #pytest_dto;")
80848059
db_connection.commit()
@@ -8144,10 +8119,7 @@ def test_datetimeoffset_extreme_offsets(cursor, db_connection):
81448119
for i, dt in enumerate(extreme_offsets):
81458120
_, fetched = rows[i]
81468121
assert fetched.tzinfo is not None
8147-
# Round-trip comparison via UTC
8148-
expected_utc = dt.astimezone(timezone.utc).replace(tzinfo=None)
8149-
fetched_utc = fetched.astimezone(timezone.utc).replace(tzinfo=None)
8150-
assert expected_utc == fetched_utc, f"Extreme offset round-trip failed for {dt.tzinfo}"
8122+
assert fetched == dt, f"Value mismatch for id {i}: expected {dt}, got {fetched}"
81518123
finally:
81528124
cursor.execute("IF OBJECT_ID('tempdb..#pytest_dto', 'U') IS NOT NULL DROP TABLE #pytest_dto;")
81538125
db_connection.commit()

0 commit comments

Comments
 (0)