Skip to content

Commit 4fd7174

Browse files
authored
Fix subtracting negative timedelta around DST (#419)
1 parent 51d2f69 commit 4fd7174

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pendulum/datetime.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,7 @@ def _subtract_timedelta(self, delta):
780780
microseconds=delta.microseconds,
781781
)
782782

783-
return self.subtract(
784-
days=delta.days, seconds=delta.seconds, microseconds=delta.microseconds
785-
)
783+
return self.subtract(seconds=delta.total_seconds())
786784

787785
# DIFFERENCES
788786

tests/datetime/test_sub.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,18 @@ def test_subtract_invalid_type():
231231

232232
with pytest.raises(TypeError):
233233
"ab" - d
234+
235+
236+
def test_subtract_negative_over_dls_transitioning_off():
237+
just_before_dls_ends = pendulum.datetime(
238+
2019, 11, 3, 1, 30, tz="US/Pacific", dst_rule=pendulum.PRE_TRANSITION
239+
)
240+
plus_10_hours = just_before_dls_ends + timedelta(hours=10)
241+
minus_neg_10_hours = just_before_dls_ends - timedelta(hours=-10)
242+
243+
# 1:30-0700 becomes 10:30-0800
244+
assert plus_10_hours.hour == 10
245+
assert minus_neg_10_hours.hour == 10
246+
assert just_before_dls_ends.is_dst()
247+
assert not plus_10_hours.is_dst()
248+
assert not minus_neg_10_hours.is_dst()

0 commit comments

Comments
 (0)