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?
 
ozoConnect With a Mentor Commented:
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
 
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
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
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
 
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
 
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
All Courses

From novice to tech pro — start learning today.