comparing DateTime instances

Mark McEahern mark at mceahern.com
Fri Feb 1 18:22:19 EST 2002


Skip Montanaro [mailto:skip at pobox.com]:
> Why go to the trouble of calling cmp() explicitly?  "==" seems to work for
> me:
[sample code]
> See?  No need to stretch your brain out of shape after all... ;-)

That's an excellent question and all I can say is that I got all paranoid
after reading this:

"The internal representation of date/times behaves much like floats do in
Python, i.e. rounding errors can occur when doing calculations. There is a
special compare function included (cmp()) in the package that allows you to
compare two date/time values using a given accuracy, e.g.
cmp(date1,date2,0.5) will allow 12:00:00.5 and 12:00:01.0 to compare equal."

  http://www.lemburg.com/files/python/mxDateTime.html

Because prior to the comparison test, I basically do all kinds of dateObj2 =
dateObj1 + RelativeDateTime(whatever).  So I actually use cmp(a, b, 0.5) in
my tests and that's good enough equality for me.  I didn't want the tests to
fail just because of rounding errors.

I think I really just needed to post that in case someone else came along
with the same missing neurons as me.  It helped me to work through it too.

Cheers,

// mark





More information about the Python-list mailing list