script for add and subtract two hours

i find a script to calculate hours of job i(nclude extraordinary)

i make a script to calculate add and subtract two hours (format hh:mm:ss)

Do you know about time?
try:

time myscript.sh

Or try:

    start_time=`perl -e 'print time;'`
    myscript.sh
    elapsed_time=$(echo "`perl -e 'print time;'` - $start_time    | bc)
    echo "Total time in seconds: $elapsed_time"

doesn't 'elapsed = finish_time - start_time' and not vice versa?
not sure if you need 'bc' - your math is integer math anyway.

Yup -vgersh - you're right.

Some shells do not do integer math beyond 255. I can't tell what shell.

time myscript.sh ????

where is the code ???

i search a ssh or ksh script Timesub(time1 time2 ) which return time1-time2

(two string :"hh.mm.ss")

can you help me ??

the propper do to is using epoch. (seconds since 1.1.1970) because if you have DST is can
srew your script very badly. also the math is integer math and therefore very easy.

you can use date to convert from and into epoch.

Adapt the following script :

#!/ust/bin/ksh

TimeDiff()
{
   integer seconds
   integer h1 m1 s1 time1
   integer h2 m2 s2 time2

   # Convert times to seconds

   echo "$1" | IFS=':' read h1 m1 s1
   echo "$2" | IFS=':' read h2 m2 s2
   (( time1 = h1*3600  + m1*60 +s1 ))
   (( time2 = h2*3600  + m2*60 +s2 ))

   # Compute diff times in seconds

   (( seconds = time1 - time2 ))
   (( seconds < 0 )) && (( seconds = -seconds ))

   # Output result as seconds

   echo $seconds

   # Output result as hh:mm:ss

   (( h1 = seconds / 3600 ))
   (( m1 = seconds / 60 % 60 ))
   (( s1 = seconds % 60 ))
   printf "%02d:%02d:%02d\n" $h1 $m1 $s1
}

TimeDiff $1 $2

Output:

$ sh difftime.ksh 12:12:12 11:10:09  
3723
01:02:03

Jean-Pierre.