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

# Separator is :
BEGIN {
    FS = ":"
}

# If no fields or illegal characters, warn, skip line, accrue errors
!NF || /[^0-9:]/ {
    print "sec: Bad format" > "/dev/stderr"
    err = 1
    next
}

# Strip leading zeroes to stop awk trying to be octal
{
    for (i = 1; i <= NF; i++)
        sub(/^0*/, "", $i)
}

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

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

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

# Done, exit 1 if we had any errors on the way
END {
    exit(err > 0)
}