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
6 changes: 6 additions & 0 deletions src/pendulum/duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions tests/duration/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)