Reimplement the convert_time_unit function to make it more human-readable

This commit is contained in:
KnugiHK
2025-05-17 17:35:30 +08:00
parent 8c2868a60e
commit d7ded16239

View File

@@ -44,23 +44,31 @@ def convert_time_unit(time_second: int) -> str:
Returns: Returns:
str: A human-readable string representing the time duration. str: A human-readable string representing the time duration.
""" """
time = str(timedelta(seconds=time_second)) if time_second < 1:
if "day" not in time: return "less than a second"
if time_second < 1: elif time_second == 1:
time = "less than a second" return "a second"
elif time_second == 1:
time = "a second" delta = timedelta(seconds=time_second)
elif time_second < 60: parts = []
time = time[5:][1 if time_second < 10 else 0:] + " seconds"
elif time_second == 60: days = delta.days
time = "a minute" if days > 0:
elif time_second < 3600: parts.append(f"{days} day{'s' if days > 1 else ''}")
time = time[2:] + " minutes"
elif time_second == 3600: hours = delta.seconds // 3600
time = "an hour" if hours > 0:
else: parts.append(f"{hours} hour{'s' if hours > 1 else ''}")
time += " hour"
return time minutes = (delta.seconds % 3600) // 60
if minutes > 0:
parts.append(f"{minutes} minute{'s' if minutes > 1 else ''}")
seconds = delta.seconds % 60
if seconds > 0:
parts.append(f"{seconds} second{'s' if seconds > 1 else ''}")
return " ".join(parts)
def bytes_to_readable(size_bytes: int) -> str: def bytes_to_readable(size_bytes: int) -> str: