Introduction
I detected the following problem with the
COleDateTime ==, <, >, <=, >= operators. A COleDateTime object
is internally represented by a double. So, when comparing
two COleDateTime objects it is in fact two doubles that are
being compared and this means trouble. E.g. I saw that
two COleDateTime objects that were perfectly equal (in human
readable format) were indicated as not equal with the
COleDateTime == operator.
Solution
Do the comparing yourself based on a string-compare of
the COleDateTime objects. These are the functions that I am using now.
BOOL DatesEqual(COleDateTime &odt1, COleDateTime &odt2)
{
CString str1 = odt1.Format();
CString str2 = odt2.Format();
return (!str1.Compare(str2));
}
BOOL DateSmallerThan(COleDateTime &odt1,
COleDateTime &odt2)
{
if (DatesEqual(odt1, odt2))
return FALSE;
else
return odt1 < odt2;
}
BOOL DateGreaterThan(COleDateTime &odt1,
COleDateTime &odt2)
{
if (DatesEqual(odt1, odt2))
return FALSE;
else
return odt1 > odt2;
}
BOOL DateSmallerThanOrEqual(COleDateTime &odt1,
COleDateTime &odt2)
{
if (DatesEqual(odt1, odt2))
return TRUE;
else
return odt1 < odt2;
}
BOOL DateGreaterThanOrEqual(COleDateTime &odt1,
COleDateTime &odt2)
{
if (DatesEqual(odt1, odt2))
return TRUE;
else
return odt1 > odt2;
}
Another aid in programming more accurate when using COleDateTimeSpan
objects
is the following.
Suppose you want to produce a sequence of
15 minute ColeDateTime objects, starting at some point in
time. Normally, one would program this something like:
COleDateTimeSpan span;
span = COleDateTimeSpan(0,0,15,0);
ColeDateTime StartTime, DateTimeWalker;
StartTime = ...; DateTimeWalker = StartTime;
for (int i=0; i<NR_OF_QUARTERS; i++)
{
...
DateTimeWalker += span;
}
However, it is more accurate to replace the body of the loop by:
{
COleDateTimeSpan dtsSpan(0,0,i*15,0);
COleDateTime TimeToUse = StartTime + dtsSpan;
...
}
This way, no error is accumulated during the loop,
resulting in an almost perfect value for the variable TimeToUse
even for the last loop iteration. Hope this is helpful to you
The first 5 years of my career I programmed in pure C. (Production automatisation software)
In Q4 of 1997 I switched to Visual C++/MFC.
(Headend Management system for cable operators)
In Q1 of 2003 I changed job and since then I'm programming in JAVA. So, now I'm looking for a JAVA site as brilliant as I found this one for C++.
Two years ago I started also programming in Adobe Flex.