Skip to content

Commit d32f5df

Browse files
redlickigrzegorzsdispater
authored andcommitted
Implement recognition of the transition for int_timestamp method (#296)
1 parent 31d15ae commit d32f5df

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pendulum/datetime.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ def float_timestamp(self):
185185
def int_timestamp(self):
186186
# Workaround needed to avoid inaccuracy
187187
# for far into the future datetimes
188+
kwargs = {"tzinfo": self.tzinfo}
189+
190+
if _HAS_FOLD:
191+
kwargs["fold"] = self.fold
192+
188193
dt = datetime.datetime(
189194
self.year,
190195
self.month,
@@ -193,7 +198,7 @@ def int_timestamp(self):
193198
self.minute,
194199
self.second,
195200
self.microsecond,
196-
tzinfo=self.tzinfo,
201+
**kwargs
197202
)
198203

199204
delta = dt - self._EPOCH

tests/datetime/test_getters.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from pendulum import DateTime
66
from pendulum.tz import timezone
7+
from pendulum.utils._compat import _HAS_FOLD
78

89
from ..conftest import assert_date, assert_time
910

@@ -92,6 +93,29 @@ def test_int_timestamp_accuracy():
9293
assert d.int_timestamp == 32527311790
9394

9495

96+
def test_timestamp_with_transition():
97+
d_pre = pendulum.datetime(
98+
2012, 10, 28, 2, 0, tz="Europe/Warsaw", dst_rule=pendulum.PRE_TRANSITION
99+
)
100+
d_post = pendulum.datetime(
101+
2012, 10, 28, 2, 0, tz="Europe/Warsaw", dst_rule=pendulum.POST_TRANSITION
102+
)
103+
104+
if _HAS_FOLD:
105+
# the difference between the timestamps before and after is equal to one hour
106+
assert d_post.timestamp() - d_pre.timestamp() == pendulum.SECONDS_PER_HOUR
107+
assert d_post.float_timestamp - d_pre.float_timestamp == (
108+
pendulum.SECONDS_PER_HOUR
109+
)
110+
assert d_post.int_timestamp - d_pre.int_timestamp == pendulum.SECONDS_PER_HOUR
111+
else:
112+
# when the transition is not recognizable
113+
# then the difference should be equal to zero hours
114+
assert d_post.timestamp() - d_pre.timestamp() == 0
115+
assert d_post.float_timestamp - d_pre.float_timestamp == 0
116+
assert d_post.int_timestamp - d_pre.int_timestamp == 0
117+
118+
95119
def test_age():
96120
d = pendulum.now()
97121
assert d.age == 0

0 commit comments

Comments
 (0)