[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1254
  • Last Modified:

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.
0
hiron1p
Asked:
hiron1p
  • 9
  • 4
  • 3
  • +2
1 Solution
 
migelCommented:
Hi!
just use this code:
if (fabs(oneHour-res)> 0.1)
assert(false);
0
 
xu2000Commented:
Hi,
ASSERT(res.GetTotalSeconds() == oneHour.GetTotalSeconds());

Xu
0
 
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.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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!)
0
 
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)))

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
0
 
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.
0
 
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
0
 
hiron1pAuthor Commented:
Tim,

Thanks, but please read my previous comments.
0
 
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
0
 
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.

0
 
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.
0
 
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
0
 
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.
0
 
hiron1pAuthor Commented:
migel,

My last comment stated that I have subclassed the COleDateTimeSpan class!!!
0
 
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
0
 
DanRollinsCommented:
Hi hiron1p,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
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).
0
 
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
0
 
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.
0
 
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
0
 
hiron1pAuthor Commented:
Thanks Dan.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 9
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now