APS算法-截取时间范围内的时间段

2019PHP高薪工程师学习路线图....>>>

/// <summary>
/// 截取时间范围内的时间段
/// </summary>
public static List<SJDuan> GetBetween(this SortedList<DateTime, SJDuan> duans, DateTime begin, DateTime end)
{
    DebugHelper.Start("GetBetween"); // 用于时间统计
    
    var rslt = new List<SJDuan>();
    if (end > begin)
    {
        var first = duans.Keys.GetFirstIndexBefore(begin);
        if (first == null)
        {
            first = 0;
        }
        var last = duans.Keys.GetFirstIndexAfter(end);
        if (last == null)
        {
            last = duans.Count - 1;
        }
        else
        {
            last = last - 1;
        }

        for (int i = first.Value; i <= last; i++)
        {
            var duan = duans.Values[i];
            if (duan.end > begin && duan.begin < end)
            {
                var dbegin = duan.begin > begin ? duan.begin : begin;
                var dend = duan.end < end ? duan.end : end;
                rslt.Add(new SJDuan(dbegin, dend));
            }
        }
    }
    DebugHelper.Stop("GetBetween");
    return rslt;
}