[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# Clock and timestamp conversions

Posted on 1999-11-30
Medium Priority
354 Views
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
0
Question by:sailwind

LVL 2

Expert Comment

ID: 2245157

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

LVL 3

Author Comment

ID: 2245163
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

LVL 16

Accepted Solution

ID: 2245197
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

LVL 85

Expert Comment

ID: 2245279
#include <time.h>

size_t strftime (char *s, size_t maxsize, const char *format, const struct tm *timeptr);
0

LVL 10

Expert Comment

ID: 2268178
#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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
###### Suggested Courses
Course of the Month9 days, 9 hours left to enroll