Skip to content

Commit 9b3a086

Browse files
committed
Fix an error when subtracting two pendulum instances in the same timezone
1 parent a7400a7 commit 9b3a086

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Fixed an error when adding intervals to a Pendulum instance across DST transition.
8+
- Fixed an error when subtracting two pendulum instances in the same timezone.
89

910

1011
## [1.4.2] - 2018-02-22

pendulum/helpers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ def precise_diff(d1, d2):
159159
d1, d2 = d2, d1
160160
sign = -1
161161

162-
y_diff = d2.year - d1.year
163-
m_diff = d2.month - d1.month
164-
d_diff = d2.day - d1.day
162+
d_diff = 0
165163
hour_diff = 0
166164
min_diff = 0
167165
sec_diff = 0
@@ -170,6 +168,7 @@ def precise_diff(d1, d2):
170168
_day_number(d2.year, d2.month, d2.day)
171169
- _day_number(d1.year, d1.month, d1.day)
172170
)
171+
173172
in_same_tz = False
174173
tz1 = None
175174
tz2 = None
@@ -228,6 +227,14 @@ def precise_diff(d1, d2):
228227
hour_diff += 24
229228
d_diff -= 1
230229

230+
if d1 > d2:
231+
d1, d2 = d2, d1
232+
sign = -1
233+
234+
y_diff = d2.year - d1.year
235+
m_diff = d2.month - d1.month
236+
d_diff += d2.day - d1.day
237+
231238
if d_diff < 0:
232239
year = d2.year
233240
month = d2.month

tests/pendulum_tests/test_add.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ def test_add_time_to_new_transition_does_not_use_transition_rule(self):
237237
self.assertEqual(7200, dt.offset)
238238
self.assertTrue(dt.is_dst)
239239

240+
def test_period_over_midnight_tz(self):
241+
start = pendulum.create(2018, 2, 25, tz='Europe/Paris')
242+
end = start.add(hours=1)
243+
period = end - start
244+
new_end = start + period
245+
246+
assert new_end == end
247+
240248
def test_add_interval(self):
241249
dt = pendulum.create(2017, 3, 11, 10, 45, tz='America/Los_Angeles')
242250
new = dt + pendulum.interval(hours=24)

0 commit comments

Comments
 (0)