Q:

Python checking daytime

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?

A:

You want datetime

The datetime module supplies classes for manipulating dates and times in both simple and complex ways

If you use date.hour from datetime.now() you'll get the current hour:
datetimenow = datetime.now();
if datetimenow.hour in range(4, 5)
    sleep(time_left_till_5am)

You can calculate time_left_till_5am by taking 60 - datetimenow.minute multiplying by 60 and adding to 60 - datetimenow.second.

A:

Python has a built-in datetime library: http://docs.python.org/library/datetime.html
This should probably get you what you're after:
import datetime as dt
from time import sleep

now = dt.datetime.now()

if now.hour >= 4 andnow.hour < 5:
    sleep((60 - now.minute)*60 + (60 - now.second))

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):
import datetime as dt
from time import sleep

now = dt.datetime.now()
pause = dt.datetime(now.year, now.month, now.day, 4)
start = dt.datetime(now.year, now.month, now.day, 5)

if now >= pause and now < start:
    sleep((start - now).seconds)

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).

