/// <summary> /// 合并时段。如果有相交的时段,则合并。 /// </summary> /// <param name="spans">时段集合</param> /// <returns></returns> public static List <BufferedTimePeriod> Merge(List <BufferedTimePeriod> spans) { List <BufferedTimePeriod> outs = new List <BufferedTimePeriod>(); spans.Sort(); BufferedTimePeriod last = null; foreach (var item in spans) { if (last == null) { last = item; continue; } if (last.BufferedContains(item.Start)) { last = BufferedTimePeriod.Merge(last, item); } else { outs.Add(last); last = null; } } if (last != null) { outs.Add(last); } return(outs); }
/// <summary> /// 返回结束时间最大的区段 /// </summary> /// <param name="prevObj">时段1</param> /// <param name="other">时段2</param> /// <returns></returns> public static BufferedTimePeriod Max(BufferedTimePeriod first, BufferedTimePeriod other) { if (first.End > other.End) { return(first); } return(other); }
/// <summary> /// 返回起始时间最小的区段 /// </summary> /// <param name="prevObj">时段1</param> /// <param name="other">时段2</param> /// <returns></returns> public static BufferedTimePeriod Min(BufferedTimePeriod first, BufferedTimePeriod other) { if (first.Start < other.Start) { return(first); } return(other); }
/// <summary> /// 合并两个时间段,不管中间是否具有间隙。 /// </summary> /// <param name="prevObj">第一个时段</param> /// <param name="other">第二个时段</param> /// <returns></returns> public static BufferedTimePeriod Merge(BufferedTimePeriod first, BufferedTimePeriod other) { Time min = Time.Min(first.Start, other.Start); Time max = Time.Max(first.End, other.End); BufferedTimePeriod minPeriod = Min(first, other); BufferedTimePeriod maxPeriod = Max(first, other); return(new BufferedTimePeriod(min, max, minPeriod.StartBuffer, maxPeriod.EndBuffer)); }
/// <summary> /// 时段构造函数 /// </summary> /// <param name="timePeriod"></param> public BufferedTimePeriod(BufferedTimePeriod timePeriod) : this(timePeriod.Start, timePeriod.End) { this.StartBuffer = timePeriod.StartBuffer; this.EndBuffer = timePeriod.EndBuffer; }
/// <summary> /// 合并这两个时段 /// </summary> /// <param name="other">另一个时段</param> /// <returns></returns> public BufferedTimePeriod Merge(BufferedTimePeriod other) { return(Merge(this, other)); }
/// <summary> /// 起始时间在前的为小。 /// 返回值的含义如下:值含义小于零此对象小于 other 参数。零此对象等于 other。大于零此对象大于 other。 /// </summary> /// <param name="other"></param> /// <returns></returns> public int CompareTo(BufferedTimePeriod other) { return((int)this.Start.CompareTo(other.Start)); }