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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert string(variable) into date( epoch) in ksh on HPUX machine?
# 1  
Old 09-17-2012
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:
Mod Comment Please use code tags

but i'm not able to convert it into a ksh equivalent.So can anybody help me to do so?
# 2  
Old 09-17-2012
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.
# 3  
Old 09-17-2012
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
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 "

# 5  
Old 09-18-2012
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?
# 6  
Old 09-18-2012
@Rashu123: From your first post, what does $DATE, $TIME contain? Please provide sample input and expected output.
# 7  
Old 09-18-2012
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 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Second Column Date Into EPOCH Time And Print Complete Row

Hello Team, I am stuck in getting the required output in the following case. Please help. My input file is aa|08/01/2016 bb|08/15/2016 I wish to convert the file into aa|epoch time bb|epoch time I am using following code: (3 Replies)
Discussion started by: angshuman
3 Replies

2. Shell Programming and Scripting

Convert a string to epoch time

Team, I am working on a shell script and i am extracting a date string in "SunOS server" with below format. Mon Jan 21 04:13:48 EST 2021 Can you please assist me the best way to convert the extracted string to epoch time like "date +%s" in Linux. Thanks in advance (1 Reply)
Discussion started by: Girish19
1 Replies

3. Shell Programming and Scripting

Convert a future date into epoch seconds on HPUX system

Hi All, I have scenario where i have to compare two dates. I thought of converting them to epoch seconds and do a numeric comparison. This works fine on Linux systems. $ date -d '2015/12/31' +%s 1451538000 $ date +%s 1449159121 But we don't have -d option in HPUX. What would be... (5 Replies)
Discussion started by: veeresh_15
5 Replies

4. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

5. Shell Programming and Scripting

Convert epoch time to Julian date

Need assistance in converting an epoch time to Julian date To get epoch perl -e 'use Time::Local; print timelocal(1,5,2,12,10,2008), "\n"' (3 Replies)
Discussion started by: ajayram_arya
3 Replies

6. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

7. Shell Programming and Scripting

Convert epoch to human readable date & time format

Hello I have log file from solaris system which has date field converted by Java application using System.currentTimeMillis() function, example is 1280943608380 which equivalent to GMT: Wed, 04 Aug 2010 17:40:08 GMT. Now I need a function in shell script which will convert 1280943608380... (3 Replies)
Discussion started by: Yaminib
3 Replies

8. Shell Programming and Scripting

Need to convert an epoch date to MMDDYYHHmm format

System: HP-UX Kornshell Perl is installed, but not POSIX Hello, I am calculating a future date/time. To do this I take the system date in epoch format and add to it. I now need to take the new epoch date and convert it to MMDDYYHHmm format. Any help with this is greatly appreciated. (4 Replies)
Discussion started by: LetsGoPens
4 Replies

9. Shell Programming and Scripting

Convert value stored in a variable to epoch time?

Hello I have a the creation date of a file stored in a variable in the following format: Wed May 06 10:14:58 2009Is there a way I can echo the variable and display it in epoch time? I've done a lot of searching on this topic, but haven't managed to get a solution. I'm on Solaris 10. ... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

10. Shell Programming and Scripting

Convert date into epoch in Perl on Solaris

Solaris 10 doesn't seem to like me a lot. I am trying to run a simple script to accept date and return epoch of that date: #!/usr/bin/perl -w use strict; use Time::ParseDate; my($date1)="Mon Mar 27 05:54:08 CDT 2009"; chomp $date1; #Convert to seconds since start of epoch my $time1 =... (3 Replies)
Discussion started by: pavanlimo
3 Replies
Login or Register to Ask a Question