From d29cd2fa8690eaf8c37da33ab6802e326442d794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Sat, 18 Feb 2017 14:02:51 -0500 Subject: [PATCH] Fixes precise_diff on full month. --- pendulum/helpers.py | 14 ++++++++++---- tests/test_helpers.py | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pendulum/helpers.py b/pendulum/helpers.py index 2b890487..5f2a3029 100644 --- a/pendulum/helpers.py +++ b/pendulum/helpers.py @@ -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 diff --git a/tests/test_helpers.py b/tests/test_helpers.py index c0775e8b..65c536e2 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -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):