aboutsummaryrefslogtreecommitdiff
path: root/src/gui/tasks/duration.py
blob: d1b710685af74e8376769e0765f3974343963d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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