File: difftime.py

package info (click to toggle)
gtimelog 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,008 kB
  • sloc: python: 4,530; xml: 171; makefile: 93
file content (34 lines) | stat: -rwxr-xr-x 728 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python
import readline  # noqa


def parse_time(s):
    h, m = map(int, s.strip().split(':'))
    return h * 60 + m


def fmt_delta(mins):
    sign = mins < 0 and "-" or ""
    mins = abs(mins)
    if mins >= 60:
        h = mins / 60
        m = mins % 60
        return "%s%d min (%d hr, %d min)" % (sign, mins, h, m)
    else:
        return "%s%d min" % (sign, mins)


while True:
    try:
        what = raw_input("start, end> ")
    except EOFError:
        print
        break
    try:
        if ',' in what:
            t1, t2 = map(parse_time, what.split(','))
        else:
            t1, t2 = map(parse_time, what.split())
        print fmt_delta(t2 - t1)
    except ValueError:
        print eval(what)