RoughRyde said:
What I want is to calculate the number of days between two dates.
Given my limited knowledge of C I found an example of this on the web
which looked as below. The problem is that I'm given indata as doubles.
What do the doubles contain? Without knowing that, it's pretty hard to
suggest what might work better.
Based on the code you posted, let's pretend that the doubles you're
given represent a date as
10000 * year + 100 * month + day
so that today's date would be 20061214.0. If that's the case, you can
replace the stotm() function with something like this,
void dtotm( struct tm *t, double d )
{
memset( t, 0, sizeof( *t ));
t->tm_year = ( int ) d / 10000 - 1900;
t->tm_mon = ( int ) fmod( d, 10000 ) / 100 - 1;
t->tm_mday = ( int ) fmod( d, 100 );
}
Change calcdiff() to accept double arguments rather than strings, and
replace calls to stotm() with dtotm().
But now you have another problem. If either of the dates is before
January 1, 1970, calcdiff() will fail. In many implementations, it'll
also fail if either date is after January 18, 2038. This is the range
of dates that can typically be stored in a value of type time_t.
If you need to work with dates outside this range, consider converting
them to Julian day numbers. This is the number of days since January 1,
4713 B.C. Today is JD 2454084, for example. You can subtract these
numbers directly to find the number of days between two dates. Code for
converting to JD is easily googled.
- Ernie
http://home.comcast.net/~erniew