cromulant/utils.py

49 lines
1.1 KiB
Python

import time
def now() -> float:
return int(time.time())
def singular_or_plural(num: float, singular: str, plural: str) -> str:
if num == 1:
return singular
return plural
def time_ago(start_time: float, end_time: float) -> str:
diff = end_time - start_time
seconds = int(diff)
if seconds < 60:
word = singular_or_plural(seconds, "second", "seconds")
return f"{seconds} {word} ago"
minutes = seconds // 60
if minutes < 60:
word = singular_or_plural(minutes, "minute", "minutes")
return f"{minutes} {word} ago"
hours = minutes / 60
if hours < 24:
word = singular_or_plural(hours, "hour", "hours")
return f"{hours:.1f} {word} ago"
days = hours / 24
if days < 30:
word = singular_or_plural(days, "day", "days")
return f"{days:.1f} {word} ago"
months = days / 30
if months < 12:
word = singular_or_plural(months, "month", "months")
return f"{months:.1f} {word} ago"
years = months / 12
word = singular_or_plural(years, "year", "years")
return f"{years:.1f} {word} ago"