This commit is contained in:
Edward Betts 2024-11-13 14:31:35 +00:00
parent 1caf233075
commit 42066f9dde

View file

@ -42,6 +42,11 @@ def timedelta_display(delta: timedelta) -> str:
) )
def plural(value: int, unit: str) -> str:
"""Value + unit with unit written as singular or plural as appropriate."""
return f"{value} {unit}{'s' if value > 1 else ''}"
def human_readable_delta(future_date: date) -> str | None: def human_readable_delta(future_date: date) -> str | None:
""" """
Calculate the human-readable time delta for a given future date. Calculate the human-readable time delta for a given future date.
@ -64,14 +69,11 @@ def human_readable_delta(future_date: date) -> str | None:
weeks, days = divmod(days, 7) weeks, days = divmod(days, 7)
# Formatting the output # Formatting the output
parts = [] parts = [
if months > 0: plural(value, unit)
parts.append(f"{months} month{'s' if months > 1 else ''}") for value, unit in ((months, "month"), (weeks, "week"), (days, "days"))
if weeks > 0: if value > 0
parts.append(f"{weeks} week{'s' if weeks > 1 else ''}") ]
if days > 0:
parts.append(f"{days} day{'s' if days > 1 else ''}")
return " ".join(parts) if parts else None return " ".join(parts) if parts else None