diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-07 23:49:30 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-07 23:49:30 +1300 |
commit | eb48a2254b480764e1ab427f98a13262b3479002 (patch) | |
tree | 5586803ae0154e548dc284aae8b42e1b2d5961aa /bin/sec.awk | |
parent | Prevent getopts call in tree() panicking (diff) | |
download | dotfiles-eb48a2254b480764e1ab427f98a13262b3479002.tar.gz dotfiles-eb48a2254b480764e1ab427f98a13262b3479002.zip |
Add sec(1df)
Might extend this later to do stuff like 1w3d2m0s
Diffstat (limited to 'bin/sec.awk')
-rw-r--r-- | bin/sec.awk | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/bin/sec.awk b/bin/sec.awk new file mode 100644 index 00000000..aab8fcf4 --- /dev/null +++ b/bin/sec.awk @@ -0,0 +1,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) +} |