From fb199565ae0cc834b49ff68ef47114c022e65f8d Mon Sep 17 00:00:00 2001 From: Grzegorz Redlicki Date: Sat, 27 Oct 2018 16:41:37 +0200 Subject: [PATCH] Implement recognition of the transition for int_timestamp method --- pendulum/datetime.py | 7 ++++++- tests/datetime/test_getters.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pendulum/datetime.py b/pendulum/datetime.py index 59ebcfb0..2fed8564 100644 --- a/pendulum/datetime.py +++ b/pendulum/datetime.py @@ -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, @@ -193,7 +198,7 @@ def int_timestamp(self): self.minute, self.second, self.microsecond, - tzinfo=self.tzinfo, + **kwargs ) delta = dt - self._EPOCH diff --git a/tests/datetime/test_getters.py b/tests/datetime/test_getters.py index 90bc671d..cd9b3eca 100644 --- a/tests/datetime/test_getters.py +++ b/tests/datetime/test_getters.py @@ -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 @@ -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