comparing datetime with date

Donnal Walter donnal at donnal.net
Wed Sep 15 13:21:05 EDT 2004


Robert Brewer wrote:
> Given your birthdate/time example, I'd look seriously at providing four
> Cell subclasses: str, int, date, and time. Separating date and time
> completely allows either to be None (= unknown). If consumer code needs
> to compare both date and time, you can then write:
> 
> import datetime
> 
> class Thing(object):
>     def __init__(self, d, t):
>         self.d = DateCell(d)
>         self.t = TimeCell(t)
> 
>     def dt(self):
>         return datetime.datetime.combine(self.d, self.t)
> 
> x = Thing(datetime.date(2004, 9, 15), None)
> y = Thing(datetime.date(2004, 9, 15), datetime.time(11, 59))
> if x.dt == y.dt:
>     do_stuff()
> 
> ...the 'if' statement will then raise TypeError, since x.t is None and
> won't combine. You can then trap that either inside or outside the dt()
> method and handle it as you like.

Except that the only code that needs to *compare* two values is found in 
the superclass Cell.set() method.

     def set(self, value):
         if value != self._state:	# the only comparison
             self._state = value
             self.notify_observers(self)
             if self._depth > 0:
                 self._depth -= 1
                 self.feedback()
                 self._depth += 1

One would prefer not to have to override this method (and reproduce the 
code) in subclasses. Moreover the TimePoint is considered a single value 
by consumer objects (presenters and Interval observers). The Interval 
observers supply a time of 00:00 when the time is unknown, but the 
various presenters (a DateTimeField, for example) display '__:__' 
instead of '00:00' (unless 00:00 really was entered by the user).

Regards,
Donnal Walter
Arkansas Children's Hospital





More information about the Python-list mailing list