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

Remove usages of deprecated datetime.utcnow() and datetime.utcfromtimestamp() #765

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions rest_framework_simplejwt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ def make_utc(dt: datetime) -> datetime:


def aware_utcnow() -> datetime:
return make_utc(datetime.utcnow())
dt = datetime.now(tz=timezone.utc)
if not settings.USE_TZ:
dt = dt.replace(tzinfo=None)

return dt


def datetime_to_epoch(dt: datetime) -> int:
return timegm(dt.utctimetuple())


def datetime_from_epoch(ts: float) -> datetime:
return make_utc(datetime.utcfromtimestamp(ts))
dt = datetime.fromtimestamp(ts, tz=timezone.utc)
if not settings.USE_TZ:
dt = dt.replace(tzinfo=None)

return dt


def format_lazy(s: str, *args, **kwargs) -> str:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
extras_require = {
"test": [
"cryptography",
"freezegun",
"pytest-cov",
"pytest-django",
"pytest-xdist",
Expand Down
8 changes: 3 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from datetime import datetime, timedelta
from unittest.mock import patch

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

from rest_framework_simplejwt.utils import (
aware_utcnow,
Expand Down Expand Up @@ -34,11 +34,9 @@ def test_it_should_return_the_correct_values(self):

class TestAwareUtcnow(TestCase):
def test_it_should_return_the_correct_value(self):
now = datetime.utcnow()

with patch("rest_framework_simplejwt.utils.datetime") as fake_datetime:
fake_datetime.utcnow.return_value = now
now = datetime.now(tz=timezone.utc).replace(tzinfo=None)

with freeze_time(now):
# Should return aware utcnow if USE_TZ == True
with self.settings(USE_TZ=True):
self.assertEqual(
Expand Down
Loading