Skip to content

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.29%. Comparing base (f1f239c) to head (ddb88fb).
⚠️ Report is 13 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.29%   +0.09%     
==========================================
  Files          32       32              
  Lines        1008     1008              
  Branches      105      105              
==========================================
+ Hits          889      890       +1     
  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

@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

@auvipy auvipy requested a review from Copilot October 4, 2025 07:42
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

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


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +149 to +155
# Handle both naive and timezone-aware last_run_at properly
if is_naive(self.last_run_at):
# Naive datetime - make it aware using app timezone
last_run_at_in_tz = make_aware(self.last_run_at, self.app.timezone)
else:
# Already timezone-aware - convert to app timezone if needed
last_run_at_in_tz = self.last_run_at.astimezone(self.app.timezone)
Copy link

Copilot AI Oct 4, 2025

Choose a reason for hiding this comment

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

The comment on line 151 is misleading. The make_aware function assumes the naive datetime is in the system's default timezone (settings.TIME_ZONE), not the app timezone. Consider updating the comment to clarify this assumption or use a different approach if the naive datetime should be interpreted as being in the app timezone.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

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

@NeverStopDreamingWang can you please cross check the comment? also if possible increase unit test coverage?

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