[Tutor] Accuracy of time.sleep()

Dave S pythontut at pusspaws.net
Sat Dec 4 15:24:45 CET 2004


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
  


More information about the Tutor mailing list