Skip to content

Commit d29cd2f

Browse files
committed
Fixes precise_diff on full month.
1 parent feaa62a commit d29cd2f

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

pendulum/helpers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,17 @@ def precise_diff(d1, d2):
190190
d_diff += d1.day
191191
else:
192192
d_diff += days_in_last_month
193-
194-
m_diff -= 1
195-
else:
196-
# We have a full month, remove days
193+
elif d_diff == days_in_month - days_in_last_month:
194+
# We have exactly a full month
195+
# We remove the days difference
196+
# and add one to the months difference
197197
d_diff = 0
198+
m_diff += 1
199+
else:
200+
# We have a full month
201+
d_diff += days_in_last_month
202+
203+
m_diff -= 1
198204

199205
if m_diff < 0:
200206
m_diff += 12

tests/test_helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ def test_precise_diff(self):
3737
hours=20, minutes=54, seconds=47, microseconds=282310
3838
)
3939

40+
dt1 = datetime(2017, 2, 17, 16, 5, 45, 123456)
41+
dt2 = datetime(2018, 2, 17, 16, 5, 45, 123256)
42+
43+
diff = precise_diff(dt1, dt2)
44+
self.assert_diff(
45+
diff,
46+
months=11, days=30, hours=23, minutes=59, seconds=59, microseconds=999800
47+
)
48+
4049
def assert_diff(self, diff,
4150
years=0, months=0, days=0,
4251
hours=0, minutes=0, seconds=0, microseconds=0):

0 commit comments

Comments
 (0)