From 8c0fb7cbebf7355d187a9e476c31b66d8188215b Mon Sep 17 00:00:00 2001 From: kevin <5299031+kevin9327@users.noreply.github.com> Date: Wed, 4 Nov 2026 04:11:54 +0900 Subject: [PATCH] Keep the sign of negative sub-second durations in in_words() --- src/pendulum/duration.py | 2 +- src/pendulum/interval.py | 2 +- tests/duration/test_in_words.py | 6 ++++++ tests/interval/test_in_words.py | 8 ++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index d6cc0657d..5022e0424 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -275,7 +275,7 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str: count: int | str = 0 if self.microseconds != 0: unit = f"units.second.{loaded_locale.plural(0)}" - count = f"{abs(self.microseconds) / 1e6:.2f}" + count = f"{self.microseconds / 1e6:.2f}" else: unit = f"units.microsecond.{loaded_locale.plural(0)}" translation = loaded_locale.translation(unit) diff --git a/src/pendulum/interval.py b/src/pendulum/interval.py index c5e0713a7..d2ab70fba 100644 --- a/src/pendulum/interval.py +++ b/src/pendulum/interval.py @@ -278,7 +278,7 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str: count: str | int = 0 if abs(self.microseconds) > 0: unit = f"units.second.{loaded_locale.plural(1)}" - count = f"{abs(self.microseconds) / 1e6:.2f}" + count = f"{self.microseconds / 1e6:.2f}" else: unit = f"units.microsecond.{loaded_locale.plural(0)}" diff --git a/tests/duration/test_in_words.py b/tests/duration/test_in_words.py index 13982c33d..af6529e2f 100644 --- a/tests/duration/test_in_words.py +++ b/tests/duration/test_in_words.py @@ -65,6 +65,12 @@ def test_subseconds(): assert pi.in_words() == "0.12 seconds" +def test_negative_subseconds(): + pi = pendulum.duration(microseconds=-123456) + + assert pi.in_words() == "-0.12 seconds" + + def test_subseconds_with_seconds(): pi = pendulum.duration(seconds=12, microseconds=123456) assert pi.in_words() == "12 seconds" diff --git a/tests/interval/test_in_words.py b/tests/interval/test_in_words.py index f60e20d04..2144877c5 100644 --- a/tests/interval/test_in_words.py +++ b/tests/interval/test_in_words.py @@ -56,6 +56,14 @@ def test_subseconds(): assert interval.in_words() == "0.12 second" +def test_negative_subseconds(): + start_date = pendulum.datetime(2012, 1, 1) + interval = pendulum.interval( + start=start_date.add(microseconds=123456), end=start_date + ) + assert interval.in_words() == "-0.12 second" + + def test_subseconds_with_seconds(): start_date = pendulum.datetime(2012, 1, 1) interval = pendulum.interval(