From 5c5f1b1c0ee9a0af6c2f98a8307d6ecb9ebfdd40 Mon Sep 17 00:00:00 2001 From: Matthew Kenigsberg Date: Fri, 23 Jul 2021 17:35:07 -0400 Subject: [PATCH] Fix now(tz) now(tz) should return the same time utcnow returns adjusted by whatever offset is contained by tz. Currently, the offset to freeze_time is also added, which is removed by this change The current unit test is wrong because the UTC time is 2:00:00, so GMT5 should be 7:00:00, not 3:00:00 Closes https://github.com/spulec/freezegun/issues/405 --- freezegun/api.py | 2 +- tests/test_operations.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/freezegun/api.py b/freezegun/api.py index 81d4da17..bfa44378 100644 --- a/freezegun/api.py +++ b/freezegun/api.py @@ -385,7 +385,7 @@ def timestamp(self): def now(cls, tz=None): now = cls._time_to_freeze() or real_datetime.now() if tz: - result = tz.fromutc(now.replace(tzinfo=tz)) + cls._tz_offset() + result = tz.fromutc(now.replace(tzinfo=tz)) else: result = now + cls._tz_offset() return datetime_to_fakedatetime(result) diff --git a/tests/test_operations.py b/tests/test_operations.py index 303a1dc8..6f7b9a77 100644 --- a/tests/test_operations.py +++ b/tests/test_operations.py @@ -66,7 +66,7 @@ def test_datetime_timezone_real(): @freeze_time("2012-01-14 2:00:00", tz_offset=-4) def test_datetime_timezone_real_with_offset(): now = datetime.datetime.now(tz=GMT5()) - assert now == datetime.datetime(2012, 1, 14, 3, tzinfo=GMT5()) + assert now == datetime.datetime(2012, 1, 14, 7, tzinfo=GMT5()) assert now.utcoffset() == timedelta(0, 60 * 60 * 5)