def sleep_interval(interval, now, wake_up_second):
    """
    Work out how long to sleep (so we don't drift over time)

    Only works properly when interval is at least one minute and the
    runtime between calls is less than interval (although it will
    return a reasonable value in both these situations).

    :param interval: number of seconds between cycles
    :param now: the current time
    :param wake_up_second: modula time to wake up at
    :return: number of seconds to sleep
    """
    next_wakeup = float(now + interval)
    if wake_up_second > interval:
        return next_wakeup - now
    next_wakeup = next_wakeup - (next_wakeup % wake_up_second)
    return next_wakeup - now