Skip to content

Commit 8ae34dd

Browse files
Remove usages of deprecated datetime.utcnow() and datetime.utcfromtimestamp() (#765)
* Remove usages of deprecated datetime.utcnow() and datetime.utcfromtimestamp() * Update CHANGELOG.md --------- Co-authored-by: Andrew Chen Wang <[email protected]>
1 parent d810271 commit 8ae34dd

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 5.3.1
2+
3+
## What's Changed
4+
* Remove EOL Python, Django and DRF version support by @KOliver94 in [#754](https://github.com/jazzband/djangorestframework-simplejwt/pull/754)
5+
* Declare support for type checking (closes #664) by @PedroPerpetua in [#760](https://github.com/jazzband/djangorestframework-simplejwt/pull/760)
6+
* Remove usages of deprecated datetime.utcnow() and datetime.utcfromtimestamp() in [#765](https://github.com/jazzband/djangorestframework-simplejwt/pull/765)
7+
8+
#### Translation Updates:
9+
* Update Korean translations by @TGoddessana in https://github.com/jazzband/djangorestframework-simplejwt/pull/753
10+
111
## 5.3.0
212

313
#### Notable Changes:

rest_framework_simplejwt/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@ def make_utc(dt: datetime) -> datetime:
2323

2424

2525
def aware_utcnow() -> datetime:
26-
return make_utc(datetime.utcnow())
26+
dt = datetime.now(tz=timezone.utc)
27+
if not settings.USE_TZ:
28+
dt = dt.replace(tzinfo=None)
29+
30+
return dt
2731

2832

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

3236

3337
def datetime_from_epoch(ts: float) -> datetime:
34-
return make_utc(datetime.utcfromtimestamp(ts))
38+
dt = datetime.fromtimestamp(ts, tz=timezone.utc)
39+
if not settings.USE_TZ:
40+
dt = dt.replace(tzinfo=None)
41+
42+
return dt
3543

3644

3745
def format_lazy(s: str, *args, **kwargs) -> str:

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
extras_require = {
77
"test": [
88
"cryptography",
9+
"freezegun",
910
"pytest-cov",
1011
"pytest-django",
1112
"pytest-xdist",

tests/test_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from datetime import datetime, timedelta
2-
from unittest.mock import patch
32

43
from django.test import TestCase
54
from django.utils import timezone
5+
from freezegun import freeze_time
66

77
from rest_framework_simplejwt.utils import (
88
aware_utcnow,
@@ -34,11 +34,9 @@ def test_it_should_return_the_correct_values(self):
3434

3535
class TestAwareUtcnow(TestCase):
3636
def test_it_should_return_the_correct_value(self):
37-
now = datetime.utcnow()
38-
39-
with patch("rest_framework_simplejwt.utils.datetime") as fake_datetime:
40-
fake_datetime.utcnow.return_value = now
37+
now = datetime.now(tz=timezone.utc).replace(tzinfo=None)
4138

39+
with freeze_time(now):
4240
# Should return aware utcnow if USE_TZ == True
4341
with self.settings(USE_TZ=True):
4442
self.assertEqual(

0 commit comments

Comments
 (0)