Skip to content

In months fix #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions pendulum/_extensions/_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,14 +824,6 @@ PyObject *precise_diff(PyObject *self, PyObject *args)
day_diff += days_in_last_month;
}
}
else if (day_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
day_diff = 0;
month_diff += 1;
}
else
{
// We have a full month
Expand Down
25 changes: 19 additions & 6 deletions tests/date/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
import pendulum


@pytest.fixture(autouse=True)
def setup():
pendulum.set_local_timezone(pendulum.timezone("UTC"))

yield

pendulum.set_test_now()
pendulum.set_locale("en")
pendulum.set_local_timezone()
pendulum.week_starts_at(pendulum.MONDAY)
pendulum.week_ends_at(pendulum.SUNDAY)


@pytest.fixture
def today():
return pendulum.today().date()
Expand Down Expand Up @@ -136,7 +149,7 @@ def test_diff_for_humans_now_and_month():
with pendulum.test(pendulum.datetime(2017, 2, 28)):
today = pendulum.today().date()

assert "1 month ago" == today.subtract(weeks=4).diff_for_humans()
assert "4 weeks ago" == today.subtract(weeks=4).diff_for_humans()


def test_diff_for_humans_now_and_months(today):
Expand Down Expand Up @@ -189,7 +202,7 @@ def test_diff_for_humans_now_and_future_month():
with pendulum.test(pendulum.datetime(2017, 3, 31)):
today = pendulum.today().date()

assert "in 1 month" == today.add(months=1).diff_for_humans()
assert "in 4 weeks" == today.add(months=1).diff_for_humans()

with pendulum.test(pendulum.datetime(2017, 4, 30)):
today = pendulum.today().date()
Expand All @@ -199,7 +212,7 @@ def test_diff_for_humans_now_and_future_month():
with pendulum.test(pendulum.datetime(2017, 1, 31)):
today = pendulum.today().date()

assert "in 1 month" == today.add(weeks=4).diff_for_humans()
assert "in 4 weeks" == today.add(weeks=4).diff_for_humans()


def test_diff_for_humans_now_and_future_months(today):
Expand Down Expand Up @@ -252,7 +265,7 @@ def test_diff_for_humans_other_and_month():
with pendulum.test(pendulum.datetime(2017, 3, 31)):
today = pendulum.today().date()

assert "1 month before" == today.diff_for_humans(today.add(months=1))
assert "4 weeks before" == today.diff_for_humans(today.add(months=1))

with pendulum.test(pendulum.datetime(2017, 4, 30)):
today = pendulum.today().date()
Expand All @@ -262,7 +275,7 @@ def test_diff_for_humans_other_and_month():
with pendulum.test(pendulum.datetime(2017, 1, 31)):
today = pendulum.today().date()

assert "1 month before" == today.diff_for_humans(today.add(weeks=4))
assert "4 weeks before" == today.diff_for_humans(today.add(weeks=4))


def test_diff_for_humans_other_and_months(today):
Expand Down Expand Up @@ -315,7 +328,7 @@ def test_diff_for_humans_other_and_future_month():
with pendulum.test(pendulum.datetime(2017, 2, 28)):
today = pendulum.today().date()

assert "1 month after" == today.diff_for_humans(today.subtract(weeks=4))
assert "4 weeks after" == today.diff_for_humans(today.subtract(weeks=4))


def test_diff_for_humans_other_and_future_months(today):
Expand Down
26 changes: 26 additions & 0 deletions tests/duration/test_in_methods.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import pendulum


Expand All @@ -24,3 +26,27 @@ def test_in_minutes():
def test_in_seconds():
it = pendulum.duration(seconds=72)
assert it.in_seconds() == 72


@pytest.mark.parametrize("month", range(1, 13))
def test_in_months_one_year_less_a_day(month):
start = pendulum.datetime(2022, month, 2).date()
end = pendulum.datetime(2023, month, 1).date() # One year less a day
assert (end - start).in_months() == 11


@pytest.mark.parametrize("month", range(1, 13))
def test_in_months_one_year(month):
start = pendulum.datetime(2022, month, 1).date()
end = pendulum.datetime(2023, month, 1).date() # One year exactly
assert (end - start).in_months() == 12


@pytest.mark.parametrize("day", range(1, 29))
def test_in_months_february(day):
start = pendulum.datetime(2022, 2, 4).date()
end_1 = pendulum.datetime(2023, 2, day).date()
end_2 = end_1.add(days=1)
m_diff_1 = (end_1 - start).in_months()
m_diff_2 = (end_2 - start).in_months()
assert m_diff_1 <= m_diff_2