How to convert string(variable) into date( epoch) in ksh on HPUX machine?

Hi all,
I have used a bash script which ultimately converts a string into date using date --date option:

 
DATE=$DATE" "$TIME" "`date +%Y` //concatenating 2 strings
TMRW_DATE=`date --date="$DATE" +"%s"` //applying date command on string and getting the unixtime 
 

but i'm not able to convert it into a ksh equivalent.So can anybody help me to do so?

What do you exactly mean by ksh equivalent? The date tool that you've used in your bash script is a utility installed on your system running HP-UX, and not a shell feature specific to bash.. You may invoke it from ksh as much you did from bash, if that's what your concern is all about.

and that date syntax is specific to your OS... You will need to look at what HP-UX date command proposes and try to figure out what you need...

You have posix-sh in your UX - use it or download ksh93

Then you can do using ksh builtin printf , no need for external GNU date.

((day=24*60*60))  
fromdate="04/01/11" 
todate="05/04/11" 
# datestr => epoc 
epoc1=$(printf "%(%#)T" "$fromdate") 
epoc2=$(printf "%(%#)T" "$todate") 
echo $(( (epoc2-epoc1) /day)) 

fromdate="2011-04-01" 
todate="2011-05-04" 
# datestr => epoc 
epoc1=$(printf "%(%#)T" "$fromdate") 
epoc2=$(printf "%(%#)T" "$todate") 
echo $(( (epoc2-epoc1) /day))

Or using some function which give you julian value - works with almost any shell. I have used this set:

#########################################################################
Date2Jul()
{
        oifs="$IFS"
        IFS="-"
        array=($1)
        IFS="$oifs"
        dd=${array[2]}
        mm=${array[1]}
        yyyy=${array[0]}
        ((a= (14-mm)/12 ))
        ((y= yyyy+4800 - a ))
        ((m= mm + 12*a -3 ))
        ((jd= dd + (153*m+2)/5 + 365*y + y/4 - 32083 ))
        echo $jd
}

#########################################################################
Julian2Date()
{
        Xjd="$1"
        Xb=0
        (( Xc=Xjd+32082))
        (( Xd= (4*Xc+3)/1461 ))
        (( Xe= Xc-((1461*Xd)/4)  ))
        (( Xm= (5*Xe+2)/153 ))
        (( Xday= Xe - (153*Xm+2)/5 + 1 ))
        (( Xmonth= Xm + 3 - 12*(Xm/10) ))
        (( Xyear= 100*Xb + Xd - 4800 + Xm/10 ))
        Xdstr="$Xday"
        Xmstr="$Xmonth"
        [ "$Xday" -lt 10 ] && Xdstr="0$Xdstr"
        [ "$Xmonth" -lt 10 ] && Xmstr="0$Xmstr"
        Xjulstr="$Xyear$Xmstr$Xdstr"


        ((Xa= (14-Xmonth)/12 ))
        ((Xy= Xyear - Xa ))
        ((Xm= Xmonth + 12*Xa - 2 ))
        ((Xweekday=  (6 + Xday + Xy + Xy/4 + (31*Xm)/12 ) % 7  ))
        ((Xday<10)) && Xday="0$Xday"
        ((Xmonth<10)) && Xmonth="0$Xmonth"
        echo "$Xjd $Xdstr $Xmstr $Xyear $Xweekday $Xjulstr"
}

######################

        # yyyy-mm-dd 
        [ "$#" -lt 1 ] && echo "usage:$0 yyyy-mm-dd" >&2 && exit 1
        datestr="$1"

        julian=$(Date2Jul "$datestr")
        Julian2Date "$julian" | read julian2 d m y dayOfWeek yyyymmdd
        echo "$julian: $d.$m.$y "
        echo "weekday (mon=1):$dayOfWeek"
        echo "yyyymmdd: $yyyymmdd"
        ((tomorrow=julian+1))
        Julian2Date "$tomorrow" | read julian2 d m y dayOfWeek yyyymmdd
        echo "tomorrow $tomorrow: $d.$m.$y "

To be more specific, in bash

TMRW_DATE=`date --date="$DATE" +"%s"`

where 'date --date' is converting the string $DATE into its epoch time (in sec) but in ksh there is no such option with date.

As another limitation I can't download ksh93 or any higher version.I'm currently having ksh88 installed in HPUX machine .If its not possible in ksh script then can i accomplish it using any perl script?

@Rashu123: From your first post, what does $DATE, $TIME contain? Please provide sample input and expected output.

According to my bash script:

 
DATE=`TZ=IST-24 date |awk -F' ' '{print $2,$3}'`
 echo $DATE
 TIME="10:00:00 IST"
DATE1=$DATE" "$TIME" "`date +%Y`
echo $DATE1
TMRW_DATE=`date --date="$DATE1" +"%s"`
echo $TMRW_DATE

gives output as:

Sep 19
Sep 19 10:00:00 IST 2012
1348029000

So how implement it in ksh or perl ?

I'm not sure if you understood post #2 and post #3.

In perl, you may use the timelocal routine from Time::Local module to get the time in seconds since epoch. Check the perldocs for instructions on how to implement the same.