Given an integer number of seconds, return a string which represents in human readable format the number of days/hours/minutes/seconds that are equivalent.
from datetime import datetime, timedelta
def GetTime(sec):
d = datetime(1,1,1) + timedelta(seconds=sec)
days = (d.day-1,'d',' ') if (d.day-1 > 0) else ('','','')
hours = (d.hour, 'h', ' ') if (d.hour > 0) else ('', '', '')
mins = (d.minute, 'm', ' ') if (d.minute > 0) else ('', '', '')
secs = (d.second, 's', ' ') if (d.second > 0) else ('', '', '')
time_format = days + hours + mins + secs
format_str = '{}'*len(time_format)
return format_str.format(*time_format)
I think there might be a better way to do the formatting to omit values that are zero instead of combining tuples and exploding them in the format string.