Operating System - HP-UX
1833268 Members
2890 Online
110051 Solutions
New Discussion

Script to get the time difference in UNIX Shell

 
SOLVED
Go to solution
Suman_7
Frequent Advisor

Script to get the time difference in UNIX Shell

Can someone send me a script through which I can calculate the time difference.

For example

begin_time=12,20,00

end_time=16,40,00

time_diff = ${end_time} - ${begin_time}

The result should be 04,20,00

Thank You,
4 REPLIES 4
RAC_1
Honored Contributor

Re: Script to get the time difference in UNIX Shell

Serach the forum for date hammer.

Anil
There is no substitute to HARDWORK
Sergejs Svitnevs
Honored Contributor

Re: Script to get the time difference in UNIX Shell

test.sh
-----------------------------------
#!/bin/bash
h1=`echo $1 | cut -c1-2` # Get Start Hour
m1=`echo $1 | cut -c3-4` # Get Start Minute
s1=`echo $1 | cut -c5-6` # Get Start Second
h2=`echo $2 | cut -c1-2` # Get Stop Hour
m2=`echo $2 | cut -c3-4` # Get Stop Minute
s2=`echo $2 | cut -c5-6` # Get Stop Second
s3=`expr $s2 - $s1`
if [ $s3 -lt 0 ]
then
s3=`expr $s3 + 60`
m1=`expr $m1 + 1`
fi
m3=`expr $m2 - $m1`
if [ $m3 -lt 0 ]
then
m3=`expr $m3 + 60`
h1=`expr $h1 + 1`
fi
h3=`expr $h2 - $h1`
if [ $h3 -lt 0 ]
then
h3=`expr $h3 + 24`
fi
for number in $h3 $m3 $s3
do
if [ $number -lt 10 ]
then
echo -n "0$number "
else
echo -n "$number "
fi
done
echo ""
-----------------------------------
./test.sh 122000 164000
04 20 00


Regards,
Sergejs
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Script to get the time difference in UNIX Shell

The key to what you are trying to do is converting to seconds and do all the calculations and then converting back to HHMMSS. This sounds like the job for functions to me. You also need to consider the case of end_time < begin_time because you passed midnite.

This should do it but it does assume that the maximum wrap is 1 day.

--------------------------------------------

#!/usr/bin/sh

to_seconds()
{
typeset T=${1}
shift
typeset -i10 HR=0
typeset -i10 MIN=0
typeset -i10 SEC=0
typeset -i10 SECONDS=0
echo "${T}" | awk -F ',' '{print $1,$2,$3}' | read HR MIN SEC
typeset -i10 STAT=${?}
SECONDS=$(( (${HR} * 3600) + (${MIN} * 60) + ${SEC} ))
echo ${SECONDS}
return ${STAT}
} # to_seconds

to_hhmmss()
{
typeset -i10 SECONDS=${1}
shift
typeset -i10 HR=0
typeset -i10 MIN=0
typeset -i10 SEC=0
HR=$((${SECONDS} / 3600))
MIN=$(( (${SECONDS} % 3600) / 60 ))
SEC=$(( ${SECONDS} % 60 ))
printf "%02d,%02d,%02d\n" ${HR} ${MIN} ${SEC}
return 0
} # to_hhmmss

begin_time="12,20,00"
end_time="16,40,00"

begin_seconds=$(to_seconds ${begin_time})
end_seconds=$(to_seconds ${end_time})

typeset -i10 DIFF=$((${end_seconds} - ${begin_seconds}))
if [[ ${DIFF} -lt 0 ]]
then
((DIFF += 86400))
fi
echo "Diff: ${DIFF}"

TIMEDIFF=$(to_hhmmss ${DIFF})
echo "Diff: ${TIMEDIFF}"

If it ain't broke, I can fix that.
Muthukumar_5
Honored Contributor

Re: Script to get the time difference in UNIX Shell

Hai,

We can do it with a simple script as,
# cat forum.ksh
set -x

# Calculate the time which the child process consumed for the Network authentication to SUT
time_cal()
{
time1=$1
time2=$2
diff=0

hour1=$(($(echo $time1 | cut -d , -f 1)*3600))
min1=$(($(echo $time1 | cut -d , -f 2)*60))
sec1=$(echo $time1 | cut -d , -f 3)

hour2=$(($(echo $time2 | cut -d , -f 1)*3600))
min2=$(($(echo $time2 | cut -d , -f 2)*60))
sec2=$(echo $time2 | cut -d , -f 3)

# Assign the difference in seconds to a variable
diff=$(($hour2+$min2+$sec2-$hour1-$min1-$sec1))

}

begin_time=$1
end_time=$2

time_cal $2 $1

result=$diff
hr=$(($result/3600))
div1=$(($result%3600))
min=$(($div1/60))
sec=$(($div1%60))

hours=$hr
[[ $hr -lt 10 ]] && hours="0$hr"

mins=$min
[[ $min -lt 10 ]] && mins="0$min"

secs=$sec
[[ $sec -lt 10 ]] && secs="0$sec"

echo $hours,$mins,$secs

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!