Skip to content

fix: handle timezone correctly when DJANGO_CELERY_BEAT_TZ_AWARE=False (Issue #924) #926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

NeverStopDreamingWang
Copy link

@NeverStopDreamingWang NeverStopDreamingWang commented Jul 31, 2025

Timezone Handling Fix for django-celery-beat (Issue #924)

Description

This PR resolves timezone compatibility issues when DJANGO_CELERY_BEAT_TZ_AWARE=False by:

  1. Ensuring consistent datetime comparisons in is_due()
  2. Properly handling all combinations of:
    • DJANGO_CELERY_BEAT_TZ_AWARE
    • USE_TZ

Fixes #924

Technical Changes

1. Core Modifications

File Changes
schedulers.py - Rewrote _default_now() to handle timezone awareness logic hierarchy
- Replaced maybe_make_aware with explicit make_aware conversion

📌 Core Changes

File Changes
schedulers.py - Rewrote _default_now() to handle timezone awareness logic hierarchy
- Replaced maybe_make_aware with explicit make_aware conversion

Copilot

This comment was marked as outdated.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

the tests are failing, so the change proposed appears to be not right. what do you think?

@auvipy auvipy requested a review from Copilot July 31, 2025 15:03
Copilot

This comment was marked as outdated.

@NeverStopDreamingWang
Copy link
Author

NeverStopDreamingWang commented Jul 31, 2025 via email

@NeverStopDreamingWang
Copy link
Author

NeverStopDreamingWang commented Aug 1, 2025

Fix: The issue where the local time obtained does not match the CELERY_TIMEZONE.

The previous fix version, DJANGO_CELERY_BEAT_TZ_AWARE = False, required that the local time should be consistent with the CELERY_TIMEZONE timezone.

DJANGO_CELERY_BEAT_TZ_AWARE = False At this point, the local time zone of the system is obtained, and then it is converted to the timezone of self.app.timezone using astimezone to ensure consistency with the Celery timezone.

Copy link

codecov bot commented Aug 1, 2025

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.30%. Comparing base (f1f239c) to head (343d368).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
django_celery_beat/schedulers.py 80.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #926      +/-   ##
==========================================
+ Coverage   88.19%   88.30%   +0.11%     
==========================================
  Files          32       32              
  Lines        1008     1009       +1     
  Branches      105      105              
==========================================
+ Hits          889      891       +2     
  Misses        101      101              
+ Partials       18       17       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@auvipy auvipy requested a review from Copilot August 1, 2025 04:49
Copilot

This comment was marked as outdated.

@auvipy auvipy requested a review from Copilot August 4, 2025 09:51
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes timezone handling issues when DJANGO_CELERY_BEAT_TZ_AWARE=False by improving datetime comparison logic and ensuring consistent timezone behavior across different configuration combinations.

  • Rewrote _default_now() method to properly handle timezone awareness based on both DJANGO_CELERY_BEAT_TZ_AWARE and USE_TZ settings
  • Replaced maybe_make_aware with explicit timezone handling logic in is_due() method
  • Updated test cases to use more realistic datetime generation that matches the new implementation

Reviewed Changes

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

File Description
django_celery_beat/schedulers.py Core timezone handling logic improvements in _default_now() and is_due() methods
t/unit/test_schedulers.py Updated test datetime generation to align with new timezone-aware implementation

Comment on lines 160 to 161
if (getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True) and
getattr(settings, 'USE_TZ', True)):
Copy link
Preview

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

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

[nitpick] The condition logic is unclear. Consider extracting the timezone awareness check into a separate variable or method to improve readability and make the intent more explicit.

Suggested change
if (getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True) and
getattr(settings, 'USE_TZ', True)):
tz_aware = (
getattr(settings, 'DJANGO_CELERY_BEAT_TZ_AWARE', True) and
getattr(settings, 'USE_TZ', True)
)
if tz_aware:

Copilot uses AI. Check for mistakes.

getattr(settings, 'USE_TZ', True)):
return now

# A naive datetime representing local time in the app's timezone
Copy link
Preview

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment should be more specific about when this path is taken. Consider adding details about the configuration conditions that lead to returning a naive datetime.

Suggested change
# A naive datetime representing local time in the app's timezone
# Return a naive datetime representing local time in the app's timezone.
# This path is taken when either DJANGO_CELERY_BEAT_TZ_AWARE or USE_TZ
# is set to False in Django settings.

Copilot uses AI. Check for mistakes.

@auvipy
Copy link
Member

auvipy commented Aug 5, 2025

I will need more eyes and bit more time before reaching to a consensus for this

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.

DJANGO_CELERY_BEAT_TZ_AWARE = False Time zone usage issue
2 participants