Show playtime total on session list
All checks were successful
continuous-integration/drone/push Build is passing

Fixes #6
Fixes #25
This commit is contained in:
2023-01-09 18:57:22 +01:00
parent 24f4459318
commit b77089f7ad
9 changed files with 104 additions and 58 deletions

View File

@ -8,8 +8,17 @@ def now() -> datetime:
return datetime.now(ZoneInfo(settings.TIME_ZONE))
def _safe_timedelta(duration: timedelta | int | None):
if duration == None:
return timedelta(0)
elif isinstance(duration, int):
return timedelta(seconds=duration)
elif isinstance(duration, timedelta):
return duration
def format_duration(
duration: timedelta | None, format_string: str = "%H hours %m minutes"
duration: timedelta | int | None, format_string: str = "%H hours %m minutes"
) -> str:
"""
Format timedelta into the specified format_string.
@ -22,11 +31,8 @@ def format_duration(
minute_seconds = 60
hour_seconds = 60 * minute_seconds
day_seconds = 24 * hour_seconds
if not isinstance(duration, timedelta):
if duration == None:
duration = timedelta(seconds=0)
else:
duration = timedelta(seconds=duration)
duration = _safe_timedelta(duration)
# we don't need float
seconds_total = int(duration.total_seconds())
# timestamps where end is before start
if seconds_total < 0: