From 5b7b8523d6565056fdcc06964f379a06e3be01e7 Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Sat, 29 Dec 2012 07:46:31 -0600 Subject: [PATCH] sh/stopwatch: rename and expose stopwatch_humany_duration as unstable API --- lib/sh/stopwatch.sh | 20 +++++++++++++------- test/stopwatch.sh | 14 +++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/sh/stopwatch.sh b/lib/sh/stopwatch.sh index 1ba1ce8..59e6785 100644 --- a/lib/sh/stopwatch.sh +++ b/lib/sh/stopwatch.sh @@ -68,6 +68,12 @@ Stop a stopwatch; can be resumed later with 'start' Reset a stopwatch back to zero stopwatch reset +Internal functions available: + +stopwatch_humany_duration + Transform an integer number of microseconds into a human-readable string + (Ex: 3d14h10m3.223s or 7m10.013s ) + EOF kill -ABRT $$ } @@ -90,8 +96,8 @@ EOF esac # Try and format a passed integer number of seconds into something more - # useful (like 6m10s or something) - _stopwatch_timefmt() { + # easily parseable by a human (like 6m10s or something) + stopwatch_humany_duration() { local usec secs mins hurs days [ "${1}" -a "$(echo "${1}" | tr -Cd '0-9')" = "${1}" ] || return 255 usec=$(( ${1} % 1000000 )) @@ -129,7 +135,7 @@ EOF # start the stopwatch accum="$(kvs_get stopwatch "${name}_accumulator")" kvs_set stopwatch "${name}" "${nao}" - [ "${accum}" ] && restart="$(printf " at %s" "$(_stopwatch_timefmt "${accum}")")" + [ "${accum}" ] && restart="$(printf " at %s" "$(stopwatch_humany_duration "${accum}")")" printf "Stopwatch: '%s' starts%s.\n" "${name}" "${restart}" fi } @@ -147,10 +153,10 @@ EOF accum="$(kvs_get stopwatch "${name}_accumulator")" delta="$(( ${nao} - ${start} ))" [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))" - printf "Stopwatch: '%s' is running: %s.\n" "${name}" "$(_stopwatch_timefmt "${delta}")" + printf "Stopwatch: '%s' is running: %s.\n" "${name}" "$(stopwatch_humany_duration "${delta}")" else accum="$(kvs_get stopwatch "${name}_accumulator")" - [ "${accum}" ] && printf "Stopwatch: '%s' is stopped: %s.\n" "${name}" "$(_stopwatch_timefmt "${accum}")" + [ "${accum}" ] && printf "Stopwatch: '%s' is stopped: %s.\n" "${name}" "$(stopwatch_humany_duration "${accum}")" fi } @@ -167,10 +173,10 @@ EOF [ "${accum}" ] && delta="$(( ${delta} + ${accum} ))" kvs_set stopwatch "${name}_accumulator" "${delta}" kvs_unset stopwatch "${name}" - printf "Stopwatch: '%s' stops at %s.\n" "${name}" "$(_stopwatch_timefmt "${delta}")" + printf "Stopwatch: '%s' stops at %s.\n" "${name}" "$(stopwatch_humany_duration "${delta}")" else accum="$(kvs_get stopwatch "${name}_accumulator")" - [ "${accum}" ] && printf "Stopwatch: '%s' is stopped at %s.\n" "${name}" "$(_stopwatch_timefmt "${accum}")" + [ "${accum}" ] && printf "Stopwatch: '%s' is stopped at %s.\n" "${name}" "$(stopwatch_humany_duration "${accum}")" fi } diff --git a/test/stopwatch.sh b/test/stopwatch.sh index 1eb242a..992c6d9 100644 --- a/test/stopwatch.sh +++ b/test/stopwatch.sh @@ -4,25 +4,25 @@ _root="$(readlink -f "$(dirname "${0}")/../")"; . "${_root}/lib/sh/env.sh" want stopwatch stopwatch test start -_stopwatch_timefmt 10000000 +stopwatch_humany_duration 10000000 echo stopwatch test lap -_stopwatch_timefmt 30000000 +stopwatch_humany_duration 30000000 echo stopwatch test lap -_stopwatch_timefmt 59000000 +stopwatch_humany_duration 59000000 echo stopwatch test lap -_stopwatch_timefmt 60000000 +stopwatch_humany_duration 60000000 echo stopwatch test lap -_stopwatch_timefmt 61000000 +stopwatch_humany_duration 61000000 echo stopwatch test lap -_stopwatch_timefmt 86399000000 +stopwatch_humany_duration 86399000000 echo stopwatch test lap -_stopwatch_timefmt 99999999000000 +stopwatch_humany_duration 99999999000000 echo stopwatch test stop stopwatch test reset -- 2.45.0