Skip to content
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

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

Open
2 tasks done
gazpachoking opened this issue Jan 28, 2024 · 1 comment
Open
2 tasks done

Comments

@gazpachoking
Copy link
Contributor

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

def _to_microseconds(self) -> int:
return (self._days * (24 * 3600) + self._seconds) * 1000000 + self._microseconds

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
Copy link
Contributor

Benji19967 commented Mar 8, 2024

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

@sdispater @Secrus I see two options here:

  • Raise an error when floating-point multiplication is applied on a Duration with years>0 and/or months>0.
  • Round up or down to the nearest integer as is done with __truediv__
    if isinstance(other, float):
    a, b = other.as_integer_ratio()
    return self.__class__(
    0,
    0,
    _divide_and_round(b * usec, a),
    years=_divide_and_round(self._years * b, a),
    months=_divide_and_round(self._months, other),

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

2 participants