# How to calculate the time interval

April 16, 2007 at 03:40:05
Specs: Unix, 512

 Hi Experts, Using unix-KSH script, we need to calculate the difference between two time intervals. for example; time_a=04:35:54time_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

See More: How to calculate the time interval

#1
April 16, 2007 at 08:54:51

 60 seconds in a minute and 3600 seconds in an hour. Use the set command to parse the time string into 3 arguments:#!/bin/kshtime_a=04:35:54set - \$(IFS=":"; echo \$time_a)tta=\$(( ((3600 * \$1) + (60 * \$2)) + \$3))echo \$ttatime_b=00:00:05set - \$(IFS=":"; echo \$time_b)ttb=\$(( ((3600 * \$1) + (60 * \$2)) + \$3))echo \$ttb# difference in secondstdiff=\$((\$tta - \$ttb))echo \$tdiff

Report •

#2
April 21, 2007 at 21:49:42

 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 •

Related Solutions