improved uptime calculation with fewer lines

kami_dev
Kami 2025-02-02 00:14:55 +01:00
parent 5725439354
commit 29e48907df
1 changed files with 26 additions and 30 deletions

View File

@ -5,41 +5,37 @@ import json
DICTIONARY_PATH = "dictionary/" # Path to dictionary files DICTIONARY_PATH = "dictionary/" # Path to dictionary files
def format_uptime(seconds: float) -> str: def format_uptime(seconds: float) -> tuple[str, int]:
""" """
Convert seconds into a human-readable string: Convert seconds into a human-readable string:
e.g. 32 minutes, 8 days, 8 months, etc. - Example outputs:
Returns two values: "32 minutes"
1. Human-readable format "8 days, 4 hours"
2. Seconds since start "1 year, 3 months"
- Returns a tuple:
(Human-readable string, total seconds)
""" """
# Convert float seconds to an integer seconds = int(seconds) # Ensure integer seconds
seconds = int(seconds)
# We'll break it down # Define time units
minutes, seconds = divmod(seconds, 60) units = [
hours, minutes = divmod(minutes, 60) ("year", 31536000), # 365 days
days, hours = divmod(hours, 24) ("month", 2592000), # 30 days
# For months and years, let's approximate 30 days per month, ("day", 86400), # 24 hours
# 365 days per year, etc. (just a rough approach) ("hour", 3600), # 60 minutes
("minute", 60),
months, days = divmod(days, 30) ("second", 1)
years, months = divmod(months, 12) ]
# Build a short string, only listing the largest units # Compute time breakdown
# If you want more detail, you can keep going time_values = []
if years > 0: for unit_name, unit_seconds in units:
return [f"{years} year(s), {months} month(s)", seconds] value, seconds = divmod(seconds, unit_seconds)
elif months > 0: if value > 0:
return [f"{months} month(s), {days} day(s)", seconds] time_values.append(f"{value} {unit_name}{'s' if value > 1 else ''}") # Auto pluralize
elif days > 0:
return [f"{days} day(s), {hours} hour(s)", seconds] # Return only the **two most significant** time units (e.g., "3 days, 4 hours")
elif hours > 0: return (", ".join(time_values[:2]), seconds) if time_values else ("0 seconds", 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]
def get_random_reply(dictionary_name: str, category: str, **variables) -> str: def get_random_reply(dictionary_name: str, category: str, **variables) -> str:
""" """