diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index d6cc0657d..01429f973 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -353,6 +353,12 @@ def __neg__(self) -> Self: microseconds=-self._microseconds, ) + def __pos__(self) -> Self: + return self + + def __abs__(self) -> Self: + return -self if self.total_seconds() < 0 else self + def _to_microseconds(self) -> int: return (self._days * (24 * 3600) + self._seconds) * 1000000 + self._microseconds diff --git a/tests/duration/test_arithmetic.py b/tests/duration/test_arithmetic.py index cba4d39d0..7d7e0e29c 100644 --- a/tests/duration/test_arithmetic.py +++ b/tests/duration/test_arithmetic.py @@ -83,3 +83,25 @@ def test_floor_divide(): assert isinstance(mul, pendulum.Duration) assert_duration(mul, 0, 1, 0, 0, 16, 0, 11, 840740) + + +def test_abs(): + it = pendulum.duration(years=-2, months=-3, days=-10, seconds=-34, microseconds=-5) + result = abs(it) + + assert isinstance(result, pendulum.Duration) + assert_duration(result, 2, 3, 1, 3, 0, 0, 34, 5) + + it = pendulum.duration(years=2, months=3, days=10) + result = abs(it) + + assert isinstance(result, pendulum.Duration) + assert_duration(result, 2, 3, 1, 3, 0, 0, 0, 0) + + +def test_pos(): + it = pendulum.duration(years=2, months=3, days=10) + result = +it + + assert isinstance(result, pendulum.Duration) + assert_duration(result, 2, 3, 1, 3, 0, 0, 0, 0)