convert timedelta to readable string

This commit is contained in:
Stefan Harmuth 2021-12-06 05:56:26 +01:00
parent d757a82d80
commit 57ba56bf2b

View File

@ -1,3 +1,4 @@
import datetime
import inspect import inspect
import os.path import os.path
import sys import sys
@ -23,3 +24,21 @@ def get_script_dir(follow_symlinks=True):
def compare(a: Any, b: Any) -> int: def compare(a: Any, b: Any) -> int:
"""compare to values, return -1 if a is smaller than b, 1 if a is greater than b, 0 is both are equal""" """compare to values, return -1 if a is smaller than b, 1 if a is greater than b, 0 is both are equal"""
return bool(a > b) - bool(a < b) return bool(a > b) - bool(a < b)
def human_readable_time_from_delta(delta: datetime.timedelta) -> str:
time_str = ""
if delta.days > 0:
time_str += "%d days, " % delta.days
if delta.seconds > 3600:
time_str += "%02d:" % (delta.seconds // 3600)
else:
time_str += "00:"
if delta.seconds % 3600 > 60:
time_str += "%02d:" % (delta.seconds % 3600 // 60)
else:
time_str += "00:"
return time_str + "%02d" % (delta.seconds % 60)