Computing.Net > Forums > Unix > How to calculate the time interval

How to calculate the time interval

Reply to Message Icon

Original Message
Name: rajinitm
Date: April 16, 2007 at 03:40:05 Pacific
Subject: How to calculate the time interval
OS: Unix
CPU/Ram: 512
Comment:

Hi Experts,

Using unix-KSH script, we need to calculate the difference between two time intervals. for example;

time_a=04:35:54
time_b=00:00:05 (pls. note that this shows only seconds)

Now, the required script should subtract the time_b from time_a and show the output. Your help would be highly appreciable. Thanks.

Regards,
Rajinikanth


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: April 16, 2007 at 08:54:51 Pacific
Subject: How to calculate the time interval
Reply: (edit)

60 seconds in a minute and 3600 seconds in an hour. Use the set command to parse the time string into 3 arguments:

#!/bin/ksh

time_a=04:35:54
set - $(IFS=":"; echo $time_a)

tta=$(( ((3600 * $1) + (60 * $2)) + $3))
echo $tta

time_b=00:00:05
set - $(IFS=":"; echo $time_b)
ttb=$(( ((3600 * $1) + (60 * $2)) + $3))
echo $ttb

# difference in seconds
tdiff=$(($tta - $ttb))
echo $tdiff


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: April 21, 2007 at 21:49:42 Pacific
Subject: How to calculate the time interval
Reply: (edit)

using awk
[code]
awk ' BEGIN {
an = split ( "04:35:54", atime, ":" );
bn = split ( "00:00:05" , btime ,":" );
totala = ( atime[1] * 3600 ) + (atime[2] * 60 ) + atime[3]
totalb = ( btime[1] * 3600 ) + (btime[2] * 60 ) + btime[3]
total = totala + totalb
print "total time taken: " total " secs."

}'
[/code]


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to calculate the time interval 

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software