COleDateTime problem

COleDateTime d1(2001, 10, 10, 14, 0, 0);
COleDateTime d2(2001, 10, 10, 13, 0, 0);
COleDateTimeSpan res = d1 - d2;
COleDateTimeSpan oneHour(0, 1, 0, 0);
if (res != oneHour)
     assert(false);

The above code causes an assertion.  You would expect the value of "res" to be the same as "oneHour", the values returned by GetTotalSeconds() are exactly the same.

The assertion appears to be due to a difference between the values of the double that is used to store the COleDateTime value.  This difference appears to be at the sub-second level but is significant because the comparison operators work directly with the double value.

I am using Visual Studio 6 with service pack 5.

Anyone got any fixes for this.  Microsoft don't seem to have anything in their knowledgebase on this problem.
hiron1pAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

migelCommented:
Hi!
just use this code:
if (fabs(oneHour-res)> 0.1)
assert(false);
xu2000Commented:
Hi,
ASSERT(res.GetTotalSeconds() == oneHour.GetTotalSeconds());

Xu
hiron1pAuthor Commented:
Migel

This would only detect date/times that are more than 144 minutes apart.  A smaller value than 0.1 could be used that equates to just under a second of time in the COleDateTime m_dt storage but it is not practical to have to change all instances of date comparisons in my code.
Obviously, COleDateTime could be subclassed and the comparison operators recoded to allow a small difference (just under a seconds worth) as discussed above, however, I am really looking for a proper fix that will enable the actual calculation (d1 - d2 in the code sample above) to produce the correct answer.
NOTE - the code sample is only provided as the smallest amount of code that will reproduce the problem and is not actually used in this simple form in my project.  The assert is just to highlight the problem.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

hiron1pAuthor Commented:
xu2000,

The time span is being used as part of other date/time calculations and so a proper fix is required.  See my previous comments (I appreciate they were posted after your comment!)
migelCommented:
Hi!
I just show way to resolve
of course value to compare must be much smaller:
AFX_OLE_DATETIME_HALFSECOND
that define as:
(1.0 / (2.0 * (60.0 * 60.0 * 24.0)))

xu2000Commented:
Hi,
"res = d1 - d2" is "res.m_span = d1.m_dt - d2.m_dt", m_span and m_dt is double type and m_dt is far great than m_span, so there must be some imprecise. I think there is not the fix.
Look GetTotalSeconds()
long lReturns = (long)(m_span * 24 * 60 * 60 + AFX_OLE_DATETIME_HALFSECOND);
Return value is round off.

So if you have many calculations, do them, don't pay your attenation to precise. After all calculation, you can compare them with the method of mine or migels.

Xu
hiron1pAuthor Commented:
As stated before, I do not want to go through the whole of the code in the project that I am working on and change all of the code that involves COleDateTime.

As there is no mention of this problem on the Microsoft Knowledge Base I am posting this problem to see if anyone has a proper solution that the fix the cause of the problem.  I am aware of several ways, including the ones posted by Migel and Xu that I can use to fudge around issue but they are not an actual fix for the cause of the problem.
Tim_MusschootCommented:
Hello,

I tried it on my compiter (VC++ 6 Ent, SP5), no luck either.  This does work:

COleDateTime d1(2001, 10, 10, 14, 0, 0);
COleDateTime d2(2001, 10, 10, 13, 0, 0);
COleDateTimeSpan res = d1 - d2;
if ((res.GetHours() == 1) && (res.GetDays() == 0) && (res.GetMinutes() == 0) && (res.GetSeconds() == 0))
   ASSERT(false);


Kind regards,
Tim Musschoot
hiron1pAuthor Commented:
Tim,

Thanks, but please read my previous comments.
Tim_MusschootCommented:
Hi,

This seems to be the error:

res.m_dt =     0.041666666671517
oneHour.m_dt = 0.041666666666667

The problem seems to be an internal rounding error.  I don't think there is a simple solution for this one ...

Tim
xu2000Commented:
I think it is not an error, it's losing data from real world to computer.
Another choice is CTime&CTimeSpan which use int type.

hiron1pAuthor Commented:
My current temporary fix is to sub class COleDateTime and modify the comparison operators to allow a range of +/- half a second.

Has anybody got any better solutions?
Thanks.
Tim_MusschootCommented:
Don't you mean the COleDateTimeSpan class?
If I were you, I wouldn't modify the MFC classes... I would create a function
bool CompareDates(const COleDateTimeSpan& A,const COleDateTimeSpan& b)
{
   if ((A - B) == COleDateTimeSpan(0,0,0,0))
     || ((A-B) < COleDateTimeSpan(0,0,0,1))
       return true
   else return false;
}

I think this is a better solution than editing or subclassing...  And you can easyly search and replace all excisting issues...

Tim Musschoot
migelCommented:
Hi!
I agree with xu2000 it is usual round up problem.
but I recommend you derive your own class for COleDiteTimeSpan and use it instead std Span.
in this class override
operator=
operator==
operator!=
and copy constructor
this way allow you use this class in the all place where you need pass std class.
hiron1pAuthor Commented:
migel,

My last comment stated that I have subclassed the COleDateTimeSpan class!!!
DanRollinsCommented:
Risking being yelled at...

There is a solution that does not involve deriving a class from COleDateTimeSpan (and thereby requiring mods all over your code base).

The COleDateTimeSpan comparison operators are all defined as inline code.  So if you can easily replace them by changing some header that is included in all of your source:

#include <math.h>
inline BOOL COleDateTimeSpan::operator!=( const COleDateTimeSpan& dateSpan) const {
    double nDif= fabs( m_span - dateSpan.m_span );
    return (m_status != dateSpan.m_status || (nDif > 0.0000000001) );
}

Do the same for the == <=, >= and bob's yer uncle.

-- Dan
DanRollinsCommented:
Hi hiron1p,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
hiron1pAuthor Commented:
As stated above, I have subclassed and altered the comparison operators to have a tolerance of half a second.  I am still looking for a solution that will enable the COleDateTime class to work correctly (i.e. if anyone knows how to fix the rounding problems that remain in the class).
DanRollinsCommented:
I'm not sure where you're going here.  This problem is obviously inherent with the class.  It *can* be fixed, using subclassing or the inline replacement technique.  Are you looking for a magic wand?

--Dan
hiron1pAuthor Commented:
Thanks Dan, posting comments stating the obvious and being cocky is really going to help.

As i said, i would be interested in a solution that fixes the source of the problem.  i.e. changes the calculations so that they do not exhibit the rounding errors that they do, rather than adding a half second tolerance into the comparison operators and hoping that the accumulative error is never more than the tolerance.
DanRollinsCommented:
Oops, sorry.  It sounded like you are against any fix that would involve subclassing or otherwise fixing the class.  That wouldn't make any sense at all would it?

This fixes the underlying problem:  It forces date subtraction to yield a value that is exact to one second.  The floating point rounding errors cancel out.  Paste this code above the sample in your question, and the ASSERT will never occur.

inline COleDateTimeSpan COleDateTime::operator-(const COleDateTime& date) const
{
     COleDateTimeSpan spanResult;
     if (GetStatus() == null || date.GetStatus() == null) {
          spanResult.SetStatus(COleDateTimeSpan::null);
          return spanResult;
     }
     if (GetStatus() == invalid || date.GetStatus() == invalid)     {
          spanResult.SetStatus(COleDateTimeSpan::invalid);
          return spanResult;
     }
    spanResult= COleDateTimeSpan( m_dt- date.m_dt );
    spanResult.SetDateTimeSpan(0,0,0, (int)spanResult.GetTotalSeconds() );
    return( spanResult );
}
=-=-=-=-=-=-=-=-=-=-=-=-=-
However, if you are against any kind of subclassing or program code replacement, then there is no solution.  And that is the "complete and only" answer.  So I have locked this question.

-- Dan

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hiron1pAuthor Commented:
Thanks Dan.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.