Unix TimeStamp Convert

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

mkeevil
Participant
Posts: 59
Joined: Mon Jul 14, 2003 12:59 pm
Location: USA

Unix TimeStamp Convert

Post by mkeevil »

Anyone know how to convert to a Unix timestamp... example:

'19-APR-2005 00:00:00' to 1113868800
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

mkeevil,

there are more date conversion options in DataStage than you'd want to shake a stick at.

But please explain the way you get 1113868800 from '19-APR-2005'. The internal DS format is days-since-31/12/1967; Your number looks like seconds-since-1970/01/01. If that is the case, the formula is

Code: Select all

(ICONV(In.DateColumn,"D4-DMY[2,3,4]')-ICONV('01-JAN-1970','D4-DMY[2,3,4]'))*60*60*24
Last edited by ArndW on Wed Apr 20, 2005 9:28 am, edited 1 time in total.
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

Fmt(IConv(YourDateTime[1, 11], "D-DMY[2,A3,4]"), "5'0'R") : Fmt(IConv(YourDateTime[13, 8], "MTS"), "5'0'R")
mkeevil
Participant
Posts: 59
Joined: Mon Jul 14, 2003 12:59 pm
Location: USA

Post by mkeevil »

ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Sainath,

I think you forgot to convert from DS internal "0" date (31/12/1967) and the Julian from the poster (01/01/1970)...
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

mkeevil wrote:I was using this site: http://www.onlineconversion.com/unix_time.htm
mkeevil,

you are doing seconds since 1/1/1970 so the formula that I gave earlier will work for you.
mkeevil
Participant
Posts: 59
Joined: Mon Jul 14, 2003 12:59 pm
Location: USA

Post by mkeevil »

I want to put them in a batch.... and pass them into an extract.. how would it look like? The above examples look like they go into a transform
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

ArndW,

I typed it online and did not check the return value. You are correct in highlighting the change required.

I assume that we need manipulate the time bit also.
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

While I was typing the post, I had a glimpse of an error mail raised by mkeevil. Surprisingly when I submitted mine that post disappeared. Is it due to some problem in the site or had the user withdrawn the post?

By-the-way, Mkeevil, I am using the variable YourDateTime to be passed to the transform. Hence you need to supply this value. It is not an 'additional' variable to be supplied.
mkeevil
Participant
Posts: 59
Joined: Mon Jul 14, 2003 12:59 pm
Location: USA

Post by mkeevil »

Time will always be 00:00:00 because I want to extract everything from yesterdays date until today... so it would be '19-APR-2005 00:00:00' to '20-APR-2005 00:00:00' I would like to pass both dates in there unix format into an extract to use in the where clause.
clshore
Charter Member
Charter Member
Posts: 115
Joined: Tue Oct 21, 2003 11:45 am

Post by clshore »

This works. Remember that UNIX time starts at 1-JAN-1970 00:00:00

Function UnixTime(Arg1)

timepiece = Arg1[13,8]
datepiece = Arg1[1,11]

* '19-APR-2005 00:00:00' to 1113868800

secsinday = 60 * 60 * 24

Ans = (iconv(datepiece,"D-") - iconv("1 jan 1970","D")) * secsinday + iconv(timepiece, "MTS")
mkeevil
Participant
Posts: 59
Joined: Mon Jul 14, 2003 12:59 pm
Location: USA

Post by mkeevil »

I removed that post because I saw that it was a transform statement.. I needed something that went into a batch. That was my error
Sainath.Srinivasan
Participant
Posts: 3337
Joined: Mon Jan 17, 2005 4:49 am
Location: United Kingdom

Post by Sainath.Srinivasan »

That is nice. To speed up the process, you can compute the iconv(1/1/1970) and use the integer value in its place.
mkeevil
Participant
Posts: 59
Joined: Mon Jul 14, 2003 12:59 pm
Location: USA

Post by mkeevil »

REVISED: Ohhh I think I have it... got to test but I guess I have to use the DEFUN command and pass in the parms.

I am still kind of new at Data Stage so I need to create a function with this code, and then call the function with Arg1 and return Ans. Do I have to make sure that Arg1, is in a specific format, example: 01-Jan-2005 00:00:00 or 1-Jan-2005 00:00:00? Also how would I execute this function from the batch so I can pass the Ans into my extract program?

Function UnixTime(Arg1)

timepiece = Arg1[13,8]
datepiece = Arg1[1,11]

* '19-APR-2005 00:00:00' to 1113868800

secsinday = 60 * 60 * 24

Ans = (iconv(datepiece,"D-") - iconv("1 jan 1970","D")) * secsinday + iconv(timepiece, "MTS")
clshore
Charter Member
Charter Member
Posts: 115
Joined: Tue Oct 21, 2003 11:45 am

Post by clshore »

The Arg1 format has to be the same, unless you want to pass in a format string as another arg, or put some parsing logic inside the function.

You can do it as a function, or inline as suggested above by pre-computing the secsindays and iconv('1 jan 1970','D'):

So if timestamp = '19-APR-2005 00:00:00' then

(iconv(timestamp[1,11],"D-")-732)*86400+iconv(timestamp[13,8],"MTS")

yields 1113868800


Carter

mkeevil wrote:REVISED: Ohhh I think I have it... got to test but I guess I have to use the DEFUN command and pass in the parms.

I am still kind of new at Data Stage so I need to create a function with this code, and then call the function with Arg1 and return Ans. Do I have to make sure that Arg1, is in a specific format, example: 01-Jan-2005 00:00:00 or 1-Jan-2005 00:00:00? Also how would I execute this function from the batch so I can pass the Ans into my extract program?

Function UnixTime(Arg1)

timepiece = Arg1[13,8]
datepiece = Arg1[1,11]

* '19-APR-2005 00:00:00' to 1113868800

secsinday = 60 * 60 * 24

Ans = (iconv(datepiece,"D-") - iconv("1 jan 1970","D")) * secsinday + iconv(timepiece, "MTS")
Post Reply