Solved

COleDateTime problem

Posted on 2001-08-09
22
1,114 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:hiron1p
  • 9
  • 4
  • 3
  • +2
22 Comments
 
LVL 12

Expert Comment

by:migel
ID: 6368539
Hi!
just use this code:
if (fabs(oneHour-res)> 0.1)
assert(false);
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6368594
Hi,
ASSERT(res.GetTotalSeconds() == oneHour.GetTotalSeconds());

Xu
0
 

Author Comment

by:hiron1p
ID: 6368618
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.
0
 

Author Comment

by:hiron1p
ID: 6368639
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!)
0
 
LVL 12

Expert Comment

by:migel
ID: 6369107
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)))

0
 
LVL 2

Expert Comment

by:xu2000
ID: 6371003
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
0
 

Author Comment

by:hiron1p
ID: 6371445
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.
0
 
LVL 1

Expert Comment

by:Tim_Musschoot
ID: 6371579
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
0
 

Author Comment

by:hiron1p
ID: 6371645
Tim,

Thanks, but please read my previous comments.
0
 
LVL 1

Expert Comment

by:Tim_Musschoot
ID: 6371681
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
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6371772
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.

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:hiron1p
ID: 6371947
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.
0
 
LVL 1

Expert Comment

by:Tim_Musschoot
ID: 6371971
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
0
 
LVL 12

Expert Comment

by:migel
ID: 6372303
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.
0
 

Author Comment

by:hiron1p
ID: 6372911
migel,

My last comment stated that I have subclassed the COleDateTimeSpan class!!!
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6378288
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
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6420372
Hi hiron1p,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 

Author Comment

by:hiron1p
ID: 6421203
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).
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6421384
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
0
 

Author Comment

by:hiron1p
ID: 6421606
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.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 300 total points
ID: 6425638
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
0
 

Author Comment

by:hiron1p
ID: 6431096
Thanks Dan.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now