comparing datetime with date

Graeme Matthew graeme.matthew at contrado.com.au
Tue Sep 14 19:24:03 EDT 2004


This is very interesting because if you continue further and coerce both to
strings you get:

>>> print str(y) == str(y)
True
>>> str(y)
'2004-09-14 06:43:15'
>>> str(x)
'2004-09-14'
>>>

Even though there string values are different they still match?

If we perform a CRC (Cyclic Redundancy Check) i.e compute a one way hash:

import binascii
>>> crc_x = binascii.crc32(str(x))
>>> crc_y = binascii.crc32(str(y))
>>> print crc_x == crc_y
False

So this confirms that there values do differ yet a comparision using ==
returns True?

All i can assume is that when a Date / DateTime comparision is made the
'lowest denominator' i.e Date is used but I may be very wrong

Will watch this thread closely

"Donnal Walter" <donnal at donnal.net> wrote in message 
news:mailman.3292.1095162740.5135.python-list at python.org...
>I was very surprised to discover that
>
> >>> import datetime
> >>> x = datetime.date(2004, 9, 14)
> >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15)
> >>> print x == y
> True
>
> How can these two objects be considered equal? Is there a *general* way to 
> test for date != datetime as well as 4.5 != 4.6?
>
> Donnal Walter
> Arkansas Children's Hospital
>
> 





More information about the Python-list mailing list