diff --git a/django_celery_beat/schedulers.py b/django_celery_beat/schedulers.py index 2c1b5773..7789da0a 100644 --- a/django_celery_beat/schedulers.py +++ b/django_celery_beat/schedulers.py @@ -4,6 +4,7 @@ import math from multiprocessing.util import Finalize +import pytz from celery import current_app, schedules from celery.beat import ScheduleEntry, Scheduler from celery.utils.log import get_logger @@ -129,6 +130,21 @@ def is_due(self): # Don't recheck return schedules.schedstate(False, NEVER_CHECK_TIMEOUT) + # 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) + 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