sailwind
asked on
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!
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!
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#include <time.h>
size_t strftime (char *s, size_t maxsize, const char *format, const struct tm *timeptr);
size_t strftime (char *s, size_t maxsize, const char *format, const struct tm *timeptr);
#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)
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)
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