Difference between two dates in seconds

Robert Kern robert.kern at gmail.com
Wed Sep 27 19:41:14 EDT 2006


Steven D'Aprano wrote:
> On Wed, 27 Sep 2006 20:16:52 +0200, Fredrik Lundh wrote:
> 
>> Claes at work wrote:
>>
>>> Please tell me there is a simpler way than subtracting two datetimes
>>> to get a timedelta and then compute
>>>
>>> days * number of seconds per day + seconds
>>>
>>> from it myself??
>> why would you have to do that yourself?  why not let Python do it for 
>> you?  here's the code:
>>
>>     seconds = td.days * 86400 + td.seconds
> 
> I'm sure somebody as experienced as Fredrik doesn't need to be told why
> peppering code with magic constants like 84600 are a less than ideal
> solution -- and for those who do need to be told, carefully compare what I
> wrote to what Fredrik wrote to see one of those reasons.

Well, you don't pepper your code with magic numbers. You write a well-named 
function and pepper your code with calls to that function.

-- 
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