Why can't timedeltas be divided?

Robert Kern robert.kern at gmail.com
Wed May 24 20:26:51 EDT 2006


Maric Michaud wrote:
> Le Jeudi 25 Mai 2006 00:07, Dan Bishop a écrit :
> 
>>If I try to write something like:
>>
>>    num_weeks = time_diff / datetime.timedelta(days=7)
> 
> because it has no meaning,

Yes, it does.

> what you want is :
> 
> num_weeks = time_diff.days / 7
> or
> num_weeks = (time_diff / 7).days

Uh, no. Besides the integer division problem in your first line, keep in mind
that the .days attribute does not give you the time interval measured in days.
It gives you the number of *whole* days in the interval. The first method will
be incorrect if time_diff is not an even multiple of 1 day. The latter will be
incorrect if time_diff is not an even multiple of 7 days.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list