From 97511308c54d1e838ee163bfcf8f8705f4899ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3mas=20=C3=81rni=20J=C3=B3nasson?= Date: Mon, 21 Oct 2019 15:31:25 +0000 Subject: [PATCH] Fix subtracting negative timedelta around DST --- pendulum/datetime.py | 4 +--- tests/datetime/test_sub.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pendulum/datetime.py b/pendulum/datetime.py index 4e2762d3..610af71e 100644 --- a/pendulum/datetime.py +++ b/pendulum/datetime.py @@ -780,9 +780,7 @@ def _subtract_timedelta(self, delta): microseconds=delta.microseconds, ) - return self.subtract( - days=delta.days, seconds=delta.seconds, microseconds=delta.microseconds - ) + return self.subtract(seconds=delta.total_seconds()) # DIFFERENCES diff --git a/tests/datetime/test_sub.py b/tests/datetime/test_sub.py index 7e44eda2..61d9a8bf 100644 --- a/tests/datetime/test_sub.py +++ b/tests/datetime/test_sub.py @@ -231,3 +231,18 @@ def test_subtract_invalid_type(): with pytest.raises(TypeError): "ab" - d + + +def test_subtract_negative_over_dls_transitioning_off(): + just_before_dls_ends = pendulum.datetime( + 2019, 11, 3, 1, 30, tz="US/Pacific", dst_rule=pendulum.PRE_TRANSITION + ) + plus_10_hours = just_before_dls_ends + timedelta(hours=10) + minus_neg_10_hours = just_before_dls_ends - timedelta(hours=-10) + + # 1:30-0700 becomes 10:30-0800 + assert plus_10_hours.hour == 10 + assert minus_neg_10_hours.hour == 10 + assert just_before_dls_ends.is_dst() + assert not plus_10_hours.is_dst() + assert not minus_neg_10_hours.is_dst()