Solved

COleDateTime problem

Posted on 2001-08-09
22
1,159 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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