[Python-ideas] [Python-Dev] datetime module enhancements

Collin Winter collinw at gmail.com
Mon Mar 12 05:59:53 CET 2007


[Moving to python-ideas...]

On 3/9/07, Steven Bethard <steven.bethard at gmail.com> wrote on python-dev:
> On 3/9/07, Collin Winter <collinw at gmail.com> wrote on python-dev:
> > One solution that just occurred to me -- and that skirts the issue of
> > choosing an interpretation -- is that, when comparing date and
> > datetime objects, the datetime's .date() method is called and the
> > result of that call is compared to the original date. That is,
> >
> > datetime_obj < date_obj
> >
> > is implicitly equivalent to
> >
> > datetime_obj.date() < date_obj
>
> Using the .date() is fine when the year/month/day doesn't match.  So
> the following are fine::
>     datetime.datetime(2005, 1, 1, 0, 0, 0) < datetime.date(2006, 1, 1)
>     datetime.datetime(2007, 1, 1, 0, 0, 0) > datetime.date(2006, 1, 1)
> It's *not* okay to say that a date() is less than, greater than or
> equal to a datetime() if the year/month/day *does* match.  The correct
> temporal relation is During, but Python doesn't have a During
> operator. During is not the same as less-than, greater-than or
> equal-to, so all of these should be False::
>     datetime.datetime(2006, 1, 1, 0, 0, 0)  < datetime.date(2006, 1, 1)
>     datetime.datetime(2006, 1, 1, 0, 0, 0)  > datetime.date(2006, 1, 1)
>     datetime.datetime(2006, 1, 1, 0, 0, 0)  == datetime.date(2006, 1, 1)
> That is, the datetime() is not less than, greater than or equal to the
> corresponding date().
>
> Some discussion of these kinds of issues is here:
>     http://citeseer.ist.psu.edu/allen94actions.html
> The essence is that in order to properly compare intervals, you need
> the Meets, Overlaps, Starts, During and Finishes operators in addition
> to the Before (<) and Simulaneous (=) operators.
>
> So, let's not conflate Before, After or Simultaneous with the other
> relations -- if it's not strictly Before (<), After (>) or
> Simultaneous (=), we can just say so by returning False.

It might be neat to add a __contains__ method to date() objects so
that "datetime(2007, 1, 1, 0, 0, 0) in date(2007, 1, 1)" would be
True. This would seem to fulfill the During operator.

Collin Winter



More information about the Python-ideas mailing list