diff --git a/rowers/dataprep.py b/rowers/dataprep.py index a02c5ce3..9368c961 100644 --- a/rowers/dataprep.py +++ b/rowers/dataprep.py @@ -1188,7 +1188,10 @@ def save_workout_database(f2, r, dosmooth=True, workouttype='rower', if workouttype in otetypes: dragfactor = row.dragfactor + t = datetime.datetime.strptime(duration, "%H:%M:%S.%f") + + delta = datetime.timedelta( hours=t.hour, minutes=t.minute, seconds=t.second) diff --git a/rowers/tests/test_unit_tests.py b/rowers/tests/test_unit_tests.py index 56cf7b56..1a8f6d27 100644 --- a/rowers/tests/test_unit_tests.py +++ b/rowers/tests/test_unit_tests.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals from .statements import * from rowers.mytypes import rowtypes -from rowers.utils import allmonths,allsundays +from rowers.utils import allmonths,allsundays, totaltime_sec_to_string from rowers.models import update_records from django.db import transaction @@ -94,6 +94,25 @@ class OtherUnitTests(TestCase): # time zone should be US/Pacific self.assertTrue('US/Pacific' in str(startdate.tzinfo)) + def test_totaltime_sec_to_string(self): + results = ( + (0,"0:00"), + (120,"2:00"), + (3634.2,"1:00:34"), + ) + + for duration, expected in results: + self.assertEqual(totaltime_sec_to_string(duration, shorten=True), expected) + + results = ( + (0,"00:00:00.0"), + (120,"00:02:00.0"), + (3634.2,"01:00:34.2"), + ) + + for duration, expected in results: + self.assertEqual(totaltime_sec_to_string(duration, shorten=False), expected) + def test_summaryfromsplitdata(self): diff --git a/rowers/utils.py b/rowers/utils.py index 4f70f43b..7572d7f5 100644 --- a/rowers/utils.py +++ b/rowers/utils.py @@ -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: