def _round_to(dt, hour, minute, second):
    """
    Route the given datetime to the latest time with the hour, minute, second
    before it.
    """
    new_dt = dt.replace(hour=hour, minute=minute, second=second)
    if new_dt == dt:
        return new_dt
    elif new_dt < dt:
        before = new_dt
        after = new_dt + timedelta(days=1)
    elif new_dt > dt:
        before = new_dt - timedelta(days=1)
        after = new_dt

    d1 = dt - before
    d2 = after - dt

    if d1 < d2:
        return before
    elif d1 > d2:
        return after
    else:
        return before
Route the given datetime to the latest time with the hour, minute, second
    before it.