Skip to content

FIX: honor database time zone when adapting datetimes - #552

Open
Guflly (Guflly) wants to merge 3 commits into
microsoft:devfrom
Guflly:fix/371-database-timezone
Open

FIX: honor database time zone when adapting datetimes#552
Guflly (Guflly) wants to merge 3 commits into
microsoft:devfrom
Guflly:fix/371-database-timezone

Conversation

@Guflly

@Guflly Guflly (Guflly) commented Aug 5, 2026

Copy link
Copy Markdown

Uses the database connection's time zone when adapting aware datetime values and when converting datetime lookup SQL to another time zone.

Adds regressions for non-UTC parameter adaptation, cross-time-zone lookup conversion, UTC defaults, and matching time zones.

Tested with python manage.py test testapp.tests.test_timezones.TestDatabaseOperations --noinput.

Fixes #371

Copilot AI lite review requested due to automatic review settings August 5, 2026 14:39
@azure-pipelines

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

@bewithgaurav

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates the SQL Server backend’s datetime adaptation logic to use the database connection’s configured time zone when preparing aware datetime values for persistence/query parameters, aligning write-time behavior with the existing read-time conversion path (and fixing mismatches when USE_TZ=True and the DB time zone isn’t UTC).

Changes:

  • Change DatabaseOperations.adapt_datetimefield_value() to convert aware datetimes to naive using self.connection.timezone (instead of forcing UTC awareness).
  • Add a regression unit test ensuring non-UTC database time zones are honored when adapting aware datetimes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
mssql/operations.py Fixes datetime adaptation to use the connection’s time zone when USE_TZ=True, matching the conversion logic used on reads.
testapp/tests/test_timezones.py Adds a regression test covering adaptation behavior with a non-UTC database time zone.

@Guflly

Copy link
Copy Markdown
Author

Fixed the failing timezone lookup conversion and added coverage for it. The focused tests pass locally. Could you rerun Azure?

@bewithgaurav

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@bewithgaurav

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown

📊 Code Coverage Report

🔥 Diff Coverage

100%


🎯 Overall Coverage

83.44%


📈 Total Lines Covered: 2539 out of 3043
📁 Project: mssql-django


Diff Coverage

Diff: dev...HEAD, staged and unstaged changes

No lines with coverage information in this diff.


📋 Files Needing Attention

📉 Files with overall lowest coverage (click to expand)
- mssql/client.py: 19.5%  (41 lines)
- mssql/__init__.py: 50.0%  (2 lines)
- mssql/creation.py: 56.8%  (74 lines)
- mssql/operations.py: 80.0%  (400 lines)
- mssql/compiler.py: 82.3%  (644 lines)
- mssql/functions.py: 84.1%  (428 lines)
- mssql/base.py: 84.8%  (514 lines)
- mssql/schema.py: 88.3%  (719 lines)
- mssql/introspection.py: 90.2%  (133 lines)
- mssql/features.py: 98.9%  (88 lines)

🔗 Quick Links

⚙️ Build Summary 📋 Coverage Details

View Azure DevOps Build

Browse Full Coverage Report

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'?

3 participants