Move timedelta_display() function to agenda.utils
This commit is contained in:
parent
15c5053e44
commit
b9adb3d15e
2 changed files with 22 additions and 20 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import os
|
||||
import typing
|
||||
from datetime import date, datetime, timezone
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from time import time
|
||||
|
||||
|
||||
|
|
@ -28,6 +28,20 @@ def as_datetime(d: datetime | date) -> datetime:
|
|||
raise TypeError(f"Unsupported type: {type(d)}")
|
||||
|
||||
|
||||
def timedelta_display(delta: timedelta) -> str:
|
||||
"""Format timedelta as a human readable string."""
|
||||
total_seconds = int(delta.total_seconds())
|
||||
days, remainder = divmod(total_seconds, 24 * 60 * 60)
|
||||
hours, remainder = divmod(remainder, 60 * 60)
|
||||
mins, secs = divmod(remainder, 60)
|
||||
|
||||
return " ".join(
|
||||
f"{v} {label}"
|
||||
for v, label in ((days, "days"), (hours, "hrs"), (mins, "mins"))
|
||||
if v
|
||||
)
|
||||
|
||||
|
||||
def human_readable_delta(future_date: date) -> str | None:
|
||||
"""
|
||||
Calculate the human-readable time delta for a given future date.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue