[Python-Dev] datetime module enhancements

Collin Winter collinw at gmail.com
Fri Mar 9 20:53:23 CET 2007

On the subject of datetime enhancements, I came across an SF patch
(#1673403) the other day that proposed making it possible to compare
date and datetime objects. Quoting from the patch summary:

Comparing a date to a datetime currently throws an exception. This makes no
sense. In what way is:

datetime(2006, 1, 1, 0, 0, 0) < date(2007, 1, 1)

not a perfectly reasonable and well-defined comparison? Throwing an
exception here violates the "Principle of Least Surprise" to a considerable

Obviously some slight ambiguity arises if the date and the datetime differ
only in the time part. There are two sensible responses in this situation
that I can see:

Treat dates as if they have a time-part of midnight. This is my preferred
solution, and it is already what the datetime module does, for example,
when subtracting two dates.

Treat dates as if they refer to the entire day, i.e. if the date and
datetime differ only in the time part then they are equal. This is
consistent but becomes confusing in other situations such as when
subtracting dates.

Any thoughts on this?

Collin Winter

More information about the Python-Dev mailing list