From bc201b9da42329ca2b122af0c7335b4ac0d67940 Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Sat, 17 Sep 2016 20:44:13 -0400 Subject: [PATCH] wow, more nice new scripts. time tracker lets different activities be tracked for duration, keeping them separate by name. the test for it just tries a random time duration and prints it out. other tests got cleaned or renamed too. --- scripts/system/time_tracker.sh | 43 +++++++++++++++++++ ...ay_sifter_test.sh => test_array_sifter.sh} | 0 scripts/unit_test/test_assoc_array.sh | 2 +- scripts/unit_test/test_time_tracker.sh | 18 ++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 scripts/system/time_tracker.sh rename scripts/unit_test/{array_sifter_test.sh => test_array_sifter.sh} (100%) create mode 100644 scripts/unit_test/test_time_tracker.sh diff --git a/scripts/system/time_tracker.sh b/scripts/system/time_tracker.sh new file mode 100644 index 00000000..15e30db7 --- /dev/null +++ b/scripts/system/time_tracker.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# some helpful functions for counting a named duration. + +# start tracking time for a named purpose, passed as the first parameter. +function start_time_tracking() +{ + local varname="$1"; shift + if [ -z "$varname" ]; then echo must pass variable name to start_time_tracking; return 1; fi + local startvar="${varname}_START" + eval $startvar="$(date +"%s")" +} + +# stop tracking the named time duration. +function end_time_tracking() +{ + local varname="$1"; shift + if [ -z "$varname" ]; then echo must pass variable name to end_time_tracking; return 1; fi + local endvar="${varname}_END" + eval $endvar="$(date +"%s")" +} + +# display the time taken for a named duration. +function show_tracked_duration() +{ + varname="$1"; shift + if [ -z "$varname" ]; then echo must pass variable name to end_time_tracking; return 1; fi + # calculate the time taken. + local startvar="${varname}_START" + local endvar="${varname}_END" + duration="$((${!endvar} - ${!startvar}))" + # separate it into hours and minutes. + minutes="$(($duration / 60))" + hours="$(($duration / 60))" + # subtract the hours from the minutes sum for modulo. + minutes="$(($minutes - $hours * 60))" + # fashion conceit, add zeroes. + if (($minutes < 10)); then minutes="0$minutes"; fi + if (($hours < 10)); then hours="0$hours"; fi + echo "Time taken for ${varname}: ${hours}:${minutes} hh:mm (a total of $duration seconds)" +} + + diff --git a/scripts/unit_test/array_sifter_test.sh b/scripts/unit_test/test_array_sifter.sh similarity index 100% rename from scripts/unit_test/array_sifter_test.sh rename to scripts/unit_test/test_array_sifter.sh diff --git a/scripts/unit_test/test_assoc_array.sh b/scripts/unit_test/test_assoc_array.sh index 49cf6ecc..8da04248 100644 --- a/scripts/unit_test/test_assoc_array.sh +++ b/scripts/unit_test/test_assoc_array.sh @@ -7,7 +7,7 @@ declare -A snuggles # make an associative array -snuggles=([book]=petunia "[muffets]=glasgow robbery") +snuggles=([book]=petunia [muffets]="glasgow robbery") # keys: book and muffets # values: (second part) diff --git a/scripts/unit_test/test_time_tracker.sh b/scripts/unit_test/test_time_tracker.sh new file mode 100644 index 00000000..9a908578 --- /dev/null +++ b/scripts/unit_test/test_time_tracker.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# tests out the time tracking methods. + +source $FEISTY_MEOW_SCRIPTS/system/time_tracker.sh + +echo testing time tracking... +start_time_tracking crungle + +sleep $(($RANDOM / 1000)) +sleep 2 + +end_time_tracking crungle + +echo ...done testing time tracking. + +show_tracked_duration crungle + -- 2.34.1