Skip to content

in_months behavior inconsistent in 30-day months #606

Open

Description

@rileyjohngibbs
  • 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.

Activity

  1. rileyjohngibbs commented on Mar 9, 2022

    @rileyjohngibbs
    Author

    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.

  2. rileyjohngibbs commented on Mar 9, 2022

    @rileyjohngibbs
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions