diff --git a/snippets/time_duration.sh b/snippets/time_duration.sh new file mode 100755 index 0000000..1812c2e --- /dev/null +++ b/snippets/time_duration.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +function time_duration () { + + local __begin=$1 + local __end=$2 + + local __duration + + if [[ "$__begin" ]] && [[ "$__end" ]] ; then + + local __duration + + local __time=`expr $__end - $__begin` + local __t_h=`expr $__time / 60 / 60` + local __t_rest_h=`expr $__time - $__t_h \\* 60 \\* 60` + local __t_m=`expr $__t_rest_h / 60` + local __t_s=`expr $__t_rest_h - $__t_m \\* 60` + + if [[ $__t_h -gt 0 ]]; then + echo "$__t_h h : $__t_m min : $__t_s sec" + elif [[ $__t_m -gt 0 ]]; then + echo "$__t_m min : $__t_s sec" + else + echo "$__t_s sec" + fi + + else + echo "N/A" + fi + + +} + +b_timestamp=$(date +"%s") +sleep 3 +e_timestamp=$(date +"%s") + +duration=$(time_duration $b_timestamp $e_timestamp) + +echo "" +echo "time duration: [ $duration ]" +echo "" + +exit 0