from datetime import datetime


def find_avg_since(index, dt, patients):  # test
    """Calculates average of previous heart rates since specified date/time

    Args:
        index (int): index of patient to be located in list
        dt (datetime): datetime
        patients (dictionary): list of patients in database

    Returns:
        int: average heart rate
    """
    HR_list = patients[index]["heart_rate"]
    time_list = patients[index]["time_stamp"]
    sum = 0
    length = 0
    for i in range(len(time_list)):
        if dt > datetime.strptime(time_list[i], '%Y-%m-%d %H:%M:%S.%f'):
            continue
        sum += HR_list[i]
        length += 1
    if length == 0:
        return False
    avg_HR_since = sum / length
    return int(avg_HR_since)