0

./script.csh 05:25:00 02:30:50

should return output in mins

since i am looping through the series recursively how to compare values with new large value . would | bc work in csh ?

2 Answers2

0

For full difference:

perl -e 'for(@ARGV){s/(..):(..):(..)/$1*3600+$2*60+$3/e} $_=(shift)-(shift); printf"%02d:%02d:%02d\n",$_/3600,$_/60%60,$_%60' 05:25:00 02:30:50

For difference in minutes:

perl -e 'for(@ARGV){s/(..):(..):(..)/$1*3600+$2*60+$3/e} $_=(shift)-(shift); printf"%d\n",$_/60' 05:25:00 02:30:50

Works in bash, too.

Ole Tange
  • 35,514
0

It's not clear whether you want the two dates to wrap through days, or you're assuming that they're both in the same day and may be in any order.

This answer assumes the first, i.e. timediff 23:00:00 01:00:00 should return 2 hours = 120 minutes.

Change @ s = 86400 + $s to @ s = - $s if that should return 22 hours = 79200 minutes.

alias timediff 'set t=(\!:*:gs/:/ * 3600 + /:gs/:/ * 60 + /:q);\\
   @ s = ($t[2]) - ($t[1]); if($s < 0) @ s = 86400 + $s;\\
   @ m = $s / 60;\\
   @ H = $s / 3600 M = $s % 3600 / 60 S = $s % 60;\\
   echo seconds=$s minutes=$m hours=$H\:$M\:$S'

timediff 05:25:00 02:30:50
seconds=75950 minutes=1265 hours=21:5:50

As a script:

set t=($argv:gs/:/ * 3600 + /:gs/:/ * 60 + /:q)
@ s = ($t[2]) - ($t[1]); if($s < 0) @ s = 86400 + $s
@ m = $s / 60
@ H = $s / 3600 M = $s % 3600 / 60 S = $s % 60
echo seconds=$s minutes=$m hours=$H\:$M\:$S