summaryrefslogtreecommitdiffstats
path: root/dash/prettytime.py
blob: 3cb47b4c05457aa9aa1593e1a24e05982a149603 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python3

def prettytime(seconds, prec = 2):
    seconds = int(seconds)
    output = []

    for div, unit in [(365*86400, 'year'), (30*86400, 'month'), (86400, 'day'), (3600, 'hour'), (60, 'minute'), (1, 'second')]:
        time = seconds // div
        seconds = seconds % div
        if time > 0:
            if time > 1:
                output.append('{} {}s'.format(time, unit))
            else:
                output.append('{} {}'.format(time, unit))

    if len(output) == 0:
        output.append('0 seconds')

    return ', '.join(output[:prec])