2 # some helpful functions for counting a duration.
4 #hmmm: should take a variable name for the start tracker and end tracker.
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 function end_time_tracking()
16 local varname="$1"; shift
17 if [ -z "$varname" ]; then echo must pass variable name to end_time_tracking; return 1; fi
18 local endvar="${varname}_END"
19 eval $endvar="$(date +"%s")"
22 function show_tracked_duration()
25 if [ -z "$varname" ]; then echo must pass variable name to end_time_tracking; return 1; fi
26 # calculate the time taken.
27 local startvar="${varname}_START"
28 local endvar="${varname}_END"
29 duration="$((${!endvar} - ${!startvar}))"
30 # separate it into hours and minutes.
31 minutes="$(($duration / 60))"
32 hours="$(($duration / 60))"
33 # subtract the hours from the minutes sum for modulo.
34 minutes="$(($minutes - $hours * 60))"
35 # fashion conceit, add zeroes.
36 if (($minutes < 10)); then minutes="0$minutes"; fi
37 if (($hours < 10)); then hours="0$hours"; fi
38 echo "Time taken for ${varname}: ${hours}:${minutes} hh:mm (a total of $duration seconds)"
41 echo testing time tracking...
42 start_time_tracking crungle
44 sleep $(($RANDOM / 1000))
47 end_time_tracking crungle
49 echo ...done testing time tracking.
51 show_tracked_duration crungle