From d252be0b18fff272f09701a21d5967fd43cbda7d Mon Sep 17 00:00:00 2001 From: Nick Chambers Date: Sun, 15 May 2022 13:27:46 -0500 Subject: [PATCH] Turn seconds into a human-readable timestamp --- bash/sheconds | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 bash/sheconds diff --git a/bash/sheconds b/bash/sheconds new file mode 100755 index 0000000..b69fc3c --- /dev/null +++ b/bash/sheconds @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# The variables are checked dynamically +# shellcheck disable=SC2034 +seconds() { + local day hour minute second span time + (( day = $1 / 60 / 60 / 24 )) + (( hour = $1 / 60 / 60 % 24 )) + (( minute = $1 / 60 % 60 )) + (( second = $1 % 60 )) + + for span in day hour minute second; do + if (( ${!span} )); then + if [[ $time ]]; then + time+=", " + fi + + time+="${!span} $span" + + if (( ${!span} > 1 )); then + time+=s + fi + fi + done + + printf %s "$time" + + if [[ -t 1 ]]; then + printf '\n' + fi +} + +seconds "${1-3628800}"