def seglength(pyccd_result, ordinal, series_start):
    """
    The number of days since the beginning of the segment that the ordinal
    intersects with. The days between and around segments identified through
    the change detection process comprise valid segments for this. This why we
    need to know when the actual start ordinal, as the segments identified
    through change detection might not include it.

    Defaults to 0 in cases where the given ordinal day to calculate from
    is either < 1 or is before the start of the time series.

    Args:
        pyccd_result: dict return from pyccd
        ordinal: ordinal day to calculate from
        series_start: ordinal day when the change detection was started from

    Returns:
        int
    """
    ret = 0

    if ordinal > 0:
        all_dates = [series_start]

        for segment in pyccd_result['change_models']:
            all_dates.append(segment['start_day'])
            all_dates.append(segment['end_day'])

        diff = [(ordinal - d) for d in all_dates if (ordinal - d) > 0]

        if diff:
           ret = min(diff)

    return ret