Article ID: 148751
Article Last Modified on 11/21/2006
double DoubleFromDate(DATE dt)
{
// No problem if it's positive.
if(dt >= 0)
return dt;
// If it's negative, must convert because negative dates not
// continuous (for example, -1.25 to -.75, -1.50 to -.50,
// -1.75 to -.25).
double dblWhole = modf(dt, &dt); // dt is now a fractional part.
return dblWhole - dt;
}
DATE DateFromDouble(double dbl)
{
// No problem if it's positive.
if(dbl >= 0)
return dbl;
// If it's negative, must convert because negative dates not
// continuous (for example, -.75 to -1.25, -.50 to -1.50,
// -.25 to -1.75).
DATE dtWhole = modf(dbl, &dbl); // dbl is now a fractional part.
return dtWhole - dbl;
}
Notice that the modf() function is called incorrectly. The whole number and
the fractional portion are reversed.
#include <afxole.h>
#include <math.h>
double GetDaySpan(COleDateTime dt1, COleDateTime dt2)
{
// No problem if it's positive.
if(dt1>=0 && dt2 >= 0)
return fabs(dt1-dt2);
// Days from midnight December 30, 1899.
double Dt1SpanFromZero, Dt2SpanFromZero;
if (dt1<0)
{
double dblDt1Whole, dblDt1Fraction;
dblDt1Fraction=modf(dt1,&dblDt1Whole);
Dt1SpanFromZero=dblDt1Whole-dblDt1Fraction;
}
else
{
Dt1SpanFromZero=dt1;
}
if (dt2<0)
{
double dblDt2Whole,dblDt2Fraction;
dblDt2Fraction = modf(dt2, &dblDt2Whole);
Dt2SpanFromZero=dblDt2Whole-dblDt2Fraction;
}
else
{
Dt2SpanFromZero=dt2;
}
// Return positive number of days.
return fabs(Dt1SpanFromZero-Dt2SpanFromZero);
}
void AddDays(COleDateTime &date, double dblDays)
{
// First, normalize the negative date.
// Days from midnight December 30, 1899.
double Dt1SpanFromZero;
if (date.m_dt<0)
{
double dblDt1Whole, dblDt1Fraction;
dblDt1Fraction=modf(date.m_dt,&dblDt1Whole);
Dt1SpanFromZero=dblDt1Whole-dblDt1Fraction;
}
else
{
Dt1SpanFromZero = date.m_dt;
}
date.m_dt = Dt1SpanFromZero + dblDays;
if (date.m_dt>=0)
return;
// Date before midnight December 30, 1899.
// You need to correct the date by changing the fractional portion.
double dblWhole, dblFraction;
dblFraction=modf(date,&dblWhole);
date.m_dt=dblWhole - ( 2 + dblFraction);
// Add 1 to make the fraction positive.
// Add 1 more to increase the whole part by one.
// For example, it converts a span of -1.75 (which is -1 - .75 )
// to -2.25 ( which is -2 + .25 )
}
Keywords: kbbug kbfix kbnoupdate kbvc420fix KB148751