OokamiPupV2/modules/utility.py

34 lines
1.2 KiB
Python

import time
def format_uptime(seconds: float) -> str:
"""
Convert seconds into a human-readable string:
e.g. 32 minutes, 8 days, 8 months, etc.
"""
# Convert float seconds to an integer
seconds = int(seconds)
# We'll break it down
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
# For months and years, let's approximate 30 days per month,
# 365 days per year, etc. (just a rough approach)
months, days = divmod(days, 30)
years, months = divmod(months, 12)
# Build a short string, only listing the largest units
# If you want more detail, you can keep going
if years > 0:
return [f"{years} year(s), {months} month(s)", seconds]
elif months > 0:
return [f"{months} month(s), {days} day(s)", seconds]
elif days > 0:
return [f"{days} day(s), {hours} hour(s)", seconds]
elif hours > 0:
return [f"{hours} hour(s), {minutes} minute(s)", seconds]
elif minutes > 0:
return [f"{minutes} minute(s)", seconds]
else:
return [f"{seconds} second(s)", seconds]