Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic fold support #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ def datetime_to_fakedatetime(datetime):
datetime.minute,
datetime.second,
datetime.microsecond,
datetime.tzinfo)
datetime.tzinfo,
fold=datetime.fold)


def date_to_fakedate(date):
Expand Down Expand Up @@ -385,9 +386,14 @@ 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()
result = now

# Add the _tz_offset only if it's non-zero to preserve fold
if cls._tz_offset():
result += cls._tz_offset()

return datetime_to_fakedatetime(result)

def date(self):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from freezegun import freeze_time
from dateutil import tz
from dateutil.relativedelta import relativedelta
from datetime import timedelta, tzinfo
from tests import utils
Expand Down Expand Up @@ -63,13 +64,32 @@ def test_datetime_timezone_real():
assert now.utcoffset() == timedelta(0, 60 * 60 * 5)


@freeze_time("2021-10-31 02:30:00+01:00") # This will have fold set to 1
def test_datetime_timezone_real_fold():
vienna = tz.gettz("Europe/Vienna")
now = datetime.datetime.now(tz=vienna)
assert now == datetime.datetime(2021, 10, 31, 2, 30, tzinfo=vienna)
assert now.utcoffset() == timedelta(0, 60 * 60 * 1)
assert now.fold == 1


@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.utcoffset() == timedelta(0, 60 * 60 * 5)


@freeze_time("2021-10-31 06:30:00+01:00", tz_offset=-4)
def test_datetime_timezone_real_with_offset_fold():
vienna = tz.gettz("Europe/Vienna")
now = datetime.datetime.now(tz=vienna)
assert now == datetime.datetime(2021, 10, 31, 2, 30, tzinfo=vienna)
# Because of adding the tz_offset timedelta, fold information is lost
assert now.utcoffset() == timedelta(0, 60 * 60 * 2)
assert now.fold == 0


@freeze_time("2012-01-14 00:00:00")
def test_astimezone():
now = datetime.datetime.now(tz=GMT5())
Expand Down