From 8ae34dd128ef8c5a79d4632e29fa82162bc9a787 Mon Sep 17 00:00:00 2001 From: Thomas Berdy Date: Mon, 4 Dec 2023 07:34:23 +0100 Subject: [PATCH] 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 <60190294+Andrew-Chen-Wang@users.noreply.github.com> --- CHANGELOG.md | 10 ++++++++++ rest_framework_simplejwt/utils.py | 12 ++++++++++-- setup.py | 1 + tests/test_utils.py | 8 +++----- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3eea04cde..14722c445 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/rest_framework_simplejwt/utils.py b/rest_framework_simplejwt/utils.py index 4490fa23c..cfe5ae189 100644 --- a/rest_framework_simplejwt/utils.py +++ b/rest_framework_simplejwt/utils.py @@ -23,7 +23,11 @@ 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: @@ -31,7 +35,11 @@ def datetime_to_epoch(dt: datetime) -> int: 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: diff --git a/setup.py b/setup.py index 60327eeef..e6397fc9b 100755 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ extras_require = { "test": [ "cryptography", + "freezegun", "pytest-cov", "pytest-django", "pytest-xdist", diff --git a/tests/test_utils.py b/tests/test_utils.py index 79afdbbf7..17eca26df 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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, @@ -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(