Skip to content

fix: use configured timezone for MySQL session time_zone#2127

Merged
abondar merged 2 commits intotortoise:mainfrom
Br1an67:fix/mysql-session-timezone
Mar 3, 2026
Merged

fix: use configured timezone for MySQL session time_zone#2127
abondar merged 2 commits intotortoise:mainfrom
Br1an67:fix/mysql-session-timezone

Conversation

@Br1an67
Copy link
Contributor

@Br1an67 Br1an67 commented Mar 1, 2026

Description

When use_tz=True, the MySQL client sets the session time_zone using timezone.now().utcoffset(). Since timezone.now() returns a UTC-aware datetime, its utcoffset() is always timedelta(0), causing the session to always be set to +0:00 regardless of the configured timezone (e.g. Asia/Shanghai).

This PR replaces timezone.now() with get_default_timezone() to compute the correct UTC offset from the user-configured timezone.

Motivation and Context

Fixes #2114

How Has This Been Tested?

  • Added test_mysql_session_timezone_uses_configured_tz in tests/backends/test_connection_params.py
  • The test configures USE_TZ=True and TIMEZONE=Asia/Shanghai, then verifies the SET time_zone call uses +8:00
  • Existing tests continue to pass

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

When use_tz=True, the MySQL client was using timezone.now().utcoffset()
to compute the session time_zone offset. Since timezone.now() returns a
UTC-aware datetime, its utcoffset() is always timedelta(0), so the
session was always set to +0:00 regardless of the configured timezone.

Use get_default_timezone() to obtain the configured timezone and compute
the correct UTC offset from it.

Fixes tortoise#2114
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 3, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing Br1an67:fix/mysql-session-timezone (57d0cfe) with develop (03703a7)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (d914c70) during the generation of this report, so develop (03703a7) was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@abondar
Copy link
Member

abondar commented Mar 3, 2026

@Br1an67 Hi! Check linter here please

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@abondar abondar merged commit 54820ed into tortoise:main Mar 3, 2026
25 of 26 checks passed
abondar pushed a commit that referenced this pull request Mar 3, 2026
* fix: use configured timezone for MySQL session time_zone

When use_tz=True, the MySQL client was using timezone.now().utcoffset()
to compute the session time_zone offset. Since timezone.now() returns a
UTC-aware datetime, its utcoffset() is always timedelta(0), so the
session was always set to +0:00 regardless of the configured timezone.

Use get_default_timezone() to obtain the configured timezone and compute
the correct UTC offset from it.

Fixes #2114

* style: fix ruff formatting in test_connection_params.py

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
(cherry picked from commit 54820ed)
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.

2 participants