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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to calculate the time interval

Reply to Message Icon

Name: rajinitm
Date: April 16, 2007 at 03:40:05 Pacific
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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 16, 2007 at 08:54:51 Pacific
Reply:

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


0

Response Number 2
Name: ghostdog
Date: April 21, 2007 at 21:49:42 Pacific
Reply:

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]


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to calculate the time interval

Calculating Time www.computing.net/answers/unix/calculating-time/6225.html

how to get the last field www.computing.net/answers/unix/how-to-get-the-last-field/6529.html

how to change the monitor resolution? www.computing.net/answers/unix/how-to-change-the-monitor-resolution/1256.html