Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

finding difference between 2 given time.

SOLVED
Go to solution
Anand_30
Regular Advisor

finding difference between 2 given time.

Hi,

I am writing a script which genrates the start time and the end time of some transactions. I need to find the difference between them. How do I accomplish it. For example one of the start time and end time are as follows:

start time = 021037 (2nd hour, 10th min and 37th sec)
end time = 021206 (2nd hour, 12th min and 06th sec)

How do I find the difference (89 sec) in the script.

Thanks,
Andy

10 REPLIES
Tom Danzig
Honored Contributor

Re: finding difference between 2 given time.

I would use the built-in shell variable SECONDS for this assuming sh or ksh.

It starts at zero when the script runs. So if you have two portions of a script you want to time, you could use:


let CHECKPOINT=$SECONDS
echo "Elapsed time for fisrt operation is $CHECKPOINT seconds"

let CHECKPOINT=${SECONDS}-${CHECKPOINT}
echo "Elapsed time for second operation is $CHECKPOINT seconds"

You can also reset the SECONDS variable to zero at key points as well if you prefer.
Pete Randall
Outstanding Contributor

Re: finding difference between 2 given time.

Andy,

What about using the time command and wrapping all your transactions into a script:

time scriptname

real 0m0.25s
user 0m0.02s
sys 0m0.02s


Where scriptname contains all your transactions.


Pete



Pete
Anand_30
Regular Advisor

Re: finding difference between 2 given time.

My script actually retrieves the start time and the end time of transaction from the logs.

for example:-

IVR Start Time=195415,IVR End Time=195535

is one of the output created by my script.

Now I would need to find the difference between 195535 and 195415 which is 80sec.

How can I do this in a script.

Sorry for conveying improper message in my previous mail.

Thanks,
Anand.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: finding difference between 2 given time.

This should be rather easy leveraging awk (and I do assume that your input strings are 6 chars in length):

---------------------------------
#!/usr/bin/sh

calc_seconds()
{
typeset TIME=${1}
shift
typeset -i10 OUT=$(echo "${TIME}" | awk '{ print ((substr($0,1,2) + 0) * 3600) + ((substr($0,3,2) + 0) * 60) + (substr($0,5,2) + 0) }')
typeset -i10 STAT=${?}
echo "${OUT}"
return ${STAT}
} # calc_seconds

TIME1=021037
TIME2=021206

DIFF=$(( $(calc_seconds ${TIME2}) - $(calc_seconds ${TIME1}) ))
echo "Diff = ${DIFF} seconds."
------------------------------------

Note: I am not out of my mind (more or less) with those seemingly stupid substr() + 0's. The + 0 is a standard awk idiom to force an expression into a numeric context.

If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: finding difference between 2 given time.


In perl:

#!/bin/perl
sub seconds{
my($t) = @_;
return ((60*substr($t,0,2)+substr($t,2,2))*60+substr($t,4,2))
}
$begin = shift @ARGV;
$end = shift @ARGV;
$diff = seconds( $end ) - seconds( $begin );
$diff += seconds("240000") if ($diff < 0);
print "Diff = $diff\n";
Hein van den Heuvel
Honored Contributor

Re: finding difference between 2 given time.

Anand,


will you ever have to deal with a start time just before midnite and an end in the next day?

If so you will need to augment Clay's solution with an "add 86400 to diff if it is negative" like my perl answer does.

Cheers,
Hein.
Arturo Galbiati
Esteemed Contributor

Re: finding difference between 2 given time.

Hi Andy,
for this kind of problem usually I used two functions I've created: ResTime & GetTime

######################################
restime() {
######################################
# Resets var
SECONDS=0
}

######################################
gettime() {
######################################
# Returns the time spent (hh:mm:ss) by commands between restime and itself #
sec=$SECONDS
hh=$(expr $sec / 3600)
mm=$(expr \( $sec - 3600 \* $hh \) / 60)
ss=$(expr $sec - 3600 \* $hh - 60 \* $mm)
printf "%.2d:%.2d:%.2d\n" $hh $mm $ss
unset sec hh mm ss
}


In your script put the restime at the top of teh script and at the end you can type:
echo $(gettime) to see how much time your script used.

This are my 2 cents.
Art
Anand_30
Regular Advisor

Re: finding difference between 2 given time.

HI Clay,

I used your solution for calculating the time difference between 2 times successfully in HP-UNIX but it is giving some errors while using the same solution in SUN OS. The error is :

syntax error at line 6: `(' unexpected

Can you please help. I know this forum is for HP-UX but just wanted to know if you could help me.

A. Clay Stephenson
Acclaimed Contributor

Re: finding difference between 2 given time.

I don't have a Solaris box to play on but here's a couple of suggestions:

1. Change the "shebang" to #!/usr/bin/ksh.
B. Change the typeset -i10 XXX= to simply typeset -i XXX=.
III. Change 'awk' to 'nawk'. Under Solaris, awk is 'oawk' (Old Awk) so that nawk must be invoked explicitly. On HP-UX, awk is 'nawk' (New Awk). By the way, new awk is at least 15 years old and oawk is considered obsolete but I guess someone forgot to tell Sun.

Okay, that's 3 suggestions but who's counting.
If it ain't broke, I can fix that.
Michael Schulte zur Sur
Honored Contributor

Re: finding difference between 2 given time.

Hi,

you did not use copy/paste to transfer the program to solaris? Maybe you forgot to reconnect the typeset line which stretches overt two line.

greetings,

Michael
typeset -i10 OUT=$(echo "${TIME}" | awk '{ print ((substr($0,1,2) + 0) * 3600) + ((substr($0,3,2) + 0) * 60) + (substr($0,5,2) + 0) }')