Skip to content

Duration division, multiplication and mod broken for month lengths. #799

Description

@gazpachoking
  • 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: Windows 11
  • Pendulum version: 3.0.0

Issue

When doing math on Duration objects, months are ignored. This is because it uses the _to_microseconds method which ignores the months attribute.
https://github.com/sdispater/pendulum/blob/3e3fec6a5b66759078c5ae39c736ca95b78adbef/src/pendulum/duration.py#L356-L357
e.g. :

>>> pendulum.Duration(years=1)/pendulum.Duration(months=1)
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2022.2.2\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
  File "C:\Users\chase\PycharmProjects\Flexget\py310\lib\site-packages\pendulum\duration.py", line 419, in __truediv__
    float, usec / other._to_microseconds()  # type: ignore[attr-defined]
ZeroDivisionError: division by zero

>>> pendulum.Duration(months=1)*3.0
Duration()

Activity

  1. Benji19967 commented on Mar 8, 2024

    @Benji19967
    Contributor

    Duration only supports full months and years, hence floating-point multiplication is not supported.

    @sdispater @Secrus I see two options here:

  2. ruaridhw commented on Apr 14, 2025

    @ruaridhw

    It should be relatively straight forward to defer to .total_seconds() since this already makes use of the years and months attributes correctly. I do agree that the number of microseconds (or seconds) in a month or year is ambiguous however pendulum already hardcodes these to be month=30days, year=365days. So given that, it should be possible to divide months by months, for example.

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