Set up tests, add tests for common.util.time, add %d
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -13,32 +13,27 @@ def format_duration(
|
||||
) -> str:
|
||||
"""
|
||||
Format timedelta into the specified format_string.
|
||||
If duration is less than 60 seconds, skips formatting, returns "less than a minute".
|
||||
If duration is 0, skips formatting, returns 0.
|
||||
Valid format variables:
|
||||
- %H hours
|
||||
- %m minutes
|
||||
- %s seconds
|
||||
- %r total seconds
|
||||
"""
|
||||
seconds_in_hours = 3600
|
||||
seconds_in_minute = 60
|
||||
hours: int
|
||||
minutes: int
|
||||
seconds: int
|
||||
seconds_total: int
|
||||
remainder: int
|
||||
seconds_total = duration.total_seconds()
|
||||
if seconds_total == 0:
|
||||
return 0
|
||||
else:
|
||||
hours = int(seconds_total // seconds_in_hours)
|
||||
remainder = int(seconds_total % seconds_in_hours)
|
||||
minutes = int(remainder // seconds_in_minute)
|
||||
if hours == 0 and minutes == 0:
|
||||
return "less than a minute"
|
||||
seconds = int(minutes % seconds_in_minute)
|
||||
literals = {"%H": str(hours), "%m": str(minutes), "%s": str(seconds)}
|
||||
formatted_string = format_string
|
||||
for pattern, replacement in literals.items():
|
||||
formatted_string = re.sub(pattern, replacement, formatted_string)
|
||||
return formatted_string
|
||||
minute_seconds = 60
|
||||
hour_seconds = 60 * minute_seconds
|
||||
day_seconds = 24 * hour_seconds
|
||||
seconds_total = int(duration.total_seconds())
|
||||
days, remainder = divmod(seconds_total, day_seconds)
|
||||
hours, remainder = divmod(remainder, hour_seconds)
|
||||
minutes, seconds = divmod(remainder, minute_seconds)
|
||||
literals = {
|
||||
"%d": str(days),
|
||||
"%H": str(hours),
|
||||
"%m": str(minutes),
|
||||
"%s": str(seconds),
|
||||
"%r": str(seconds_total),
|
||||
}
|
||||
formatted_string = format_string
|
||||
for pattern, replacement in literals.items():
|
||||
formatted_string = re.sub(pattern, replacement, formatted_string)
|
||||
return formatted_string
|
||||
|
Reference in New Issue
Block a user