[Zope3-dev] Dates and times

Guido van Rossum guido@python.org
Fri, 15 Nov 2002 15:59:14 -0500


> > What's your use case for adding a timedelta to a time?
> 
> I like making houmous.
> 
> Generally on friday evenings, I soak the chickpeas (garbanzos, to 
> Americans).
> On Saturday morning I can boil them. I don't always do this on fridays 
> and saturdays, though. Sometimes I do it on different days.
> 
> I need to boil them for three hours, changing the water every hour.
> 
> My schedule for attending to this is to start at 10am, then change the 
> water one hour later, and one additional hour after that. One further 
> hour later, the chickpeas are boiled.
> 
> I want to represent this schedule as a set of times in a day. The 
> algorithm for coming up with the appointments in a day starts with me 
> giving the time I want to start it.
> 
>    Start time                         10am
>    Time of first change of water      10am + 1 hour  = 11am
>    Time of second change of water     10am + 2 hours = 12pm
>    ... and so forth
> 
> When I want to apply this schedule of related appointments to a 
> particular day (that is, a datetime).
> 
> Another more common example is planning a workshop. The activities in a 
> workshop are of a known length. You sometimes need to plan the days of a 
> workshop, and then choose the dates later.

It's a good use case, but the proposed "time" type is not a good
match.  The workshop example makes it clear that you're looking at a
timespan of over 24 hours, and arithmetic modulo 24 hours won't do you
any good.

I think it's better done using a timedelta.  You initialize it to the
number of hours from midnight on the first day.  Your application will
have to translate hours greater than 24 into days, but that's trivial
math.  Once you pick the date, everything will fall into place.

--Guido van Rossum (home page: http://www.python.org/~guido/)