aboutsummaryrefslogtreecommitdiff
path: root/bin/sec.awk
blob: 645df1472113423c6125c799a62d5a70d9352c55 (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
# Convert [[[hh:]mm:]ss] timestamps to seconds

# Separator is :, strip out leading zeroes
BEGIN {
    FS = ":0*"
    stderr = ""
    ex = 0
}

# If no fields, too many fields, or illegal characters, warn, skip line, accrue
# errors
!NF || NF > 3 || /[^0-9:]/ {
    if (!stderr)
        stderr = "cat >&2"
    print "sec: Bad format" | stderr
    ex = 1
    next
}

# Match hh:mm:ss
NF == 3 { printf "%u\n", $1 * 3600 + $2 * 60 + $3 }

# Match mm:ss
NF == 2 { printf "%u\n", $1 * 60 + $2 }

# Match ss (in which case all we've done is strip zeroes)
NF == 1 { printf "%u\n", $1 }

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