Skip to content

Commit

Permalink
make sure fromtimestamp always return FakeDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
Koswu committed Oct 27, 2022
1 parent 9d5ea73 commit d6146ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,11 @@ def astimezone(self, tz=None):
@classmethod
def fromtimestamp(cls, t, tz=None):
if tz is None:
return real_datetime.fromtimestamp(
t, tz=dateutil.tz.tzoffset("freezegun", cls._tz_offset())
).replace(tzinfo=None)
return datetime_to_fakedatetime(real_datetime.fromtimestamp(t, tz))
tz = dateutil.tz.tzoffset("freezegun", cls._tz_offset())
result = real_datetime.fromtimestamp(t, tz=tz).replace(tzinfo=None)
else:
result = datetime_to_fakedatetime(real_datetime.fromtimestamp(t, tz))
return datetime_to_fakedatetime(result)

def timestamp(self):
if self.tzinfo is None:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ def test_tz_offset():
freezer.stop()


def test_timestamp_tz_offset():
freezer = freeze_time(datetime.datetime.fromtimestamp(1), tz_offset=-1)
freezer.start()
t = datetime.datetime.now().timestamp()

assert datetime.datetime.fromtimestamp(t).timestamp() == t
freezer.stop()


def test_timedelta_tz_offset():
freezer = freeze_time("2012-01-14 03:21:34",
tz_offset=-datetime.timedelta(hours=3, minutes=30))
Expand Down

0 comments on commit d6146ad

Please sign in to comment.