diff options
author | Joris | 2020-05-10 15:10:13 +0200 |
---|---|---|
committer | Joris | 2020-05-10 15:10:13 +0200 |
commit | 80ceab3620cc09b10612991ac982ea42745c2a07 (patch) | |
tree | fa3397cb9c31ca55b0f95fc87efd6d115eb586c2 /src/gui/tasks | |
parent | 2b479fae26b7446f9269223d1e1fb4073839f5cc (diff) |
Allows decimals in durations
Diffstat (limited to 'src/gui/tasks')
-rw-r--r-- | src/gui/tasks/duration.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/gui/tasks/duration.py b/src/gui/tasks/duration.py index d1b7106..e863914 100644 --- a/src/gui/tasks/duration.py +++ b/src/gui/tasks/duration.py @@ -4,27 +4,35 @@ import math def format(minutes: int): if minutes >= 60 * 24: - return '' + str(math.floor(minutes / 60 / 24)) + 'd' + return '' + format_decimal(minutes / 60 / 24) + 'd' elif minutes >= 60: - return '' + str(math.floor(minutes / 60)) + 'h' + return '' + format_decimal(minutes / 60) + 'h' elif minutes > 0: return '' + str(minutes) + 'm' else: return '' +def format_decimal(d: float) -> str: + return '{0:.2g}'.format(d) + def parse(duration: str) -> Optional[int]: duration = duration.strip() if duration: - result = re.match('^(\d+)([mhd])$', duration.strip()) + result = re.match('^(\d+)(\.(\d+))?([mhd])$', duration.strip()) if result: n = int(result.group(1)) - unit = result.group(2) + if result.group(3): + d = int(result.group(3)) * pow(10, -1 * len(result.group(3))) + else: + d = 0 + num = n + d + unit = result.group(4) if unit == 'm': - return n + return math.floor(num) elif unit == 'h': - return n * 60 + return math.floor(num * 60) elif unit == 'd': - return n * 60 * 24 + return math.floor(num * 60 * 24) else: return None else: |