aboutsummaryrefslogtreecommitdiff
path: root/bin/hms.awk
blob: 2aa492a15a0f08e273b5e9b7f1144cdcc98aa26a (plain) (blame)
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
# Convert seconds to colon-delimited durations
BEGIN {
    OFS = ":"
    ex = 0
    stderr = ""
}

# Refuse to deal with anything that's not a positive (unsigned) integer
/[^0-9]/ {
    if (!stderr)
        stderr = "cat >&2"
    print "hms: Bad number" | stderr
    ex = 1
    next
}

# Integer looks valid
{
    # Break it down into hours, minutes, and seconds
    s = int($0 + 0)
    h = int(s / 3600)
    s %= 3600
    m = int(s / 60)
    s %= 60

    # Print it, with the biggest number without a leading zero
    if (h)
        printf "%u:%02u:%02u\n", h, m, s
    else if (m)
        printf "%u:%02u\n", m, s
    else
        printf "%u\n", s
}

# Done, exit 1 if we had any errors on the way
END {
    if (stderr)
        close(stderr)
    exit(ex)
}