Basically, I want my script to pause between 4 and 5 AM. The only way to do this I’ve come up with so far is this:
seconds_into_day = time.time() % (60*60*24)
if 60*60*4 < seconds_into_day < 60*60*5:
sleep(time_left_till_5am)
Any “proper” way to do this? Aka some built-in function/lib for calculating time; rather than just using seconds all the time?
Python has a built-in datetime library: http://docs.python.org/library/datetime.html
This should probably get you what you’re after:
OK, the above works, but here’s the purer, less error-prone solution (and what I was originally thinking of but suddenly forgot how to do):
That’s where my original “timedelta” comment came from — what you get from subtracting two datetime objects is a timedelta object (which in this case we pull the ‘seconds’ attribute from).