Skip to content

Fix per-database timezone datetime handling - #562

Draft
Hugo Moreira (hugosmoreira) wants to merge 1 commit into
microsoft:devfrom
hugosmoreira:agent/fix-371-per-database-timezone
Draft

Fix per-database timezone datetime handling#562
Hugo Moreira (hugosmoreira) wants to merge 1 commit into
microsoft:devfrom
hugosmoreira:agent/fix-371-per-database-timezone

Conversation

@hugosmoreira

Copy link
Copy Markdown

Summary

  • Adapt aware datetime parameters to the configured database connection time zone before binding them to SQL Server's time-zone-less datetime2 columns.
  • Convert datetime SQL expressions from the connection time zone to the requested time zone instead of assuming the stored source is UTC.
  • Add regressions covering parameter adaptation, exact lookups, ORM round trips, local-date lookups, and both SQL conversion helper paths.

Root cause

convert_datetimefield_value() interpreted time-zone-less values in connection.timezone, while adapt_datetimefield_value() always converted aware parameters to UTC. This made reads and writes asymmetric whenever DATABASES[alias]["TIME_ZONE"] was non-UTC.

The datetime SQL conversion helpers also added only the target offset. Once parameter binding honors the connection time zone, those helpers must convert from the connection offset to the target offset.

Fixes #371

Validation

  • python manage.py test testapp.tests --noinput -v 1 — 214 tests completed successfully (3 skipped, 3 expected failures).
  • Django 6.0.8 upstream timezones module — 84 tests completed successfully (3 skipped, 7 expected failures).
  • Focused TestDateTimeField SQL Server tests — 4 passed.
  • Negative controls confirmed the regressions fail with the original UTC binding, and that changing only parameter binding breaks upstream per-database time-zone conversion.
  • python -m compileall -q mssql testapp
  • git diff --check

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

Datetime conversion issue with USE_TZ and settings.TIME_ZONE != 'UTC'?

1 participant