Skip to content

Commit 9bb5fb2

Browse files
committed
Fixes wrong result when adding/subtracting a Period if a DST transition occurs.
Fixes #125
1 parent c2df79d commit 9bb5fb2

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- Fixed `next()` and `previous()` hanging when passed an invalid input.
8+
- Fixed wrong result when adding/subtracting a Period if a DST transition occurs.
9+
10+
311
## [1.2.1] - 2017-05-23
412

513
### Fixed

pendulum/pendulum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,14 @@ def add_timedelta(self, delta):
11401140
11411141
:rtype: Pendulum
11421142
"""
1143+
if isinstance(delta, Period):
1144+
return self.add(
1145+
years=delta.years, months=delta.months,
1146+
weeks=delta.weeks, days=delta.remaining_days,
1147+
hours=delta.hours, minutes=delta.minutes,
1148+
seconds=delta.remaining_seconds, microseconds=delta.microseconds
1149+
)
1150+
11431151
return self.add(days=delta.days, seconds=delta.seconds,
11441152
microseconds=delta.microseconds)
11451153

@@ -1152,6 +1160,14 @@ def subtract_timedelta(self, delta):
11521160
11531161
:rtype: Pendulum
11541162
"""
1163+
if isinstance(delta, Period):
1164+
return self.subtract(
1165+
years=delta.years, months=delta.months,
1166+
weeks=delta.weeks, days=delta.remaining_days,
1167+
hours=delta.hours, minutes=delta.minutes,
1168+
seconds=delta.remaining_seconds, microseconds=delta.microseconds
1169+
)
1170+
11551171
return self.subtract(days=delta.days, seconds=delta.seconds,
11561172
microseconds=delta.microseconds)
11571173

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pendulum
4+
5+
6+
def test_dst_add():
7+
start = pendulum.create(2017, 3, 7, tz='America/Toronto')
8+
end = start.add(days=6)
9+
period = end - start
10+
new_end = start + period
11+
12+
assert new_end == end
13+
14+
15+
def test_dst_subtract():
16+
start = pendulum.create(2017, 3, 7, tz='America/Toronto')
17+
end = start.add(days=6)
18+
period = end - start
19+
new_start = end - period
20+
21+
assert new_start == start

tests/period_tests/test_construct.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
import pendulum
34
from datetime import datetime
45
from pendulum import Period, Pendulum
56

@@ -72,6 +73,18 @@ def test_accuracy(self):
7273
self.assertEqual(4, p2.remaining_days)
7374
self.assertEqual(5824, p2.in_days())
7475

76+
def test_dst_transition(self):
77+
start = pendulum.create(2017, 3, 7, tz='America/Toronto')
78+
end = start.add(days=6)
79+
period = end - start
80+
81+
assert period.days == 5
82+
assert period.seconds == 82800
83+
84+
assert period.remaining_days == 6
85+
assert period.hours == 0
86+
assert period.remaining_seconds == 0
87+
7588
def test_timedelta_behavior(self):
7689
dt1 = Pendulum(2000, 11, 20, 1)
7790
dt2 = Pendulum(2000, 11, 25, 2)

0 commit comments

Comments
 (0)