Skip to content

Commit

Permalink
Fix subtracting negative timedelta around DST
Browse files Browse the repository at this point in the history
  • Loading branch information
tomage committed Oct 21, 2019
1 parent bc32743 commit 15ded3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 1 addition & 3 deletions pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,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

Expand Down
15 changes: 15 additions & 0 deletions tests/datetime/test_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 15ded3f

Please sign in to comment.