Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/pendulum/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from datetime import timedelta
from typing import TYPE_CHECKING
from typing import cast
from typing import overload

import pendulum
Expand Down Expand Up @@ -43,6 +42,15 @@ def _divide_and_round(a: float, b: float) -> int:
return q


def _timedelta_to_microseconds(delta: timedelta) -> int:
if isinstance(delta, Duration):
return delta._to_microseconds()

return (
delta.days * SECONDS_PER_DAY + delta.seconds
) * US_PER_SECOND + delta.microseconds


class Duration(timedelta):
"""
Replacement for the standard timedelta class.
Expand Down Expand Up @@ -386,10 +394,7 @@ def __floordiv__(self, other: int | timedelta) -> int | Duration:

usec = self._to_microseconds()
if isinstance(other, timedelta):
return cast(
"int",
usec // other._to_microseconds(), # type: ignore[attr-defined]
)
return usec // _timedelta_to_microseconds(other)

if isinstance(other, int):
return self.__class__(
Expand All @@ -412,10 +417,7 @@ def __truediv__(self, other: int | float | timedelta) -> Self | float:

usec = self._to_microseconds()
if isinstance(other, timedelta):
return cast(
"float",
usec / other._to_microseconds(), # type: ignore[attr-defined]
)
return usec / _timedelta_to_microseconds(other)

if isinstance(other, int):
return self.__class__(
Expand All @@ -441,7 +443,7 @@ def __truediv__(self, other: int | float | timedelta) -> Self | float:

def __mod__(self, other: timedelta) -> Self:
if isinstance(other, timedelta):
r = self._to_microseconds() % other._to_microseconds() # type: ignore[attr-defined]
r = self._to_microseconds() % _timedelta_to_microseconds(other)

return self.__class__(0, 0, r)

Expand All @@ -451,7 +453,7 @@ def __divmod__(self, other: timedelta) -> tuple[int, Duration]:
if isinstance(other, timedelta):
q, r = divmod(
self._to_microseconds(),
other._to_microseconds(), # type: ignore[attr-defined]
_timedelta_to_microseconds(other),
)

return q, self.__class__(0, 0, r)
Expand Down
31 changes: 31 additions & 0 deletions tests/duration/test_arithmetic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from __future__ import annotations

import operator

from datetime import timedelta

import pytest

import pendulum

from tests.conftest import assert_duration
Expand Down Expand Up @@ -83,3 +89,28 @@ def test_floor_divide():

assert isinstance(mul, pendulum.Duration)
assert_duration(mul, 0, 1, 0, 0, 16, 0, 11, 840740)


@pytest.mark.parametrize(
"operation", [operator.truediv, operator.floordiv, operator.mod, divmod]
)
@pytest.mark.parametrize(
"divisor",
[
timedelta(seconds=2, microseconds=1),
timedelta(seconds=-2, microseconds=1),
timedelta(days=500_000, microseconds=1),
],
)
def test_arithmetic_with_native_timedelta(operation, divisor):
native = timedelta(days=1, seconds=1, microseconds=3)
duration = pendulum.duration(days=1, seconds=1, microseconds=3)
assert operation(duration, divisor) == operation(native, divisor)


@pytest.mark.parametrize(
"operation", [operator.truediv, operator.floordiv, operator.mod, divmod]
)
def test_arithmetic_with_zero_native_timedelta(operation):
with pytest.raises(ZeroDivisionError):
operation(pendulum.duration(seconds=1), timedelta())