bash/snippets/time_duration.sh

71 lines
1.3 KiB
Bash
Executable File

#!/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
if [[ $__t_h -lt 10 ]] ; then
__t_h=" $__t_h"
fi
if [[ $__t_m -lt 10 ]] ; then
__t_m=" $__t_m"
fi
if [[ $__t_s -lt 10 ]] ; then
__t_s=" $__t_s"
fi
echo "$__t_h h : $__t_m min : $__t_s sec"
elif [[ $__t_m -gt 0 ]]; then
if [[ $__t_m -lt 10 ]] ; then
__t_m=" $__t_m"
fi
if [[ $__t_s -lt 10 ]] ; then
__t_s=" $__t_s"
fi
echo "$__t_m min : $__t_s sec"
else
if [[ $__t_s -lt 10 ]] ; then
echo " $__t_s sec"
else
echo "$__t_s sec"
fi
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