sdispater / pendulum

Python datetimes made easy
https://pendulum.eustace.io
MIT License
6.12k stars 372 forks source link

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

Open gazpachoking opened 5 months ago

gazpachoking commented 5 months ago

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()
Benji19967 commented 3 months ago

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

@sdispater @Secrus I see two options here: