Skip to content

in_months behavior inconsistent in 30-day months #606

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
2 tasks done
rileyjohngibbs opened this issue Mar 9, 2022 · 2 comments
Open
2 tasks done

in_months behavior inconsistent in 30-day months #606

rileyjohngibbs opened this issue Mar 9, 2022 · 2 comments

Comments

@rileyjohngibbs
Copy link

  • I am on the latest Pendulum version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: macOS 11.6.1
  • Pendulum version: 2.1.2 and 3.0.0a0

Issue

When calculating a diff's in_months method, the following results seem inconsistent:

start end in_months result
2022-01-02 2023-01-01 11
2022-02-02 2023-02-01 11
2022-03-02 2023-03-01 11
2022-04-02 2023-04-01 12
2022-05-02 2023-05-01 11
2022-06-02 2023-06-01 12
2022-07-02 2023-07-01 11
2022-08-02 2023-08-01 11
2022-09-02 2023-09-01 12
2022-10-02 2023-10-01 11
2022-11-02 2023-11-01 12
2022-12-02 2023-12-01 11

It seems that whenever the end date is in one of the months with 30 days (April, June, September, November), if the start date's day of the month is one day later than the corresponding end day of the month, the number of months from in_months is one higher than it "should" be.

Obviously this is a sticky issue because the definition of a "month" is not well defined (it can be any value from 28 to 31 days), but since this behavior doesn't happen with February, the behavior seems inconsistent at the least.

Desired Result

The in_months method should be determined based on the comparison of the day of the month, not the duration of the last month in the interval, which seems to be the root of this behavior.

@rileyjohngibbs
Copy link
Author

rileyjohngibbs commented Mar 9, 2022

Some more inconsistency, specifically with February:

>>> import pendulum
>>> start = pendulum.parse("2022-02-05")
>>> for d in range(1, 29):
...     months = (pendulum.parse(f"2023-02-{str(d).zfill(2)}") - start).in_months()
...     print(d, months)
... 
1 11
2 12
3 11
4 11
5 12
6 12
7 12
8 12
9 12
10 12
11 12
12 12
13 12
14 12
15 12
16 12
17 12
18 12
19 12
20 12
21 12
22 12
23 12
24 12
25 12
26 12
27 12
28 12

As you can see, February 5th to February 2nd of the following year is calculated as 12 months, but February 5th to February 3rd of the following year (one day longer) is calculated as only 11 months.

I think, again, the root cause is this check for equality.

@rileyjohngibbs
Copy link
Author

I think I have a (remarkably brutish) fix for this but some other tests are now failing, starting with the fix exposing a masked bug (see #608).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant