comparing datetime with date

Andrew Durdin adurdin at gmail.com
Tue Sep 14 21:18:47 EDT 2004


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.

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 :)



More information about the Python-list mailing list