Test to see if the edtf candidate is a valid interval

def is_valid_interval(edtf_candidate):
    """Test to see if the edtf candidate is a valid interval"""

    # resolve interval into from / to datetime objects
    from_date = None
    to_date = None
    # initialize interval flags for special cases, assume positive
    end, start = 'pos', 'pos'
    if edtf_candidate.count('/') == 1:
        # replace all 'problem' cases (unspecified, 0000 date, ?~, -, y)
        # break the interval into two date strings
        edtf_candidate = replace_all(edtf_candidate, interval_replacements)
        edtf_candidate = re.sub(U_PATTERN, replace_u, edtf_candidate)
        parts = edtf_candidate.split('/')
        # set flag for negative start date
        if parts[0].startswith("-"):
            start = 'neg'
            parts[0] = parts[0][1:]
        # set flag for negative end date
        if parts[1].startswith("-"):
            end = 'neg'
            parts[1] = parts[1][1:]
        # if starts positive and ends negative, that's always False
        if start == 'pos' and end == 'neg':
            return False
        # handle special case of 0000 year
        if parts[0].startswith("0000") or parts[1].startswith("0000"):
            return zero_year_special_case(parts[0], parts[1], start, end)
        # 2 '-' characters means we are matching year-month-day
        if parts[0].count("-") == 2:
            from_date = datetime.datetime.strptime(parts[0], "%Y-%m-%d")
        if parts[1].count("-") == 2:
            to_date = datetime.datetime.strptime(parts[1], "%Y-%m-%d")
        # 1 '-' character means we are match year-month
        if parts[0].count("-") == 1:
            from_date = datetime.datetime.strptime(parts[0], "%Y-%m")
        if parts[1].count("-") == 1:
            to_date = datetime.datetime.strptime(parts[1], "%Y-%m")
        # zero '-' characters means we are matching a year
        if parts[0].count("-") == 0:
            # if from_date is unknown, we can assume the lowest possible date
            if parts[0] == 'unknown':
                from_date = datetime.datetime.strptime("0001", "%Y")
            else:
                from_date = datetime.datetime.strptime(parts[0], "%Y")
        if parts[1].count("-") == 0:
            # when the to_date is open and the from_date is valid, it's valid
            if parts[1] == 'open' or parts[1] == 'unknown':
                to_date = 'open'
            else:
                to_date = datetime.datetime.strptime(parts[1], "%Y")
        # if it starts negative and ends positive, that's always True
        if start == 'neg' and end == 'pos':
            return True
        # if start and end are negative, the from_date must be >= to_date
        elif start == 'neg' and end == 'neg':
            if from_date >= to_date and from_date and to_date:
                return True
        # if the to_date is unknown or open, it could be any date, therefore
        elif (
            parts[1] == 'unknown' or
            parts[1] == 'open' or
            parts[0] == 'unknown'
        ):
            return True
        # if start and end are positive, the from_date must be <= to_date
        elif start == 'pos' and end == 'pos':
            if from_date <= to_date and from_date and to_date:
                return True
        else:
            return False
    else:
        return False