From b9b5a431422c4213fe555e284fcfaa96cd692267 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 5 Feb 2017 02:17:12 +1300 Subject: Add hms(1df) --- .gitignore | 1 + Makefile | 1 + README.markdown | 4 +++- bin/hms.awk | 31 +++++++++++++++++++++++++++++++ man/man1/hms.1df | 23 +++++++++++++++++++++++ man/man1/sec.1df | 2 ++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 bin/hms.awk create mode 100644 man/man1/hms.1df diff --git a/.gitignore b/.gitignore index 22e40356..8ccf6b5f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ bin/ddup bin/gwp bin/jfp bin/han +bin/hms bin/htdec bin/htenc bin/htref diff --git a/Makefile b/Makefile index ae1655fa..6a63dbb7 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,7 @@ BINS = bin/brnl \ bin/ddup \ bin/gwp \ bin/han \ + bin/hms \ bin/htdec \ bin/htenc \ bin/htref \ diff --git a/README.markdown b/README.markdown index f2ef003e..586594a4 100644 --- a/README.markdown +++ b/README.markdown @@ -424,6 +424,9 @@ Installed by the `install-bin` target: * `jfc(1df)` adds and commits lazily to a Git repository. * `jfcd(1df)` watches a directory for changes and runs `jfc(1df)` if it sees any. +* Two time duration functions: + * `hms(1df)` converts seconds to `hh:mm:ss` or `mm:ss` timestamps. + * `sec(1df)` converts `hh:mm:ss` or `mm:ss` timestamps to seconds. * `ap(1df)` reads arguments for a given command from the standard input, prompting if appropriate. * `apf(1df)` prepends arguments to a command with ones read from a file, @@ -493,7 +496,6 @@ 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. * `sqs(1df)` chops off query strings from filenames, usually downloads. * `sshi(1df)` prints human-readable SSH connection details. * `stex(1df)` strips extensions from filenames. diff --git a/bin/hms.awk b/bin/hms.awk new file mode 100644 index 00000000..3a9a1499 --- /dev/null +++ b/bin/hms.awk @@ -0,0 +1,31 @@ +BEGIN { + OFS = ":" +} + +# Refuse to deal with anything that's not a positive (unsigned) integer +/[^0-9]/ { + print "hms: Bad number" | "cat >&2" + err = 1 + next +} + +# Integer looks valid +{ + # Break it down into hours, minutes, and seconds + s = $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 { exit(err > 0) } diff --git a/man/man1/hms.1df b/man/man1/hms.1df new file mode 100644 index 00000000..5c1a287c --- /dev/null +++ b/man/man1/hms.1df @@ -0,0 +1,23 @@ +.TH HMS 1df "February 2017" "Manual page for hms" +.SH NAME +.B hms +\- convert seconds to colon-delimited durations +.SH USAGE +.B hms +FILE1 [FILE2 ...] +.br +.B hms +< FILE +.br +printf '1:02:54\\n' | +.B hms +.br +hms=$(printf '%s\\n' "$seconds" | hms) +.SH DESCRIPTION +Applies awk(1) to convert counts of seconds into hh:mm:ss or mm:ss timestamps. +Exits zero if all lines were successfully recognised and converted, non-zero +otherwise. +.SH SEE ALSO +sec(1df) +.SH AUTHOR +Tom Ryder diff --git a/man/man1/sec.1df b/man/man1/sec.1df index aad09ddd..d0011b38 100644 --- a/man/man1/sec.1df +++ b/man/man1/sec.1df @@ -17,5 +17,7 @@ sec=$(printf '%s\\n' "$timestamp" | sec) 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 SEE ALSO +hms(1df) .SH AUTHOR Tom Ryder -- cgit v1.2.3