Difference between two times (working ugly code, needs polish)
Steve Holden
steve at holdenweb.com
Tue Sep 11 18:09:52 EDT 2007
Shawn Milochik wrote:
> On 9/11/07, Grant Edwards <grante at visi.com> wrote:
>> On 2007-09-11, Shawn Milochik <Shawn at Milochik.com> wrote:
>>
>>> I have done what I wanted, but I think there must be a much better way.
>> See the strptime() function in either the time or the datetime
>> modules:
>>
>> http://docs.python.org/lib/module-time.html
>> http://docs.python.org/lib/module-datetime.html
>>
>
>
> Grant:
>
> Thanks, this works, and is much shorter. Any further improvements, anyone?
>
> def isOld(lastUpdate, runTimeStamp):
>
> oneDay = 60 * 60 * 24
>
> lastUpdate = time.mktime(time.strptime(lastUpdate, "%Y-%m-%d_%H:%M"))
> runTimeStamp = time.mktime(time.strptime(runTimeStamp, "%Y-%m-%d_%H:%M"))
>
> return (runTimeStamp - lastUpdate) / oneDay
I suppose really oneDay should be a global (i.e. outside the function
definition). Apart from that it would be hard to improve on: obvious,
easy to read, in short - pythonic.
Are you concerned about daylight savings? That could certainly introduce
a whole new level of complexity into the problem. Let's hope not ...
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
More information about the Python-list
mailing list