aboutsummaryrefslogtreecommitdiff
path: root/src/gui/tasks/duration.py
blob: 3ce82031e6fdaa905c422e8312bbac8225e52740 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from PyQt5 import QtGui
from typing import Optional
import math
import re

import gui.color

def format(minutes: int):
    if minutes >= 60 * 24:
        return '' + format_decimal(minutes / 60 / 24) + 'd'
    elif minutes >= 60:
        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+)(\.(\d+))?([mhd])$', duration.strip())
        if result:
            n = int(result.group(1))
            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 math.floor(num)
            elif unit == 'h':
                return math.floor(num * 60)
            elif unit == 'd':
                return math.floor(num * 60 * 24)
        else:
            return None
    else:
        return 0

def color(minutes: int):
    if minutes <= 15:
        return gui.color.short_duration
    elif minutes < 60:
        return gui.color.medium_duration
    else:
        return gui.color.long_duration