IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

converting unix (epoch) time in rexx


IBM Mainframe Forums -> CLIST & REXX
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Deborah Shugerts

New User


Joined: 14 Oct 2009
Posts: 10
Location: A Florida Cubicle...

PostPosted: Thu Oct 29, 2009 2:07 am
Reply with quote

Has anyone tried to convert UNIX (epoch) time into MM/DD/YY and HH:MM:SS format ? For example, 1256651712 equates to 10/27/09 13:55:12 (UTC). I know that 1256651712 equates to the # of seconds since midnight 1-1-1970.

I can determine that 1256651712 is 14,544 days 13 hours 55 minutes 12 seconds since midnight 1-1-1970. So I know the time is 13:55:12.
I suppose I can start dividing 14,544 by 365 to determine the # of years ago. But is there an easier way ?
Back to top
View user's profile Send private message
Deborah Shugerts

New User


Joined: 14 Oct 2009
Posts: 10
Location: A Florida Cubicle...

PostPosted: Thu Oct 29, 2009 2:48 am
Reply with quote

I have worked out the basic code, but my julian date is off. I suspect leap years are the culprit.

/* rexx */
epoch = 1256651712
epochdays = 1256651712 % 86400
epochrem = epoch - (epochdays*86400)
epochhrs = epochrem % 3600
epochrem2 = epochrem - (epochhrs*3600)
epochmin = epochrem2 % 60
epochsec = epochrem2 - (epochmin*60)
epochyrs = epochdays % 365
daysleftover = epochdays - (epochyrs * 365)
thisyr = 1970 + epochyrs
jdate = substr(thisyr,3,2) || daysleftover
newdate = left(Date('U',jdate,'J'),8)
epochyrs = epochdays % 365
daysleftover = epochdays - (epochyrs * 365)
thisyr = 1970 + epochyrs
jdate = substr(thisyr,3,2) || daysleftover
newdate = left(Date('U',jdate,'J'),8)
say epochdays 'days since midnite 1-1-1970'
say 'Date is 'newdate
say 'Time is 'epochhrs':'epochmin':'epochsec

Time is 13:55:10
14544 days since midnite 1-1-1970
Date is 11/05/09
Time is 13:55:10

Date returned should be 10/27/09 13:55:10
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Oct 29, 2009 3:59 am
Reply with quote

a leapgrp is the number of days in a 4 year span - 3 normal 1 leap

leapgrp = ((365 * 3) + 366)

if you integer divide that into your number of days,
that will tell you how many leapgrp's or the number of extra days that you
did not allow for when dividing days by 365.

numtoomanydays = numdays % leapgrp

so the following is over by numtoomanydays (yeah I know, too long a reference name

daysleftover = epochdays - (epochyrs * 365) - numtoomanydays
Back to top
View user's profile Send private message
Deborah Shugerts

New User


Joined: 14 Oct 2009
Posts: 10
Location: A Florida Cubicle...

PostPosted: Thu Oct 29, 2009 5:00 pm
Reply with quote

Thanks to all for your help !
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Thu Oct 29, 2009 6:21 pm
Reply with quote

Don't know if this is easier for you, letting the REXX date function take care of leap years from 1970 onwards and this code also copes with the 100 and 400 year anomolies.
Code:

/* REXX *** EPOCH TIME TO CURRENT TIME                               */
NUMERIC DIGITS 31
EPOCH   = 1256651712
DAYS    = TRUNC(EPOCH / (3600 * 24))
REMAIN  = EPOCH - (DAYS * 24 * 3600)
BASEREX = DATE('B',19700101,'S')
BASEREX = BASEREX + DAYS
NEWDATE = DATE('S',BASEREX,'B')
HH = 0
MM = 0
SS = 0
DO FOREVER
  IF REMAIN >= 3600 THEN DO
    REMAIN = REMAIN - 3600
    HH = HH + 1
  END
  ELSE DO
    IF REMAIN >= 60 THEN DO
      REMAIN = REMAIN - 60
      MM = MM + 1
    END
    SS = REMAIN
    IF SS < 60 THEN LEAVE   
  END                       
END                         
SAY NEWDATE  HH MM SS       

Resule is
Code:

20091027 13 55 12
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Oct 29, 2009 7:42 pm
Reply with quote

I am never shy about using code from others that works:
Code:

/* REXX *** EPOCH TIME TO CURRENT TIME                               */

/*  from expat  */

NUMERIC DIGITS 31
EPOCH   = 1256651712
DAYS    = TRUNC(EPOCH / (3600 * 24))
REMAIN  = EPOCH - (DAYS * 24 * 3600)
say "Remain " remain
BASEREX = DATE('B',19700101,'S')
BASEREX = BASEREX + DAYS
NEWDATE = DATE('S',BASEREX,'B')

/*  from deborah  */

HH = remain % 3600
remain2 = remain - (HH*3600)
MM = remain2 % 60
SS = remain2 - (MM*60)

SAY NEWDATE  HH MM SS       



Result: 20091027 13 55 12

Thx to both Expat and Deborah for something new.
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Thu Oct 29, 2009 8:32 pm
Reply with quote

Thanks Dick, always nice to know that ones efforts are sometimes appreciated icon_lol.gif
Back to top
View user's profile Send private message
Deborah Shugerts

New User


Joined: 14 Oct 2009
Posts: 10
Location: A Florida Cubicle...

PostPosted: Thu Oct 29, 2009 11:38 pm
Reply with quote

Thx to both of you too (expat and dick) ! Really appreciate all your help with this !
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> CLIST & REXX

 


Similar Topics
Topic Forum Replies
No new posts Compile Several JCL JOB Through one r... CLIST & REXX 4
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts Running REXX through JOB CLIST & REXX 13
No new posts Error to read log with rexx CLIST & REXX 11
No new posts isfline didnt work in rexx at z/OS ve... CLIST & REXX 7
Search our Forums:

Back to Top