mirror of
https://github.com/django-q2/django-q2.git
synced 2026-09-15 13:37:56 +08:00
Fix: allow both ZoneInfo and Pytz depending on django version (#55)
This commit is contained in:
@@ -6,7 +6,6 @@ import socket
|
||||
import traceback
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from pytz import timezone as pytz_timezone
|
||||
from multiprocessing import Event, Process, Value, current_process
|
||||
from time import sleep
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import os
|
||||
import pytz
|
||||
from datetime import datetime, timedelta
|
||||
from multiprocessing import Event, Value
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
import django
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError
|
||||
from django.test import override_settings
|
||||
@@ -24,6 +24,15 @@ from django_q.tests.testing_utilities.multiple_database_routers import (
|
||||
)
|
||||
from django_q.utils import add_months
|
||||
|
||||
if django.VERSION < (4, 0):
|
||||
# pytz is the default in django 3.2. Remove when no support for 3.2
|
||||
from pytz import timezone as ZoneInfo
|
||||
else:
|
||||
try:
|
||||
from zoneinfo import ZoneInfo
|
||||
except ImportError:
|
||||
from backports.zoneinfo import ZoneInfo
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def broker(monkeypatch) -> Broker:
|
||||
@@ -90,7 +99,7 @@ def test_scheduler_daylight_saving_time_daily(broker, monkeypatch):
|
||||
# 28th of March 2021 is the day when sunlight saving starts (at 2 am)
|
||||
|
||||
monkeypatch.setattr(Conf, "TIME_ZONE", "Europe/Amsterdam")
|
||||
tz = pytz.timezone('Europe/Amsterdam')
|
||||
tz = ZoneInfo('Europe/Amsterdam')
|
||||
broker.list_key = "scheduler_test:q"
|
||||
# Let's start a schedule at 1 am on the 27th of March. This is in AMS timezone.
|
||||
# So, 2021-03-27 00:00:00 when saved (due to TZ being Amsterdam and saved in UTC)
|
||||
|
||||
@@ -1,14 +1,24 @@
|
||||
from datetime import datetime
|
||||
import pytz
|
||||
import calendar
|
||||
import inspect
|
||||
from datetime import date
|
||||
|
||||
import django
|
||||
from django.utils import timezone
|
||||
from django.conf import settings
|
||||
|
||||
from django_q.conf import Conf
|
||||
|
||||
if django.VERSION < (4, 0):
|
||||
# pytz is the default in django 3.2. Remove when no support for 3.2
|
||||
from pytz import timezone as ZoneInfo
|
||||
else:
|
||||
try:
|
||||
from zoneinfo import ZoneInfo
|
||||
except ImportError:
|
||||
from backports.zoneinfo import ZoneInfo
|
||||
|
||||
|
||||
# credits: https://stackoverflow.com/a/4131114
|
||||
# Made them aware of timezone
|
||||
def add_months(d, months):
|
||||
@@ -50,7 +60,14 @@ def get_func_repr(func):
|
||||
def localtime(value=None) -> datetime:
|
||||
"""Override for timezone.localtime to deal with naive times and local times"""
|
||||
if settings.USE_TZ:
|
||||
return timezone.localtime(value=value, timezone=pytz.timezone(Conf.TIME_ZONE))
|
||||
if django.VERSION >= (4, 0) and settings.USE_DEPRECATED_PYTZ:
|
||||
import pytz
|
||||
|
||||
convert_to_tz = pytz.timezone(Conf.TIME_ZONE)
|
||||
else:
|
||||
convert_to_tz = ZoneInfo(Conf.TIME_ZONE)
|
||||
|
||||
return timezone.localtime(value=value, timezone=convert_to_tz)
|
||||
if value is None:
|
||||
return datetime.now()
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user