[Python-Dev] datetime module enhancements

Christian Heimes lists at cheimes.de
Sun Mar 11 22:56:10 CET 2007


Jon Ribbens schrieb:
> I see you snipped without response my request to back up your claim
> that "assuming that a date() is a datetime() with a time of midnight
> will clearly break that logic".

I've another idea. Date and datetime objects are compared by date. The
date object for a given date is always smaller than the datetime for the
same day - even for midnight. The date 2007-01-02 is smaller than
2007-01-02 00:00:00 but larger than 2007-01-01 24:00:00.

>>> date(2007, 1, 2) < datetime(2007, 1, 2, 0, 0, 0)
True
>>> date(2007, 1, 2) > datetime(2007, 1, 1, 24, 0, 0)
True
>>> date(2007, 1, 2) == datetime(2007, 1, 2, 0, 0, 0)
False
>>> date(2007, 1, 2) in datetime(2007, 1, 2, 0, 0, 0)
TypeError(...)

>>> datetime(2007, 1, 2, 0, 0, 0) < date(2007, 1, 2)
TypeError(...)
>>> datetime(2007, 1, 2, 0, 0, 0) > date(2007, 1, 2)
TypeError(...)
>>> datetime(2007, 1, 2, 0, 0, 0) == date(2007, 1, 2)
False
>>> datetime(2007, 1, 2, 0, 0, 0) in date(2007, 1, 2)
True



More information about the Python-Dev mailing list