• C

Convert 32 bit timestamp to current year, day, and time

Hi there everyone.

I am trying to convert a 32 bit timestamp, which represents the number of
seconds from the year 1900, into the current year, month, day, hr, min, se.
I do not have a standard UNIX or NT library where I can just plug in the
timestamp and play. Does anyone have a routine that can do this while
accounting for the leap years? Thanks.



LVL 3
sailwindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
#include <time.h>
time_t timer = 948836865;
printf("%s",ctime(&timer));
0
sailwindAuthor Commented:
As I have mentioned before, I don't have access to a standard C library. If possible, can someone please put up the ctime() source code? Or tell me where I can find it? Thanks.
0
ozoCommented:
Sorry, I assumed a C language solution was wanted since the question was posted in the C Programming Topic Area.
If you don't have access to a standard C library, in what language do you want the routine written?
0
Redefining Cyber Security w/ AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Join our webinar on Sept. 21st to learn more about leveraging AI and machine learning to protect your business.

sailwindAuthor Commented:
Oh I'm still coding in C, I just don't have access to the time.h library. If possible, I would like to see how that ctime() routine was written in C. Or if there was a more humanized pseudocode form it would be appreciated too :)
0
ozoCommented:
If you don't have access to time.h, then you're not coding in standard C,
so I'm not sure what you do have access to in order to code the routine...
0
ozoCommented:
long jd=timer/(24*60*60)+2415021;  /* seconds since 1900-1-1 to Julian Day Number */
long jds=timer%(24*60*60);
long l,n,i,j,d,m,y;
        l = jd + 68569;
        n = ( 4 * l ) / 146097;
        l = l - ( 146097 * n + 3 ) / 4;
        i = ( 4000 * ( l + 1 ) ) / 1461001;
        l = l - ( 1461 * i ) / 4 + 31;
        j = ( 80 * l ) / 2447;
        d = l - ( 2447 * j ) / 80;
        l = j / 11;
        m = j + 2 - ( 12 * l );
        y = 100 * ( n - 49 ) + i + l;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sailwindAuthor Commented:
Thanks ozo, I tried it out and it works.  few questions:

1) Do you know what each calculation is doing? And
why it is calculated that way?

2) What are all these magic numbers like 146097, 68569, etc etc?

3) Now that we've rolled over the millenium, we really
only need to count the # of seconds from Y2K. Can
the routine now be made easier and more readable?

Thanks a lot!
0
ozoCommented:
For seconds since 2000-01-01, change 2415021 to 2451544
146097 is the number of days in 400 years
0
sailwindAuthor Commented:
Ozo, thanks for the help. Can you give me a brief pseudocode as to what the steps are doing? I have no idea what the l is, and what's being added or multiplied. A brief explanation of the magic numbers and action being done would help tremendously. Thanks!

Does any one of these numbers represent the number of days from 1/1/1900?

Please propose the solution as an answer and I will award the points.
0
ozoCommented:
If timer represents the number of seconds since 1 Jan 1900, then timer/(24*60*60) represents the number of days since 1 Jan 1900
2415021 is the Julian Day number of 1 Jan 1900
146097 is the number of days in the 400 year leap year cycle
Most of the other numbers are adjustments to get the irregular cycle of month lengths to come out.
-68569 is the Julian Day Number of 1 March -4900, because it's easier to count the month cycle starting after February
1461 is the number of days in a 4 year leap year cycle,
2447/80 would be close to the average month lenght if February had 30 days.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.