comparing datetime with date
Steve Holden
sholden at holdenweb.com
Wed Sep 15 08:43:02 EDT 2004
Andrew Durdin wrote:
> On Tue, 14 Sep 2004 17:16:58 -0400, Tim Peters <tim.peters at gmail.com> wrote:
>
>>[Donnal Walter]
>>
>>>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?
>>
>>They should not be. Please open a bug report. The problem is due to that
>>datetime.datetime is a subclass of datetime.date:
>
>
> Why should this be considered a bug? In my conception, a datetime.date
> covers the whole range of times within the date, so that this equality
> makes sense. It also allows for intuitive inequality comparisons
> between datetime.datetime and datetime.date.
>
Well you do, of course, allow that this appeals to *your* intuition, but
it seems much more reasonable to me to assume that a date, when compared
to a datetime, should specify a single canonical time (such as midnight
at the start of that date).
> To make that more clear, it makes sense to me that comparisons between
> datetime.datetime and datetime.date should only compare the date part,
> and those between datetime.datetime and datetime.time should only
> compare the time part. This latter however throws an exception; I
> infer from that that the 'pythonic' way is to explicitly make a
> datetime.date or datetime.time from the datetime.datetime before
> comparing. So I guess I've answered my own question :)
Comparing a datetime and a time makes little sense to me, and I'd prefer
to be forced to extract the time explicitly from the datetime before
comparison. A time specifies a point during *any* day, a datetime
specifies a single point in the time continuum.
When all's said and done I guess this thread mostly highlights the
unsatisfactory aspects of intuition when used as a basis for agreement
on complex software specifications :-)
regards
Steve
More information about the Python-list
mailing list