Skip to content

Commit 36e8726

Browse files
authored
Default to own .fold when calling .replace() (#414)
* Default to own .fold when calling .replace() * fixup: Rename DLS to DST
1 parent 043ea83 commit 36e8726

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

pendulum/datetime.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,8 @@ def replace(
14941494
microsecond = self.microsecond
14951495
if tzinfo is True:
14961496
tzinfo = self.tzinfo
1497+
if fold is None:
1498+
fold = self.fold
14971499

14981500
transition_rule = pendulum.POST_TRANSITION
14991501
if fold is not None:

tests/datetime/test_fluent_setters.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,41 @@ def test_fluid_at_with_transition():
139139
assert 0 == new.second
140140

141141

142-
def test_replace_tzinfo():
143-
d = pendulum.datetime(2016, 7, 2, 0, 41, 20)
142+
def test_replace_tzinfo_dst_off():
143+
d = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DST turning on
144144
new = d.replace(tzinfo=pendulum.timezone("Europe/Paris"))
145145

146+
assert_datetime(new, 2016, 3, 27, 0, 30)
147+
assert not new.is_dst()
148+
assert new.offset == 3600
149+
assert new.timezone_name == "Europe/Paris"
150+
151+
152+
def test_replace_tzinfo_dst_transitioning_on():
153+
d = pendulum.datetime(2016, 3, 27, 1, 30) # In middle of turning on
154+
new = d.replace(tzinfo=pendulum.timezone("Europe/Paris"))
155+
156+
assert_datetime(new, 2016, 3, 27, 1, 30)
157+
assert not new.is_dst()
158+
assert new.offset == 3600
159+
assert new.timezone_name == "Europe/Paris"
160+
161+
162+
def test_replace_tzinfo_dst_on():
163+
d = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DST turning off
164+
new = d.replace(tzinfo=pendulum.timezone("Europe/Paris"))
165+
166+
assert_datetime(new, 2016, 10, 30, 0, 30)
167+
assert new.is_dst()
168+
assert new.offset == 7200
146169
assert new.timezone_name == "Europe/Paris"
147170

148171

149-
def test_replace_tzinfo_dst():
150-
d = pendulum.datetime(2013, 3, 31, 2, 30)
172+
def test_replace_tzinfo_dst_transitioning_off():
173+
d = pendulum.datetime(2016, 10, 30, 1, 30) # In the middle of turning off
151174
new = d.replace(tzinfo=pendulum.timezone("Europe/Paris"))
152175

153-
assert_datetime(new, 2013, 3, 31, 3, 30)
176+
assert_datetime(new, 2016, 10, 30, 1, 30)
154177
assert new.is_dst()
155178
assert new.offset == 7200
156179
assert new.timezone_name == "Europe/Paris"

tests/datetime/test_replace.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import pendulum
2+
3+
from ..conftest import assert_datetime
4+
5+
6+
def test_replace_tzinfo_dst_off():
7+
utc = pendulum.datetime(2016, 3, 27, 0, 30) # 30 min before DST turning on
8+
in_paris = utc.in_tz("Europe/Paris")
9+
10+
assert_datetime(in_paris, 2016, 3, 27, 1, 30, 0)
11+
12+
in_paris = in_paris.replace(second=1)
13+
14+
assert_datetime(in_paris, 2016, 3, 27, 1, 30, 1)
15+
assert not in_paris.is_dst()
16+
assert in_paris.offset == 3600
17+
assert in_paris.timezone_name == "Europe/Paris"
18+
19+
20+
def test_replace_tzinfo_dst_transitioning_on():
21+
utc = pendulum.datetime(2016, 3, 27, 1, 30) # In middle of turning on
22+
in_paris = utc.in_tz("Europe/Paris")
23+
24+
assert_datetime(in_paris, 2016, 3, 27, 3, 30, 0)
25+
26+
in_paris = in_paris.replace(second=1)
27+
28+
assert_datetime(in_paris, 2016, 3, 27, 3, 30, 1)
29+
assert in_paris.is_dst()
30+
assert in_paris.offset == 7200
31+
assert in_paris.timezone_name == "Europe/Paris"
32+
33+
34+
def test_replace_tzinfo_dst_on():
35+
utc = pendulum.datetime(2016, 10, 30, 0, 30) # 30 min before DST turning off
36+
in_paris = utc.in_tz("Europe/Paris")
37+
38+
assert_datetime(in_paris, 2016, 10, 30, 2, 30, 0)
39+
40+
in_paris = in_paris.replace(second=1)
41+
42+
assert_datetime(in_paris, 2016, 10, 30, 2, 30, 1)
43+
assert in_paris.is_dst()
44+
assert in_paris.offset == 7200
45+
assert in_paris.timezone_name == "Europe/Paris"
46+
47+
48+
def test_replace_tzinfo_dst_transitioning_off():
49+
utc = pendulum.datetime(2016, 10, 30, 1, 30) # In the middle of turning off
50+
in_paris = utc.in_tz("Europe/Paris")
51+
52+
assert_datetime(in_paris, 2016, 10, 30, 2, 30, 0)
53+
54+
in_paris = in_paris.replace(second=1)
55+
56+
assert_datetime(in_paris, 2016, 10, 30, 2, 30, 1)
57+
assert not in_paris.is_dst()
58+
assert in_paris.offset == 3600
59+
assert in_paris.timezone_name == "Europe/Paris"

0 commit comments

Comments
 (0)