Skip to content

Commit 063eea7

Browse files
Jesse Harwinashb
authored andcommitted
Fixed pluralization bug and added arbitrary decimal places on subseconds
1 parent 51b6761 commit 063eea7

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/pendulum/duration.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,22 @@ def in_minutes(self) -> int:
238238
def in_seconds(self) -> int:
239239
return int(self.total_seconds())
240240

241-
def in_words(self, locale: str | None = None, separator: str = " ") -> str:
241+
def in_words(
242+
self,
243+
locale: str | None = None,
244+
separator: str = " ",
245+
seconds_n_decimal: int = 2,
246+
) -> str:
242247
"""
243248
Get the current interval in words in the current locale.
244249
245250
Ex: 6 jours 23 heures 58 minutes
246251
247252
:param locale: The locale to use. Defaults to current locale.
248253
:param separator: The separator to use between each unit
254+
:param kwargs: Additional keyword arguments.
255+
- seconds_n_decimal (int): The number of decimal places to use for seconds if no other time units are present. Defaults to 2.
256+
249257
"""
250258
intervals = [
251259
("year", self.years),
@@ -273,9 +281,10 @@ def in_words(self, locale: str | None = None, separator: str = " ") -> str:
273281

274282
if not parts:
275283
count: int | str = 0
276-
if abs(self.microseconds) > 0:
277-
unit = f"units.second.{loaded_locale.plural(1)}"
278-
count = f"{abs(self.microseconds) / 1e6:.2f}"
284+
unit = f"units.second.{loaded_locale.plural(0)}"
285+
if self.microseconds != 0:
286+
microseconds = abs(self.microseconds) / 1e6
287+
count = f"{round(microseconds, ndigits=seconds_n_decimal)}"
279288
else:
280289
unit = f"units.microsecond.{loaded_locale.plural(0)}"
281290
translation = loaded_locale.translation(unit)

0 commit comments

Comments
 (0)