Skip to content

Commit

Permalink
Remove usages of deprecated datetime.utcnow() and datetime.utcfromtim…
Browse files Browse the repository at this point in the history
…estamp() (#765)

* Remove usages of deprecated datetime.utcnow() and datetime.utcfromtimestamp()

* Update CHANGELOG.md

---------

Co-authored-by: Andrew Chen Wang <60190294+Andrew-Chen-Wang@users.noreply.github.com>
  • Loading branch information
kozlek and Andrew-Chen-Wang committed Dec 4, 2023
1 parent d810271 commit 8ae34dd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 5.3.1

## What's Changed
* Remove EOL Python, Django and DRF version support by @KOliver94 in [#754](https://github.com/jazzband/djangorestframework-simplejwt/pull/754)
* Declare support for type checking (closes #664) by @PedroPerpetua in [#760](https://github.com/jazzband/djangorestframework-simplejwt/pull/760)
* Remove usages of deprecated datetime.utcnow() and datetime.utcfromtimestamp() in [#765](https://github.com/jazzband/djangorestframework-simplejwt/pull/765)

#### Translation Updates:
* Update Korean translations by @TGoddessana in https://github.com/jazzband/djangorestframework-simplejwt/pull/753

## 5.3.0

#### Notable Changes:
Expand Down
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

0 comments on commit 8ae34dd

Please sign in to comment.