Dick Moores said unto the world upon 2004-12-05 11:17:
I'm wondering if my timer3.py could have been written more simply and easily by using the time or datetime modules to get the number of seconds separating the time now and the time to which the alarm is set.
IOW, is there an easier way to calculate the time difference between the time now, say 08:51 and say, tomorrow at 03:45, to take an example of the most difficult case?


See timer3.py at
<http://www.rcblue.com/Python/timer3_ForWeb.py>

Thanks, tutors.

Dick Moores
[EMAIL PROTECTED]


Hi Dick and all,

as you may recall, it was a week or so ago that I was show how to use datetime, so I'd be cautious about using my suggestion without testing :-)

But, does this do what is wanted?

<code with Python 2.4>
import datetime

def dif_in_seconds(dif):
    return dif.days * (24 * 60 * 60) + dif.seconds

t1 = datetime.datetime(2004, 12, 5, 8, 51, 00)
t2 = datetime.datetime(2004, 12, 6, 15, 45, 00)
dif = t2 - t1

seconds_dif = dif_in_seconds(dif)
print seconds_dif
</code>

with output

>>>
111240

I didn't check your link, or read your thread closely, so I don't know if this counts as "easier". But it does look pretty darned easy :-)

Best,

Brian vdB

_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to