• C

Clock and timestamp conversions

Hi there everyone,

I have a project where I need to convert a timestamp into
current year, month, date, hour, second, and day. The
timestamp is stored as number of seconds from Jan 1st,
1900. Is there any source available out there that does this
conversion and accounts for all the leap years? I can do the
translation given the year, month date... into the timestamp,
but have more problems going the other way around. Thanks
for all your help!
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.

basantCommented:
Answer for Win32

Store the result in Variant as

void GetTimeFrom( double dTime)
{

// dTime is the number of seconds
// from 1st Jan 1900
VARIANT varDate;

VariantInit( &varDate);
V_VT(&varDate) = VT_DATE;
V_VDate( &varDate) = dTime;

      SYSTEMTIME sysTime;
      VariantTimeToSystemTime( V_DATE(&varDate) ,&sysTime);
      if( FAILED( hr) )
      {
            return FALSE;
      }
}

Now access year, month, date, hour, second, and day from SYSTEMTIME struct
0
sailwindAuthor Commented:
I am not coding this for NT, but on an unix-like system. I
would like to see the algorithms that's used to convert
the time into dates and months, as opposed to just the
invocation of the function. Basically, I'm writing the conversion function by hand, and need to know how to do
these conversions or how the UNIX date/cal source code looks like. Thanks.
0
imladrisCommented:
Here's a routine I wrote years ago, and it has been in use in our system since that time. It converts days since Jan 1 1900 to calendar date:


static char mnthmax[13]    = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int  cumdays[2][13] = { 0, 31, 59, 90,120,151,181,212,243,273,304,334,365,
                               0, 31, 60, 91,121,152,182,213,244,274,305,335,366
                             };

/*=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=*\
<>                                                                         <>
|| Calendar                                                                ||
<>                                                                         <>
||   Calendar returns the calendar date in d m and y based on the julian   ||
<> date passed in jd.                                                      <>
||                                                                         ||
<> y = number of 4 year groups since 1900 * 4 + 1900                       <>
|| t = number of days from start of current 4 year group until now         ||
<> ty = number of years from start of current 4 year group until now       <>
|| if t is 0 you are in fact at end of previous year                       ||
<> else if ty>0 and total number of days in that many year (must include   <>
||      leap year day of first year in four year group)>=t then            ||
<>      y incremented by --ty                                              <>
|| else y incremented by ty                                                ||
<> if ty not zero subtract days of previous complete years out of t        <>
|| set month to 1                                                          ||
<> ti is true if current year is leapyear                                  <>
|| increment month while t greater than cumdays                            ||
<> subtract days of previous months out of t                               <>
|| set m to mo for month                                                   ||
<> remainder in t is d                                                     <>
||                                                                         ||
\*=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=<>=*/

void calendar(jd,d,m,y)
long jd;
int *d,*m,*y;

{       int t,ty,mo,ti;

        *y = (jd/1461)*4+1900;
        t  = jd%1461;
        ty  = t/365;
        if(ty==0 && t==0)
        {       --*y;
                t=365;
        }
        else if( ty>0 && (long)ty*365+1>=t )*y += --ty;
        else *y += ty;
        if(ty!=0) t -= ty*365+1;
        mo=1;
        ti = (int)(*y%4==0);
        while(t>cumdays[ti][mo])++mo;
        t -= cumdays[ti][mo-1];
        *m = mo;
        *d = t;
}


To get from seconds to days is just a matter of dividing by 86400.
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
ozoCommented:
#include <time.h>

size_t strftime (char *s, size_t maxsize, const char *format, const struct tm *timeptr);
0
rbrCommented:
#include <time.h>


long timestamp;
time_t conv;
struct tm *ptm;

conv = (time_t) timestamp;
ptm = localtime (&conv);


nowe you can use

ptm->tm_sec (seconds)
ptm->tm_min (minutes)
ptm->tm_hour
ptm->tm_mday
ptm->tm_mon (0=January)
ptm->tm_year (0=1900)
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.