Skip to content

Commit

Permalink
Fixes precise_diff on full month.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Feb 18, 2017
1 parent feaa62a commit d29cd2f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pendulum/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,17 @@ def precise_diff(d1, d2):
d_diff += d1.day
else:
d_diff += days_in_last_month

m_diff -= 1
else:
# We have a full month, remove days
elif d_diff == days_in_month - days_in_last_month:
# We have exactly a full month
# We remove the days difference
# and add one to the months difference
d_diff = 0
m_diff += 1
else:
# We have a full month
d_diff += days_in_last_month

m_diff -= 1

if m_diff < 0:
m_diff += 12
Expand Down
9 changes: 9 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def test_precise_diff(self):
hours=20, minutes=54, seconds=47, microseconds=282310
)

dt1 = datetime(2017, 2, 17, 16, 5, 45, 123456)
dt2 = datetime(2018, 2, 17, 16, 5, 45, 123256)

diff = precise_diff(dt1, dt2)
self.assert_diff(
diff,
months=11, days=30, hours=23, minutes=59, seconds=59, microseconds=999800
)

def assert_diff(self, diff,
years=0, months=0, days=0,
hours=0, minutes=0, seconds=0, microseconds=0):
Expand Down

0 comments on commit d29cd2f

Please sign in to comment.