Skip to content
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

Properly convert datetime from backend. Fix: #323, Fix: #330 #332

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import logging
import math
import pytz

from multiprocessing.util import Finalize

Expand Down Expand Up @@ -130,6 +131,21 @@ def is_due(self):
self.model.save()
return schedules.schedstate(False, None) # Don't recheck

# When Django settings USE_TZ is False and Django settings TIME_ZONE is set
# value of TIME_ZINE is the time zone in which Django will store all datetimes.
# Because of that if datetime is naive we should use it as source timezone
# celery.utils.time.maybe_make_aware - always convert naive datetime to UTC
# which may be wrong in Django sicase.
django_timezone = getattr(settings, 'TIME_ZONE', None)
Copy link
Member

Choose a reason for hiding this comment

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

this will need a proper test case

django_use_tz = getattr(settings, 'USE_TZ', None)
if (not django_use_tz
and django_timezone
and self.last_run_at.tzinfo is None
and self.app.timezone):
source_timezone = pytz.timezone(django_timezone)
last_run_at_django_tz = source_timezone.localize(self.last_run_at)
self.last_run_at = last_run_at_django_tz.astimezone(self.app.timezone)

# CAUTION: make_aware assumes settings.TIME_ZONE for naive datetimes,
# while maybe_make_aware assumes utc for naive datetimes
tz = self.app.timezone
Expand Down