From 9965d91ad695f630b58ddb70136a12a651a9ddeb Mon Sep 17 00:00:00 2001 From: Jesse Harwin Date: Mon, 20 May 2024 11:48:09 -0700 Subject: [PATCH 1/7] Fixed pluralization in test --- tests/duration/test_in_words.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/duration/test_in_words.py b/tests/duration/test_in_words.py index c0a1a1fe..3f97703b 100644 --- a/tests/duration/test_in_words.py +++ b/tests/duration/test_in_words.py @@ -62,7 +62,7 @@ def test_separator(): def test_subseconds(): pi = pendulum.duration(microseconds=123456) - assert pi.in_words() == "0.12 second" + assert pi.in_words() == "0.12 seconds" def test_subseconds_with_seconds(): From c24d12e219971264749e1d63672edee73551b98b Mon Sep 17 00:00:00 2001 From: Jesse Harwin Date: Mon, 20 May 2024 11:51:37 -0700 Subject: [PATCH 2/7] Added test for extra decimal places for subsecond strings --- tests/duration/test_in_words.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/duration/test_in_words.py b/tests/duration/test_in_words.py index 3f97703b..191ba844 100644 --- a/tests/duration/test_in_words.py +++ b/tests/duration/test_in_words.py @@ -65,9 +65,14 @@ def test_subseconds(): assert pi.in_words() == "0.12 seconds" +def test_subseconds_with_n_digits(): + pi = pendulum.duration(microseconds=123456) + + assert pi.in_words(seconds_n_decimal=3) == "0.123 seconds" + + def test_subseconds_with_seconds(): pi = pendulum.duration(seconds=12, microseconds=123456) - assert pi.in_words() == "12 seconds" From b1b357707b815e66fb419e8f4af6118f5834ef60 Mon Sep 17 00:00:00 2001 From: Jesse Harwin Date: Mon, 20 May 2024 12:02:39 -0700 Subject: [PATCH 3/7] Fixed pluralization bug and added arbitrary decimal places on subseconds --- src/pendulum/duration.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index a4875fca..24447d91 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -238,7 +238,12 @@ def in_minutes(self) -> int: def in_seconds(self) -> int: return int(self.total_seconds()) - def in_words(self, locale: str | None = None, separator: str = " ") -> str: + def in_words( + self, + locale: str | None = None, + separator: str = " ", + seconds_n_decimal: int = 2, + ) -> str: """ Get the current interval in words in the current locale. @@ -246,6 +251,9 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str: :param locale: The locale to use. Defaults to current locale. :param separator: The separator to use between each unit + :param kwargs: Additional keyword arguments. + - seconds_n_decimal (int): The number of decimal places to use for seconds if no other time units are present. Defaults to 2. + """ intervals = [ ("year", self.years), @@ -273,9 +281,10 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str: if not parts: count: int | str = 0 - if abs(self.microseconds) > 0: - unit = f"units.second.{loaded_locale.plural(1)}" - count = f"{abs(self.microseconds) / 1e6:.2f}" + unit = f"units.second.{loaded_locale.plural(0)}" + if self.microseconds != 0: + microseconds = abs(self.microseconds) / 1e6 + count = f"{round(microseconds, ndigits=seconds_n_decimal)}" else: unit = f"units.microsecond.{loaded_locale.plural(0)}" translation = loaded_locale.translation(unit) From 231cdaee7350d783915e76fdc78e7bd02e8d3849 Mon Sep 17 00:00:00 2001 From: Solipsistmonkey <103457994+Solipsistmonkey@users.noreply.github.com> Date: Mon, 20 May 2024 12:39:21 -0700 Subject: [PATCH 4/7] Update src/pendulum/duration.py Co-authored-by: Vasco Schiavo <115561717+VascoSch92@users.noreply.github.com> --- src/pendulum/duration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index 24447d91..074ae5e4 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -284,7 +284,7 @@ def in_words( unit = f"units.second.{loaded_locale.plural(0)}" if self.microseconds != 0: microseconds = abs(self.microseconds) / 1e6 - count = f"{round(microseconds, ndigits=seconds_n_decimal)}" + count = str({round(microseconds, ndigits=seconds_n_decimal)}) else: unit = f"units.microsecond.{loaded_locale.plural(0)}" translation = loaded_locale.translation(unit) From 14dec35188d97ec2c85e882389c28826c4f2b661 Mon Sep 17 00:00:00 2001 From: Ash Berlin-Taylor Date: Wed, 16 Jul 2025 14:31:30 +0100 Subject: [PATCH 5/7] Apply suggestions from code review Separating out fix from adding seoncds_n_decimal --- src/pendulum/duration.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index 074ae5e4..4181749e 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -242,7 +242,6 @@ def in_words( self, locale: str | None = None, separator: str = " ", - seconds_n_decimal: int = 2, ) -> str: """ Get the current interval in words in the current locale. @@ -251,9 +250,6 @@ def in_words( :param locale: The locale to use. Defaults to current locale. :param separator: The separator to use between each unit - :param kwargs: Additional keyword arguments. - - seconds_n_decimal (int): The number of decimal places to use for seconds if no other time units are present. Defaults to 2. - """ intervals = [ ("year", self.years), @@ -281,10 +277,11 @@ def in_words( if not parts: count: int | str = 0 - unit = f"units.second.{loaded_locale.plural(0)}" + unit: str if self.microseconds != 0: + unit = f"units.second.{loaded_locale.plural(0)}" microseconds = abs(self.microseconds) / 1e6 - count = str({round(microseconds, ndigits=seconds_n_decimal)}) + count = f"{abs(self.microseconds) / 1e6:.2f}" else: unit = f"units.microsecond.{loaded_locale.plural(0)}" translation = loaded_locale.translation(unit) From 80d4b824e69679d2b8ba55062c33d8d89a0f0253 Mon Sep 17 00:00:00 2001 From: Ash Berlin-Taylor Date: Wed, 16 Jul 2025 14:37:06 +0100 Subject: [PATCH 6/7] Apply suggestions from code review --- src/pendulum/duration.py | 8 +------- tests/duration/test_in_words.py | 6 ------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index 4181749e..d0ede8ff 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -238,11 +238,7 @@ def in_minutes(self) -> int: def in_seconds(self) -> int: return int(self.total_seconds()) - def in_words( - self, - locale: str | None = None, - separator: str = " ", - ) -> str: +def in_words(self, locale: str | None = None, separator: str = " ") -> str: """ Get the current interval in words in the current locale. @@ -277,10 +273,8 @@ def in_words( if not parts: count: int | str = 0 - unit: str if self.microseconds != 0: unit = f"units.second.{loaded_locale.plural(0)}" - microseconds = abs(self.microseconds) / 1e6 count = f"{abs(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 191ba844..13982c33 100644 --- a/tests/duration/test_in_words.py +++ b/tests/duration/test_in_words.py @@ -65,12 +65,6 @@ def test_subseconds(): assert pi.in_words() == "0.12 seconds" -def test_subseconds_with_n_digits(): - pi = pendulum.duration(microseconds=123456) - - assert pi.in_words(seconds_n_decimal=3) == "0.123 seconds" - - def test_subseconds_with_seconds(): pi = pendulum.duration(seconds=12, microseconds=123456) assert pi.in_words() == "12 seconds" From f454f09934d8aee543a50bd1bfeb693f44a0610a Mon Sep 17 00:00:00 2001 From: Ash Berlin-Taylor Date: Thu, 17 Jul 2025 14:18:07 +0100 Subject: [PATCH 7/7] Update src/pendulum/duration.py --- src/pendulum/duration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pendulum/duration.py b/src/pendulum/duration.py index d0ede8ff..1e209334 100644 --- a/src/pendulum/duration.py +++ b/src/pendulum/duration.py @@ -238,7 +238,7 @@ def in_minutes(self) -> int: def in_seconds(self) -> int: return int(self.total_seconds()) -def in_words(self, locale: str | None = None, separator: str = " ") -> str: + def in_words(self, locale: str | None = None, separator: str = " ") -> str: """ Get the current interval in words in the current locale.