From 95ad44bdd4e5f97b632a75987b2bd81238dc9fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3mas=20=C3=81rni=20J=C3=B3nasson?= Date: Wed, 16 Oct 2019 08:59:38 +0000 Subject: [PATCH 1/2] Default to own .fold when calling .replace() --- pendulum/datetime.py | 2 + tests/datetime/test_fluent_setters.py | 33 ++++++++++++--- tests/datetime/test_replace.py | 59 +++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 tests/datetime/test_replace.py diff --git a/pendulum/datetime.py b/pendulum/datetime.py index d79257ca..feb140fe 100644 --- a/pendulum/datetime.py +++ b/pendulum/datetime.py @@ -1494,6 +1494,8 @@ def replace( microsecond = self.microsecond if tzinfo is True: tzinfo = self.tzinfo + if fold is None: + fold = self.fold transition_rule = pendulum.POST_TRANSITION if fold is not None: diff --git a/tests/datetime/test_fluent_setters.py b/tests/datetime/test_fluent_setters.py index 1b5d1576..eb2786b7 100644 --- a/tests/datetime/test_fluent_setters.py +++ b/tests/datetime/test_fluent_setters.py @@ -139,18 +139,41 @@ def test_fluid_at_with_transition(): assert 0 == new.second -def test_replace_tzinfo(): - d = pendulum.datetime(2016, 7, 2, 0, 41, 20) +def test_replace_tzinfo_dls_off(): + d = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DLS turning on new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) + assert_datetime(new, 2016, 3, 27, 0, 30) + assert not new.is_dst() + assert new.offset == 3600 + assert new.timezone_name == "Europe/Paris" + + +def test_replace_tzinfo_dls_transitioning_on(): + d = pendulum.datetime(2016, 3, 27, 1, 30) # In middle of turning on + new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) + + assert_datetime(new, 2016, 3, 27, 1, 30) + assert not new.is_dst() + assert new.offset == 3600 + assert new.timezone_name == "Europe/Paris" + + +def test_replace_tzinfo_dls_on(): + d = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DLS turning off + new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) + + assert_datetime(new, 2016, 10, 30, 0, 30) + assert new.is_dst() + assert new.offset == 7200 assert new.timezone_name == "Europe/Paris" -def test_replace_tzinfo_dst(): - d = pendulum.datetime(2013, 3, 31, 2, 30) +def test_replace_tzinfo_dls_transitioning_off(): + d = pendulum.datetime(2016, 10, 30, 1, 30) # In the middle of turning off new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) - assert_datetime(new, 2013, 3, 31, 3, 30) + assert_datetime(new, 2016, 10, 30, 1, 30) assert new.is_dst() assert new.offset == 7200 assert new.timezone_name == "Europe/Paris" diff --git a/tests/datetime/test_replace.py b/tests/datetime/test_replace.py new file mode 100644 index 00000000..4fedc569 --- /dev/null +++ b/tests/datetime/test_replace.py @@ -0,0 +1,59 @@ +import pendulum + +from ..conftest import assert_datetime + + +def test_replace_tzinfo_dls_off(): + utc = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DLS turning on + in_paris = utc.in_tz("Europe/Paris") + + assert_datetime(in_paris, 2016, 3, 27, 1, 30, 0) + + in_paris = in_paris.replace(second=1) + + assert_datetime(in_paris, 2016, 3, 27, 1, 30, 1) + assert not in_paris.is_dst() + assert in_paris.offset == 3600 + assert in_paris.timezone_name == "Europe/Paris" + + +def test_replace_tzinfo_dls_transitioning_on(): + utc = pendulum.datetime(2016, 3, 27, 1, 30) # In middle of turning on + in_paris = utc.in_tz("Europe/Paris") + + assert_datetime(in_paris, 2016, 3, 27, 3, 30, 0) + + in_paris = in_paris.replace(second=1) + + assert_datetime(in_paris, 2016, 3, 27, 3, 30, 1) + assert in_paris.is_dst() + assert in_paris.offset == 7200 + assert in_paris.timezone_name == "Europe/Paris" + + +def test_replace_tzinfo_dls_on(): + utc = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DLS turning off + in_paris = utc.in_tz("Europe/Paris") + + assert_datetime(in_paris, 2016, 10, 30, 2, 30, 0) + + in_paris = in_paris.replace(second=1) + + assert_datetime(in_paris, 2016, 10, 30, 2, 30, 1) + assert in_paris.is_dst() + assert in_paris.offset == 7200 + assert in_paris.timezone_name == "Europe/Paris" + + +def test_replace_tzinfo_dls_transitioning_off(): + utc = pendulum.datetime(2016, 10, 30, 1, 30) # In the middle of turning off + in_paris = utc.in_tz("Europe/Paris") + + assert_datetime(in_paris, 2016, 10, 30, 2, 30, 0) + + in_paris = in_paris.replace(second=1) + + assert_datetime(in_paris, 2016, 10, 30, 2, 30, 1) + assert not in_paris.is_dst() + assert in_paris.offset == 3600 + assert in_paris.timezone_name == "Europe/Paris" From d5903542e9d43693de22921f8318ad504402d5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3mas=20=C3=81rni=20J=C3=B3nasson?= Date: Thu, 9 Jul 2020 14:00:12 -0700 Subject: [PATCH 2/2] fixup: Rename DLS to DST --- tests/datetime/test_fluent_setters.py | 12 ++++++------ tests/datetime/test_replace.py | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/datetime/test_fluent_setters.py b/tests/datetime/test_fluent_setters.py index eb2786b7..13a313f8 100644 --- a/tests/datetime/test_fluent_setters.py +++ b/tests/datetime/test_fluent_setters.py @@ -139,8 +139,8 @@ def test_fluid_at_with_transition(): assert 0 == new.second -def test_replace_tzinfo_dls_off(): - d = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DLS turning on +def test_replace_tzinfo_dst_off(): + d = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DST turning on new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) assert_datetime(new, 2016, 3, 27, 0, 30) @@ -149,7 +149,7 @@ def test_replace_tzinfo_dls_off(): assert new.timezone_name == "Europe/Paris" -def test_replace_tzinfo_dls_transitioning_on(): +def test_replace_tzinfo_dst_transitioning_on(): d = pendulum.datetime(2016, 3, 27, 1, 30) # In middle of turning on new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) @@ -159,8 +159,8 @@ def test_replace_tzinfo_dls_transitioning_on(): assert new.timezone_name == "Europe/Paris" -def test_replace_tzinfo_dls_on(): - d = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DLS turning off +def test_replace_tzinfo_dst_on(): + d = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DST turning off new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) assert_datetime(new, 2016, 10, 30, 0, 30) @@ -169,7 +169,7 @@ def test_replace_tzinfo_dls_on(): assert new.timezone_name == "Europe/Paris" -def test_replace_tzinfo_dls_transitioning_off(): +def test_replace_tzinfo_dst_transitioning_off(): d = pendulum.datetime(2016, 10, 30, 1, 30) # In the middle of turning off new = d.replace(tzinfo=pendulum.timezone("Europe/Paris")) diff --git a/tests/datetime/test_replace.py b/tests/datetime/test_replace.py index 4fedc569..11466f43 100644 --- a/tests/datetime/test_replace.py +++ b/tests/datetime/test_replace.py @@ -3,8 +3,8 @@ from ..conftest import assert_datetime -def test_replace_tzinfo_dls_off(): - utc = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DLS turning on +def test_replace_tzinfo_dst_off(): + utc = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DST turning on in_paris = utc.in_tz("Europe/Paris") assert_datetime(in_paris, 2016, 3, 27, 1, 30, 0) @@ -17,7 +17,7 @@ def test_replace_tzinfo_dls_off(): assert in_paris.timezone_name == "Europe/Paris" -def test_replace_tzinfo_dls_transitioning_on(): +def test_replace_tzinfo_dst_transitioning_on(): utc = pendulum.datetime(2016, 3, 27, 1, 30) # In middle of turning on in_paris = utc.in_tz("Europe/Paris") @@ -31,8 +31,8 @@ def test_replace_tzinfo_dls_transitioning_on(): assert in_paris.timezone_name == "Europe/Paris" -def test_replace_tzinfo_dls_on(): - utc = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DLS turning off +def test_replace_tzinfo_dst_on(): + utc = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DST turning off in_paris = utc.in_tz("Europe/Paris") assert_datetime(in_paris, 2016, 10, 30, 2, 30, 0) @@ -45,7 +45,7 @@ def test_replace_tzinfo_dls_on(): assert in_paris.timezone_name == "Europe/Paris" -def test_replace_tzinfo_dls_transitioning_off(): +def test_replace_tzinfo_dst_transitioning_off(): utc = pendulum.datetime(2016, 10, 30, 1, 30) # In the middle of turning off in_paris = utc.in_tz("Europe/Paris")