Skip to content

Commit

Permalink
Merge pull request #475 from Koswu/master
Browse files Browse the repository at this point in the history
make sure fromtimestamp always return FakeDateTime
  • Loading branch information
bblommers authored Mar 18, 2024
2 parents 12e54da + 6cba8b6 commit c65f4db
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 @@ -374,10 +374,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 = 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 c65f4db

Please sign in to comment.