Click here to Skip to main content
12,751,848 members (29,545 online)
Click here to Skip to main content
Add your own
alternative version

Stats

97K views
17 bookmarked
Posted 14 Nov 2001

Comparing COleDateTime objects

, 14 Nov 2001
Rate this:
Please Sign up or sign in to vote.
Solution to the bad working COleDateTime comparing methods

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 = ...; //init with the first moment
DateTimeWalker = StartTime;
for (int i=0; i<NR_OF_QUARTERS; i++)
{
    //do something with DateTimeWalker
    ...
    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;
    //do something with TimeToUse
    ...
}

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Geert Delmeiren
Software Developer (Senior)
Belgium Belgium
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 started programming in JAVA (and Swing)
Later on I also worked in Adobe Flex, GWT and GXT.
Recently, since our products became more web based, I started programming in JavaScript and Sencha ExtJS.

You may also be interested in...

Pro

Comments and Discussions

 
Questionhow COleDateTime create m_dt (Double),what isformula? Pin
vahid_m_200817-Jun-08 4:34
membervahid_m_200817-Jun-08 4:34 
Questionhow COleDateTime create m_dt (Double),what it formula? Pin
vahid_m_200817-Jun-08 4:34
membervahid_m_200817-Jun-08 4:34 
GeneralPlease don't do this! Pin
Marc Brooks28-Nov-01 13:09
memberMarc Brooks28-Nov-01 13:09 
GeneralRe: Please don't do this! Pin
Geert Delmeiren28-Nov-01 21:24
memberGeert Delmeiren28-Nov-01 21:24 
GeneralQuestion Pin
Brian V Shifrin16-Nov-01 5:05
memberBrian V Shifrin16-Nov-01 5:05 
GeneralRe: Question Pin
Geert Delmeiren18-Nov-01 22:36
memberGeert Delmeiren18-Nov-01 22:36 
GeneralRe: Question Pin
Brian V Shifrin6-Jan-02 7:31
memberBrian V Shifrin6-Jan-02 7:31 
GeneralRe: Question Pin
Geert Delmeiren6-Jan-02 21:48
memberGeert Delmeiren6-Jan-02 21:48 
GeneralRe: Question Pin
Sachu13-Feb-02 2:27
memberSachu13-Feb-02 2:27 
GeneralRe: Question Pin
Beaker18-Nov-01 23:02
memberBeaker18-Nov-01 23:02 
Generalquick fix? Pin
Strifter16-Jan-03 7:06
memberStrifter16-Jan-03 7:06 
GeneralRe: quick fix? Pin
Geert Delmeiren16-Jan-03 21:19
sussGeert Delmeiren16-Jan-03 21:19 
GeneralRe: quick fix? Pin
Geert Delmeiren16-Jan-03 21:28
sussGeert Delmeiren16-Jan-03 21:28 
GeneralRe: quick fix? Pin
D N Harris18-Mar-03 11:14
memberD N Harris18-Mar-03 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 15 Nov 2001
Article Copyright 2001 by Geert Delmeiren
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid