Skip to content

fix: MsSQL timezone-safe timestamp normalization via AT TIME ZONE#51

Merged
dtsong merged 2 commits intomasterfrom
fix/30-mssql-timezone-normalization
Mar 30, 2026
Merged

fix: MsSQL timezone-safe timestamp normalization via AT TIME ZONE#51
dtsong merged 2 commits intomasterfrom
fix/30-mssql-timezone-normalization

Conversation

@dtsong
Copy link
Copy Markdown
Owner

@dtsong dtsong commented Mar 30, 2026

Summary

  • Wrap MsSQL normalize_timestamp() with explicit UTC conversion
  • Uses CAST(value AS DATETIMEOFFSET) AT TIME ZONE 'UTC' before formatting
  • MsSQL cannot set session timezone, so this normalizes explicitly in SQL

Key Files

  • data_diff/databases/mssql.pynormalize_timestamp() now converts to UTC

Test plan

  • Unit tests pass (55/55)
  • Ruff lint and format pass
  • MsSQL timestamp normalization produces UTC-consistent output

Closes #30

🤖 Generated with Claude Code

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>
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7d62aedf23

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Convert to UTC to ensure consistent cross-database comparisons.
# MsSQL cannot set a session timezone, so we normalize explicitly.
# Cast to datetimeoffset (assumes server local time), then convert to UTC.
utc_value = f"CAST({value} AS DATETIMEOFFSET) AT TIME ZONE 'UTC'"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve compatibility with pre-2016 SQL Server

normalize_timestamp() now hard-codes AT TIME ZONE 'UTC', but AT TIME ZONE is only supported on SQL Server 2016+ (per Microsoft T-SQL docs). On SQL Server 2012/2014 this expression is a syntax error, so any diff that touches temporal columns will fail at query time, whereas the previous implementation used only FORMAT/SUBSTRING and remained compatible.

Useful? React with 👍 / 👎.

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>
@dtsong dtsong merged commit 9e4e745 into master Mar 30, 2026
5 of 6 checks passed
@dtsong dtsong deleted the fix/30-mssql-timezone-normalization branch March 30, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: MsSQL timezone-safe timestamp normalization via AT TIME ZONE

1 participant