Exit script after exactly 3 hours

Need to run a script for exactly 3 hours.
It will have while loop which is to keep running until its 3 hours since the script's invocation time and then exit the script.
Script could be invoked at any time.
May have to change the number of hours later as per need.

Are there any existing date function in aix version3 that could help me add specific number of hours to the time when script is invoked and store it in variable? If not, can you suggest me some other approach please?

date -d option does not exist on my aix system.
value of $TZ is :

echo $TZ
NFT-1DFT,M3.5.0/02:00:00,M10.5.0/03:00:00

The following code will print "Hello" for 3 hrs!

#! /bin/bash
start=`date +"%s"`
while [ `date +"%s"` -ne `expr $start + 10800` ]
do
    echo "Hello"
    sleep 1
done

bash and ksh have built-in variable SECONDS

AIX 3 is about 20 years old by now! If the above solutions do not work in your ancient environment you can try a diffent approach: Start a script in the background that sleeps for the desired time and then creates a flag file that causes the main script to stop.
Script sleeperagent.ksh:

#!/bin/ksh
# usage: sleeperagent.ksh <flagfile> <timeinseconds>
[ ! -f $1 ] && sleep $2   # flagfile should not exist when we start sleeping
touch $1

main script:

...
flagfile=/tmp/$$.flag
/path/to/sleeperagent.ksh $flagfile 10800 &  # 10800 seconds = 3 hours
while [ ! -f $flagfile ]; do
...
done
rm $flagfile
...

One way... Try this...

killer_script

#!/bin/bash

test $# -ne 2 && echo "Usage : killer_script <pid> <time_secs>" && exit 0                      
pid_to_kill=$1
time_to_die=$2
sleep $time_to_die
kill -9 $pid_to_kill

master_script

#!/bin/bash

./killer_script $$ 10800 &

#do your stuff and the killer will kill this script after the specified time

--ahamed

Assuming you have permissions to use "cron", I'd start the script with an "at" job to create a flag file in 3 hours time. Then test for the presence of the file in the main "while" loop of the script. This has the added advantage that you can stop the script whenever you want. There may by subtle syntax differences for AIX.

echo "touch /tmp/exitflagfile" | at now +3 hours

while whatever
do
     [ if -f /tmp/exitflagfile ]
     then
            rm /tmp/exitflagfile
            exit
     fi
     # Rest of script
done