From 9965d91ad695f630b58ddb70136a12a651a9ddeb Mon Sep 17 00:00:00 2001 From: Jesse Harwin Date: Mon, 20 May 2024 11:48:09 -0700 Subject: [PATCH 1/4] 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/4] 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/4] 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/4] 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)