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

Fix Issue #604: Celery Beat Crashing at the End of Daylight Savings #605

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 12 additions & 1 deletion django_celery_beat/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Utilities."""
# -- XXX This module must not use translation as that causes
# -- a recursive loader import!
import time
from datetime import timezone as datetime_timezone

from django.conf import settings
Expand All @@ -26,7 +27,17 @@ def make_aware(value):
else:
# naive datetimes are assumed to be in local timezone.
if timezone.is_naive(value):
value = timezone.make_aware(value, timezone.get_default_timezone())
tm_isdst = time.localtime().tm_isdst

# tm_isdst may return -1 if it cannot be determined
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure about this part, can any other way we can check it?

Copy link
Author

Choose a reason for hiding this comment

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

I referenced these posts 1 (top answer only) and 2. There are other suggested solutions online. I can summarize the problems with these solutions.

Use of localize

Source 1
Source 2
Source 3

The root cause was inside localize. Using the localize to fix the problem with localize sounds counterintuitive. For instance, link 1 mentions the following in the final solution:

Once you've **localized** a datetime in fact you can simply check it's .dst() method to see if it's zero.

time.daylight

Source

This solution might work, but the underlying code is just to do a similar check to what I do here. This code is cleaner, and I can change it if you would like.

Calculations

Source

One way is to calculate the timezone based on the current time and a previous time, but I think that this solution is long and too ad-hoc when we can achieve this with a few lines of code.

Conclusion

I think that time.daylight or time.localtime+tm_isdst is the best way to go for this. If you believe in the other solutions, please let me know.

Copy link
Member

Choose a reason for hiding this comment

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

I have to check to give you proper answer

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good. Let me know when you've checked.

if tm_isdst == -1:
is_dst = None
else:
is_dst = bool(tm_isdst)

value = timezone.make_aware(value,
timezone.get_default_timezone(),
is_dst=is_dst)
return value


Expand Down
186 changes: 186 additions & 0 deletions t/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import time
from datetime import datetime
from unittest import mock

from django.test import TestCase
from django.utils import timezone

from django_celery_beat import utils


class UtilsTest(TestCase):

@mock.patch('django_celery_beat.utils.timezone.localtime')
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone')
@mock.patch('django_celery_beat.utils.timezone.make_aware')
@mock.patch('django_celery_beat.utils.time.localtime')
@mock.patch('django_celery_beat.utils.timezone.is_naive')
@mock.patch('django_celery_beat.utils.getattr')
def test_make_aware_use_tz_naive(self,
mock_getattr,
mock_is_naive,
mock_localtime_1,
mock_make_aware,
mock_get_default_timezone,
mock_localtime_2):
dt = datetime(2022, 11, 6, 1, 15, 0)
mock_getattr.return_value = True
mock_is_naive.return_value = True
mock_get_default_timezone.return_value = "America/Los_Angeles"
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0]
mock_localtime_2.return_value = time.struct_time(test_time)
mock_make_aware.return_value = dt

self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value)

mock_localtime_1.assert_not_called()
mock_make_aware.assert_called_with(dt, timezone.utc)
mock_get_default_timezone.assert_called()
mock_localtime_2.assert_called_with(dt,
"America/Los_Angeles")

@mock.patch('django_celery_beat.utils.timezone.localtime')
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone')
@mock.patch('django_celery_beat.utils.timezone.make_aware')
@mock.patch('django_celery_beat.utils.time.localtime')
@mock.patch('django_celery_beat.utils.timezone.is_naive')
@mock.patch('django_celery_beat.utils.getattr')
def test_make_aware_use_tz_not_naive(self,
mock_getattr,
mock_is_naive,
mock_localtime_1,
mock_make_aware,
mock_get_default_timezone,
mock_localtime_2):
dt = datetime(2022, 11, 6, 1, 15, 0)
mock_getattr.return_value = True
mock_is_naive.return_value = False
mock_get_default_timezone.return_value = "America/Los_Angeles"
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0]
mock_localtime_2.return_value = time.struct_time(test_time)
mock_make_aware.return_value = dt

self.assertEqual(utils.make_aware(dt), mock_localtime_2.return_value)

mock_localtime_1.assert_not_called()
mock_make_aware.assert_not_called()
mock_get_default_timezone.assert_called()
mock_localtime_2.assert_called_with(dt,
"America/Los_Angeles")

@mock.patch('django_celery_beat.utils.timezone.localtime')
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone')
@mock.patch('django_celery_beat.utils.timezone.make_aware')
@mock.patch('django_celery_beat.utils.time.localtime')
@mock.patch('django_celery_beat.utils.timezone.is_naive')
@mock.patch('django_celery_beat.utils.getattr')
def test_make_aware_not_use_tz_naive_dst(self,
mock_getattr,
mock_is_naive,
mock_localtime_1,
mock_make_aware,
mock_get_default_timezone,
mock_localtime_2):
dt = datetime(2022, 11, 6, 1, 15, 0)
mock_getattr.return_value = False
mock_is_naive.return_value = True
mock_get_default_timezone.return_value = "America/Los_Angeles"
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 1]
mock_localtime_1.return_value = time.struct_time(test_time)
mock_make_aware.return_value = dt

self.assertEqual(utils.make_aware(dt), dt)

mock_localtime_1.assert_called_with()
mock_make_aware.assert_called_with(dt,
"America/Los_Angeles",
is_dst=True)
mock_get_default_timezone.assert_called()
mock_localtime_2.assert_not_called()

@mock.patch('django_celery_beat.utils.timezone.localtime')
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone')
@mock.patch('django_celery_beat.utils.timezone.make_aware')
@mock.patch('django_celery_beat.utils.time.localtime')
@mock.patch('django_celery_beat.utils.timezone.is_naive')
@mock.patch('django_celery_beat.utils.getattr')
def test_make_aware_not_use_tz_naive_not_dst(self,
mock_getattr,
mock_is_naive,
mock_localtime_1,
mock_make_aware,
mock_get_default_timezone,
mock_localtime_2):
dt = datetime(2022, 11, 6, 1, 15, 0)
mock_getattr.return_value = False
mock_is_naive.return_value = True
mock_get_default_timezone.return_value = "America/Los_Angeles"
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0]
mock_localtime_1.return_value = time.struct_time(test_time)
mock_make_aware.return_value = dt

self.assertEqual(utils.make_aware(dt), dt)

mock_localtime_1.assert_called_with()
mock_make_aware.assert_called_with(dt,
"America/Los_Angeles",
is_dst=False)
mock_get_default_timezone.assert_called()
mock_localtime_2.assert_not_called()

@mock.patch('django_celery_beat.utils.timezone.localtime')
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone')
@mock.patch('django_celery_beat.utils.timezone.make_aware')
@mock.patch('django_celery_beat.utils.time.localtime')
@mock.patch('django_celery_beat.utils.timezone.is_naive')
@mock.patch('django_celery_beat.utils.getattr')
def test_make_aware_not_use_tz_naive_neg_dst(self,
mock_getattr,
mock_is_naive,
mock_localtime_1,
mock_make_aware,
mock_get_default_timezone,
mock_localtime_2):
dt = datetime(2022, 11, 6, 1, 15, 0)
mock_getattr.return_value = False
mock_is_naive.return_value = True
mock_get_default_timezone.return_value = "America/Los_Angeles"
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, -1]
mock_localtime_1.return_value = time.struct_time(test_time)
mock_make_aware.return_value = dt

self.assertEqual(utils.make_aware(dt), dt)

mock_localtime_1.assert_called_with()
mock_make_aware.assert_called_with(dt,
"America/Los_Angeles",
is_dst=None)
mock_get_default_timezone.assert_called()

@mock.patch('django_celery_beat.utils.timezone.localtime')
@mock.patch('django_celery_beat.utils.timezone.get_default_timezone')
@mock.patch('django_celery_beat.utils.timezone.make_aware')
@mock.patch('django_celery_beat.utils.time.localtime')
@mock.patch('django_celery_beat.utils.timezone.is_naive')
@mock.patch('django_celery_beat.utils.getattr')
def test_make_aware_not_use_tz_not_naive_dst(self,
mock_getattr,
mock_is_naive,
mock_localtime_1,
mock_make_aware,
mock_get_default_timezone,
mock_localtime_2):
dt = datetime(2022, 11, 6, 1, 15, 0)
mock_getattr.return_value = False
mock_is_naive.return_value = False
mock_get_default_timezone.return_value = "America/Los_Angeles"
test_time = [2022, 11, 6, 1, 15, 0, 0, 310, 0]
mock_localtime_1.return_value = time.struct_time(test_time)
mock_make_aware.return_value = dt

self.assertEqual(utils.make_aware(dt), dt)

mock_localtime_1.assert_not_called()
mock_make_aware.assert_not_called()
mock_get_default_timezone.assert_not_called()
mock_localtime_2.assert_not_called()
Loading