FitTrackee/fittrackee/workouts/utils/convert.py

28 lines
762 B
Python
Raw Normal View History

from datetime import timedelta
2021-01-02 19:28:03 +01:00
from typing import Optional, Union
2021-01-02 19:28:03 +01:00
def convert_in_duration(value: str) -> timedelta:
hours = int(value.split(':')[0])
minutes = int(value.split(':')[1])
2019-08-28 13:25:39 +02:00
return timedelta(seconds=(hours * 3600 + minutes * 60))
2021-01-02 19:28:03 +01:00
def convert_timedelta_to_integer(value: str) -> int:
hours, minutes, seconds = str(value).split(':')
return int(hours) * 3600 + int(minutes) * 60 + int(seconds)
2021-01-02 19:28:03 +01:00
def convert_value_to_integer(
record_type: str, val: Union[str, float]
) -> Optional[int]:
if val is None:
return None
if record_type == 'LD':
2021-01-02 19:28:03 +01:00
return convert_timedelta_to_integer(str(val))
elif record_type in ['AS', 'MS']:
return int(val * 100)
else: # 'FD'
return int(val * 1000)