better totaltimetosec
This commit is contained in:
@@ -428,32 +428,77 @@ def wavg(group, avg_name, weight_name):
|
||||
except ZeroDivisionError: # pragma: no cover
|
||||
return d.mean()
|
||||
|
||||
from string import Formatter
|
||||
|
||||
def totaltime_sec_to_string(totaltime, shorten=False):
|
||||
if np.isnan(totaltime):
|
||||
return ''
|
||||
|
||||
fmt = '{H:02}:{M:02}:{S:02}.{t}'
|
||||
if shorten:
|
||||
fmt = '{H}:{M:02}:{S:02}'
|
||||
if totaltime < 3600:
|
||||
fmt = '{M}:{S:02}'
|
||||
|
||||
remainder = int(totaltime)
|
||||
tenths = totaltime-remainder
|
||||
|
||||
f = Formatter()
|
||||
desired_fields = [field_tuple[1] for field_tuple in f.parse(fmt)]
|
||||
|
||||
possible_fields = ('D','H', 'M', 'S')
|
||||
constants = {'D':86400, 'H': 3600, 'M': 60, 'S': 1}
|
||||
values = {}
|
||||
for field in possible_fields:
|
||||
if field in desired_fields and field in constants:
|
||||
values[field], remainder = divmod(remainder, constants[field])
|
||||
|
||||
values['t'] = round(10*tenths)
|
||||
|
||||
return f.format(fmt, **values)
|
||||
|
||||
|
||||
def totaltime_sec_to_string_old(totaltime, shorten=False):
|
||||
if np.isnan(totaltime):
|
||||
return ''
|
||||
hours = int(totaltime / 3600.)
|
||||
if hours > 23: # pragma: no cover
|
||||
message = 'Warning: The workout duration was longer than 23 hours. '
|
||||
hours = 23
|
||||
elif hours < 0:
|
||||
message = 'Warning: invalid workout duration'
|
||||
hours = 0
|
||||
|
||||
minutes = int((totaltime - 3600. * hours) / 60.)
|
||||
if minutes > 59: # pragma: no cover
|
||||
minutes = 59
|
||||
if not message: # pragma: no cover
|
||||
message = 'Warning: there is something wrong with the workout duration'
|
||||
elif minutes < 0:
|
||||
minutes = 0
|
||||
if not message:
|
||||
message = 'Warning: invalid workout duration'
|
||||
|
||||
seconds = int(totaltime - 3600. * hours - 60. * minutes)
|
||||
if seconds > 59: # pragma: no cover
|
||||
seconds = 59
|
||||
if not message: # pragma: no cover
|
||||
message = 'Warning: there is something wrong with the workout duration'
|
||||
elif seconds < 0:
|
||||
seconds = 0
|
||||
if not message:
|
||||
message = 'Warning: invalid workout duration'
|
||||
|
||||
tenths = int(10 * (totaltime - 3600. * hours - 60. * minutes - seconds))
|
||||
|
||||
tenths = round(10 * (totaltime - 3600. * hours - 60. * minutes - seconds))
|
||||
if tenths > 9: # pragma: no cover
|
||||
tenths = 9
|
||||
if not message: # pragma: no cover
|
||||
message = 'Warning: there is something wrong with the workout duration'
|
||||
elif tenths < 0:
|
||||
tenths = 0
|
||||
if not message:
|
||||
message = 'Warning: invalid workout duration'
|
||||
|
||||
duration = ""
|
||||
if not shorten:
|
||||
|
||||
Reference in New Issue
Block a user