Refactor
This commit is contained in:
parent
1caf233075
commit
42066f9dde
|
@ -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:
|
||||
"""
|
||||
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)
|
||||
|
||||
# Formatting the output
|
||||
parts = []
|
||||
if months > 0:
|
||||
parts.append(f"{months} month{'s' if months > 1 else ''}")
|
||||
if weeks > 0:
|
||||
parts.append(f"{weeks} week{'s' if weeks > 1 else ''}")
|
||||
if days > 0:
|
||||
parts.append(f"{days} day{'s' if days > 1 else ''}")
|
||||
|
||||
parts = [
|
||||
plural(value, unit)
|
||||
for value, unit in ((months, "month"), (weeks, "week"), (days, "days"))
|
||||
if value > 0
|
||||
]
|
||||
return " ".join(parts) if parts else None
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue