value of variable based on time of the day

i would need some help in setting the value of a variable (TIME_NOW) depending on the time of the day ...e.g.

if today's date is 12th April 2009 and if the current time is between midnight [00:00:00] and 16:59:59 hrs then the TIME_NOW should be yesterday's date i.e. TIME_NOW=11
else if the current time between 17:00:00 [hh mm ss] hrs and midnight [00:00:00] .... then the TIME_NOW should be set to today's date i.e. TIME_NOW=12
i tried the following logic

calculate_time_now ()

{



        typeset -i Date=$(date +%H)
    if (( $Date >= 17 ))
    then

        time_now=$(date +%d)

    else

        time_now=$(TZ=PST+24 date +%d)

    fi

        echo $time_now

}

but at some time around midgnight ... it is not working [at 0038 hrs on 11th april 2009 ... it calculated time_now as 10 {instead of 11}].

Please advise.

Keep it simple method?

TESTHOUR=`date +%H`
TODAYSDATE=`date +%d`

if [ ${TESTHOUR} -ge 17 ]; then
  TIMENOW=${TODAYSDATE}
else
  TIMENOW=`expr ${TODAYSDATE} - 1`
fi
echo ${TIMENOW}

That will fail if the date changes between the two calls to date; more reliable (and more efficient) is:

eval "$(date +"TESTHOUR=%H TODAYSDATE=%d")"

First, there is no need to use expr; the Unix shell has arithmetic builtin:

TIMENOW=$(( $TODAYSDATE - 1 ))

However, that will fail if $TODAYSDATE is the first day of the month.

Thanks for your help. It was enligthening.