From 3c946e02e59a05ea0b04aa6c95ce38d9d1f8ae30 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 9 May 2020 13:37:08 +0200 Subject: Use duration format in form --- src/gui/tasks/duration.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/gui/tasks/duration.py (limited to 'src/gui/tasks/duration.py') diff --git a/src/gui/tasks/duration.py b/src/gui/tasks/duration.py new file mode 100644 index 0000000..d1b7106 --- /dev/null +++ b/src/gui/tasks/duration.py @@ -0,0 +1,31 @@ +import re +from typing import Optional +import math + +def format(minutes: int): + if minutes >= 60 * 24: + return '' + str(math.floor(minutes / 60 / 24)) + 'd' + elif minutes >= 60: + return '' + str(math.floor(minutes / 60)) + 'h' + elif minutes > 0: + return '' + str(minutes) + 'm' + else: + return '' + +def parse(duration: str) -> Optional[int]: + duration = duration.strip() + if duration: + result = re.match('^(\d+)([mhd])$', duration.strip()) + if result: + n = int(result.group(1)) + unit = result.group(2) + if unit == 'm': + return n + elif unit == 'h': + return n * 60 + elif unit == 'd': + return n * 60 * 24 + else: + return None + else: + return 0 -- cgit v1.2.3