View previous topic :: View next topic
|
Author |
Message |
Deborah Shugerts
New User
Joined: 14 Oct 2009 Posts: 10 Location: A Florida Cubicle...
|
|
|
|
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 |
|
|
Deborah Shugerts
New User
Joined: 14 Oct 2009 Posts: 10 Location: A Florida Cubicle...
|
|
|
|
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 |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
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 |
|
|
Deborah Shugerts
New User
Joined: 14 Oct 2009 Posts: 10 Location: A Florida Cubicle...
|
|
|
|
Thanks to all for your help ! |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
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
|
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
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 |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8797 Location: Welsh Wales
|
|
|
|
Thanks Dick, always nice to know that ones efforts are sometimes appreciated |
|
Back to top |
|
|
Deborah Shugerts
New User
Joined: 14 Oct 2009 Posts: 10 Location: A Florida Cubicle...
|
|
|
|
Thx to both of you too (expat and dick) ! Really appreciate all your help with this ! |
|
Back to top |
|
|
|