四个日期时间对象相互接触/重叠

本文关键字:接触 重叠 对象 时间 四个 日期 | 更新日期: 2023-09-27 18:26:34

我正试图找出两个DateTime对象是否与其他两个DateTime对象相互接触/重叠。

有很多案例我正在努力核实。但我想知道是否有什么有效的方法可以以不同的方式做到这一点?也许是LINQ扩展?

public IQueryable<Appointment> GetAllAppointments(DateTime startTime, DateTime endTime)
        {
            return _dbContext.Appointment.Where(x => x.Appointment.StartTime <= startTime && x.Appointment.EndTime >= endTime && x.Appointment.StartTime <= endTime && x.Appointment.EndTime <= startTime
                                    || x.Appointment.StartTime >= startTime && x.Appointment.EndTime <= endTime && x.Appointment.StartTime <= endTime && x.Appointment.EndTime <= startTime
                                    || x.Appointment.StartTime == startTime && x.Appointment.EndTime == endTime && x.Appointment.StartTime <= endTime && x.Appointment.EndTime <= startTime
                                    || x.Appointment.StartTime >= startTime && x.Appointment.EndTime >= endTime
                                    || x.Appointment.StartTime <= startTime && x.Appointment.EndTime >= endTime);
        }

约会不应该互相"碰"。约会结束后,下一个开始约会不应在上一个日期和时间结束的日期和时间开始。

四个日期时间对象相互接触/重叠

您可以使用.NET的时间段库来检测重叠/接触的时间段:

// ----------------------------------------------------------------------
public void TimeRangeSample()
{
  // --- time range 1 ---
  TimeRange timeRange1 = new TimeRange(
    new DateTime( 2011, 2, 22, 14, 0, 0 ),
    new DateTime( 2011, 2, 22, 18, 0, 0 ) );
  Console.WriteLine( "TimeRange1: " + timeRange1 );
  // > TimeRange1: 22.02.2011 14:00:00 - 18:00:00 | 04:00:00
  // --- time range 2 ---
  TimeRange timeRange2 = new TimeRange(
    new DateTime( 2011, 2, 22, 15, 0, 0 ),
    new TimeSpan( 2, 0, 0 ) );
  Console.WriteLine( "TimeRange2: " + timeRange2 );
  // > TimeRange2: 22.02.2011 15:00:00 - 17:00:00 | 02:00:00
  // --- time range 3 ---
  TimeRange timeRange3 = new TimeRange(
    new DateTime( 2011, 2, 22, 16, 0, 0 ),
    new DateTime( 2011, 2, 22, 21, 0, 0 ) );
  Console.WriteLine( "TimeRange3: " + timeRange3 );
  // > TimeRange3: 22.02.2011 16:00:00 - 21:00:00 | 05:00:00
  // --- relation ---
  Console.WriteLine( "TimeRange1.GetRelation( TimeRange2 ): " +
                     timeRange1.GetRelation( timeRange2 ) );
  // > TimeRange1.GetRelation( TimeRange2 ): Enclosing
  Console.WriteLine( "TimeRange1.GetRelation( TimeRange3 ): " +
                     timeRange1.GetRelation( timeRange3 ) );
  // > TimeRange1.GetRelation( TimeRange3 ): EndInside
  Console.WriteLine( "TimeRange3.GetRelation( TimeRange2 ): " +
                     timeRange3.GetRelation( timeRange2 ) );
  // > TimeRange3.GetRelation( TimeRange2 ): StartInside
  // --- intersection ---
  Console.WriteLine( "TimeRange1.GetIntersection( TimeRange2 ): " +
                     timeRange1.GetIntersection( timeRange2 ) );
  // > TimeRange1.GetIntersection( TimeRange2 ):
  //             22.02.2011 15:00:00 - 17:00:00 | 02:00:00
  Console.WriteLine( "TimeRange1.GetIntersection( TimeRange3 ): " +
                     timeRange1.GetIntersection( timeRange3 ) );
  // > TimeRange1.GetIntersection( TimeRange3 ):
  //             22.02.2011 16:00:00 - 18:00:00 | 02:00:00
  Console.WriteLine( "TimeRange3.GetIntersection( TimeRange2 ): " +
                     timeRange3.GetIntersection( timeRange2 ) );
  // > TimeRange3.GetIntersection( TimeRange2 ):
  //             22.02.2011 16:00:00 - 17:00:00 | 01:00:00
} // TimeRangeSample