3 # some helpful functions for counting a named duration.
5 # start tracking time for a named purpose, passed as the first parameter.
6 function start_time_tracking()
8 local varname="$1"; shift
9 if [ -z "$varname" ]; then echo must pass variable name to start_time_tracking; return 1; fi
10 local startvar="${varname}_START"
11 eval $startvar="$(date +"%s")"
14 # stop tracking the named time duration.
15 function end_time_tracking()
17 local varname="$1"; shift
18 if [ -z "$varname" ]; then echo must pass variable name to end_time_tracking; return 1; fi
19 local endvar="${varname}_END"
20 eval $endvar="$(date +"%s")"
23 # display the time taken for a named duration.
24 function show_tracked_duration()
27 if [ -z "$varname" ]; then echo must pass variable name to end_time_tracking; return 1; fi
28 # calculate the time taken.
29 local startvar="${varname}_START"
30 local endvar="${varname}_END"
31 duration="$((${!endvar} - ${!startvar}))"
32 # separate it into hours and minutes.
33 minutes="$(($duration / 60))"
34 hours="$(($duration / 60))"
35 # subtract the hours from the minutes sum for modulo.
36 minutes="$(($minutes - $hours * 60))"
37 # fashion conceit, add zeroes.
38 if (($minutes < 10)); then minutes="0$minutes"; fi
39 if (($hours < 10)); then hours="0$hours"; fi
40 echo "Time taken for ${varname}: ${hours}:${minutes} hh:mm (a total of $duration seconds)"