Skip to content

Commit 9e4e745

Browse files
dtsongclaude
andauthored
fix: MsSQL timezone-safe timestamp normalization via AT TIME ZONE (#51)
* fix: MsSQL timezone-safe timestamp normalization via AT TIME ZONE Wrap MsSQL normalize_timestamp() with explicit UTC conversion using CAST(value AS DATETIMEOFFSET) AT TIME ZONE 'UTC'. MsSQL cannot set a session timezone, so we normalize explicitly in the SQL to ensure consistent cross-database timestamp comparisons. Closes #30 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: only apply UTC conversion for TimestampTZ columns in MsSQL CAST(datetime AS DATETIMEOFFSET) assigns +00:00 unconditionally, not the server's local timezone. UTC conversion only helps for datetimeoffset columns (TimestampTZ). For timezone-naive datetime/datetime2 columns, the conversion is a no-op — values are used as-is since the source timezone is unknown. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 18af77a commit 9e4e745

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

data_diff/databases/mssql.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ def constant_values(self, rows) -> str:
138138
return f"VALUES {values}"
139139

140140
def normalize_timestamp(self, value: str, coltype: TemporalType) -> str:
141+
# For timezone-aware columns (datetimeoffset), convert to UTC explicitly
142+
# since MsSQL cannot set a session timezone.
143+
# For timezone-naive columns (datetime/datetime2), no conversion is possible
144+
# without knowing the source timezone — values are used as-is.
145+
if isinstance(coltype, TimestampTZ):
146+
value = f"{value} AT TIME ZONE 'UTC'"
147+
141148
if coltype.precision > 0:
142149
formatted_value = (
143150
f"FORMAT({value}, 'yyyy-MM-dd HH:mm:ss') + '.' + "

0 commit comments

Comments
 (0)