Skip to content

Commit

Permalink
Refactor code to properly use datetime.timezone.utc rather than the d…
Browse files Browse the repository at this point in the history
…eprecated django.utils.timezone.utc
  • Loading branch information
chander committed Jan 9, 2024
1 parent a9bd88d commit c8d9859
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion docs/cookbook.rst
Expand Up @@ -91,7 +91,8 @@ modifying ``get_object_list``:

.. testcode::

from django.utils import timezone
from datetime import timezone

from myapp.models import MyModel


Expand Down
23 changes: 12 additions & 11 deletions tastypie/utils/timezone.py
@@ -1,19 +1,20 @@
import datetime
from django.conf import settings
from django.utils import timezone
from django.utils import timezone as dj_timezone
from datetime import timezone


def make_aware(value):
if settings.USE_TZ and timezone.is_naive(value):
default_tz = timezone.get_default_timezone()
value = timezone.make_aware(value, default_tz)
if settings.USE_TZ and dj_timezone.is_naive(value):
default_tz = dj_timezone.get_default_timezone()
value = dj_timezone.make_aware(value, default_tz)
return value


def make_naive(value):
if settings.USE_TZ and timezone.is_aware(value):
default_tz = timezone.get_default_timezone()
value = timezone.make_naive(value, default_tz)
if settings.USE_TZ and dj_timezone.is_aware(value):
default_tz = dj_timezone.get_default_timezone()
value = dj_timezone.make_naive(value, default_tz)
return value


Expand All @@ -22,15 +23,15 @@ def make_naive_utc(value):
Translate a datetime to UTC, then strip TZ info; useful as a last step before creating the
Retry-After header.
"""
utc_value = timezone.localtime(value, timezone.utc)
return timezone.make_naive(utc_value)
utc_value = dj_timezone.localtime(value, timezone.utc)
return dj_timezone.make_naive(utc_value)


def now():
d = timezone.now()
d = dj_timezone.now()

if d.tzinfo:
return timezone.localtime(d)
return dj_timezone.localtime(d)

return d

Expand Down
3 changes: 1 addition & 2 deletions tests/core/tests/resources.py
Expand Up @@ -24,8 +24,7 @@
from django.http import HttpRequest, QueryDict, Http404
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import timezone

from datetime import timezone
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import Authorization
from tastypie.bundle import Bundle
Expand Down

0 comments on commit c8d9859

Please sign in to comment.