How to convert string(variable) into date( epoch) in ksh on HPUX machine? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-17-2012
Rashu123 Rashu123 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 September 2012, 7:15 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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:

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

Moderator's Comments:
Please use code tags

but i'm not able to convert it into a ksh equivalent.So can anybody help me to do so?
Sponsored Links
    #2  
Old 09-17-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 26 September 2014, 11:00 PM EDT
Location: India
Posts: 1,828
Thanks: 25
Thanked 524 Times in 503 Posts
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.
Sponsored Links
    #3  
Old 09-17-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Last Activity: 30 September 2014, 8:25 AM EDT
Location: Switzerland - GE
Posts: 5,777
Thanks: 160
Thanked 406 Times in 381 Posts
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...
    #4  
Old 09-17-2012
kshji's Avatar
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 February 2014, 5:44 AM EST
Location: Finland
Posts: 602
Thanks: 0
Thanked 50 Times in 49 Posts
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.

Code:
((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:


Code:
#########################################################################
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 "

Sponsored Links
    #5  
Old 09-18-2012
Rashu123 Rashu123 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 September 2012, 7:15 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
    #6  
Old 09-18-2012
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 26 September 2014, 11:00 PM EDT
Location: India
Posts: 1,828
Thanks: 25
Thanked 524 Times in 503 Posts
@Rashu123: From your first post, what does $DATE, $TIME contain? Please provide sample input and expected output.
Sponsored Links
    #7  
Old 09-18-2012
Rashu123 Rashu123 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 18 September 2012, 7:15 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
According to my bash script:

Code:
 
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 ?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using awk or nawk to convert epoch time to date format minigts Shell Programming and Scripting 5 10-27-2011 12:56 PM
Convert epoch to human readable date & time format Yaminib Shell Programming and Scripting 3 08-14-2010 09:57 AM
Need to convert an epoch date to MMDDYYHHmm format LetsGoPens Shell Programming and Scripting 4 07-08-2010 01:39 PM
Convert value stored in a variable to epoch time? Glyn_Mo Shell Programming and Scripting 2 04-20-2010 10:21 AM
Convert date into epoch in Perl on Solaris pavanlimo Shell Programming and Scripting 3 04-07-2009 01:30 PM



All times are GMT -4. The time now is 09:50 AM.