diff options
author | Joris | 2020-05-09 13:37:08 +0200 |
---|---|---|
committer | Joris | 2020-05-09 13:37:08 +0200 |
commit | 3c946e02e59a05ea0b04aa6c95ce38d9d1f8ae30 (patch) | |
tree | a044b3d4a47f98722bec170d7519eaef49c10b36 /src/gui/tasks/duration.py | |
parent | 6ed4e669ef7cb857c7b0ac774c41e8f9c7758217 (diff) |
Use duration format in form
Diffstat (limited to 'src/gui/tasks/duration.py')
-rw-r--r-- | src/gui/tasks/duration.py | 31 |
1 files changed, 31 insertions, 0 deletions
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 |