[Tutor] Accuracy of time.sleep()
Jacob S.
keridee at jayco.net
Sun Dec 5 21:41:03 CET 2004
You know, since time.sleep() builds up errors, this is what I do to keep it
purely pythonic... (not tested)
from time import gmtime
alarmhr = 8
alarmmin = 5
alarmsec = 0
while 1:
t = gmtime()
hour = t[3]
min = t[4]
sec = t[5]
if (alarmhr,alarmmin,alarmsec) == (hour,min,sec):
print "It is 8:05 AM. Please do whatever you are supposed to at this
time.
raw_input()
break
> OK I may be pushing it, ;-)
>
> I need a script to sleep from any point to 8:05AM when in needs to
> re-start.
>
> So I calculate the number of seconds with the following ....
>
>
>
>
> def secs_till_805():
> # Returns the number of seconds till 8:05AM
>
> secs_5min=5*60
> secs_24hr=24*60*60
> secs_8hr=(8*60*60)+secs_5min
> secs_8hr_24hr=secs_24hr-secs_8hr
>
> hours=int(strftime('%H'))
> mins=int(strftime('%M'))
> secs=int(strftime('%S'))
>
> sec_left=secs_24hr-((hours*60*60)+(mins*60)+secs)
>
> # If we are before 8:05, then ...
> if sec_left>secs_8hr_24hr:
> return sec_left-secs_8hr_24hr
>
> # If we are after 8:05, then ...
> return sec_left+secs_8hr
>
>
>
> Then I ...
>
> sleep(secs_till_805())
>
> I expected the script to re-start 2-3 seconds after 8:05, python
> reloading after a long sleep etc, what I get is the script restarting at
> 08:04.55, earlier ???
>
> OK this is not a world stopping problem, more of a curiosity.
>
> Any suggestions
>
> Dave
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list