Skip to content
Merged
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
7 changes: 6 additions & 1 deletion pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ def float_timestamp(self):
def int_timestamp(self):
# Workaround needed to avoid inaccuracy
# for far into the future datetimes
kwargs = {"tzinfo": self.tzinfo}

if _HAS_FOLD:
kwargs["fold"] = self.fold

dt = datetime.datetime(
self.year,
self.month,
Expand All @@ -193,7 +198,7 @@ def int_timestamp(self):
self.minute,
self.second,
self.microsecond,
tzinfo=self.tzinfo,
**kwargs
)

delta = dt - self._EPOCH
Expand Down
24 changes: 24 additions & 0 deletions tests/datetime/test_getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pendulum import DateTime
from pendulum.tz import timezone
from pendulum.utils._compat import _HAS_FOLD

from ..conftest import assert_date, assert_time

Expand Down Expand Up @@ -92,6 +93,29 @@ def test_int_timestamp_accuracy():
assert d.int_timestamp == 32527311790


def test_timestamp_with_transition():
d_pre = pendulum.datetime(
2012, 10, 28, 2, 0, tz="Europe/Warsaw", dst_rule=pendulum.PRE_TRANSITION
)
d_post = pendulum.datetime(
2012, 10, 28, 2, 0, tz="Europe/Warsaw", dst_rule=pendulum.POST_TRANSITION
)

if _HAS_FOLD:
# the difference between the timestamps before and after is equal to one hour
assert d_post.timestamp() - d_pre.timestamp() == pendulum.SECONDS_PER_HOUR
assert d_post.float_timestamp - d_pre.float_timestamp == (
pendulum.SECONDS_PER_HOUR
)
assert d_post.int_timestamp - d_pre.int_timestamp == pendulum.SECONDS_PER_HOUR
else:
# when the transition is not recognizable
# then the difference should be equal to zero hours
assert d_post.timestamp() - d_pre.timestamp() == 0
assert d_post.float_timestamp - d_pre.float_timestamp == 0
assert d_post.int_timestamp - d_pre.int_timestamp == 0


def test_age():
d = pendulum.now()
assert d.age == 0
Expand Down