From eb48a2254b480764e1ab427f98a13262b3479002 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 7 Dec 2016 23:49:30 +1300 Subject: Add sec(1df) Might extend this later to do stuff like 1w3d2m0s --- .gitignore | 1 + Makefile | 6 ++++-- README.markdown | 1 + bin/sec.awk | 42 ++++++++++++++++++++++++++++++++++++++++++ man/man1/sec.1df | 21 +++++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 bin/sec.awk create mode 100644 man/man1/sec.1df diff --git a/.gitignore b/.gitignore index 7bd19d59..b0f351f0 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ bin/mode bin/rfct bin/rndi bin/sd2u +bin/sec bin/slsf bin/su2d bin/tot diff --git a/Makefile b/Makefile index 3f48b8d9..ee70f91a 100644 --- a/Makefile +++ b/Makefile @@ -77,6 +77,7 @@ all : bin/csmw \ bin/rfct \ bin/rndi \ bin/sd2u \ + bin/sec \ bin/slsf \ bin/su2d \ bin/tot \ @@ -97,6 +98,7 @@ clean distclean : bin/rfct \ bin/rndi \ bin/sd2u \ + bin/sec \ bin/slsf \ bin/su2d \ bin/tot \ @@ -188,8 +190,8 @@ install-bash-completion : install-bash install -pm 0644 -- bash/bash_completion.d/* "$(HOME)"/.bash_completion.d install-bin : bin/csmw bin/ddup bin/gwp bin/han bin/mean bin/med bin/mftl \ - bin/mode bin/rfct bin/rndi bin/sd2u bin/slsf bin/su2d bin/tot bin/unf \ - install-bin-man + bin/mode bin/rfct bin/rndi bin/sd2u bin/sec bin/slsf bin/su2d bin/tot \ + bin/unf install-bin-man install -m 0755 -d -- "$(HOME)"/.local/bin for name in bin/* ; do \ [ -x "$$name" ] || continue ; \ diff --git a/README.markdown b/README.markdown index b555d65d..153317ea 100644 --- a/README.markdown +++ b/README.markdown @@ -473,6 +473,7 @@ Installed by the `install-bin` target: * `rgl(1df)` is a very crude interactive `grep(1)` loop. * `shb(1df)` attempts to build shebang lines for scripts from the system paths. +* `sec(1df)` converts `hh:mm:ss` or `mm:ss` timestamps to seconds * `spr(1df)` posts its input to the sprunge.us pastebin. * `sqs(1df)` chops off query strings from filenames, usually downloads. * `sshi(1df)` prints human-readable SSH connection details. 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) +} diff --git a/man/man1/sec.1df b/man/man1/sec.1df new file mode 100644 index 00000000..589b6a74 --- /dev/null +++ b/man/man1/sec.1df @@ -0,0 +1,21 @@ +.TH SEC 1df "December 2016" "Manual page for sec" +.SH NAME +.B sec +\- convert colon-delimited durations to seconds +.SH USAGE +.B sec +FILE1 [FILE2 ...] +.br +.B sec +< FILE +.br +printf '1:02:54\\n' | +.br +sec=$(printf '%s\n' "$timestamp" | sec) +.B sec +.SH DESCRIPTION +Applies awk(1) to convert hh:mm:ss or mm:ss timestamps into a count of seconds. +Exits zero if all lines were successfully recognised and converted, non-zero +otherwise. +.SH AUTHOR +Tom Ryder -- cgit v1.2.3