2 Replies - 201 Views - Last Post: 09 April 2019 - 02:00 AM Rate Topic: -----

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

overlapping times

Posted 09 April 2019 - 01:11 AM

I have four DateTime values, let's call them startA, endA and startB, endB. I want to check whether they overlap. (It is for a car booking and the booking will be cancelled if the times overlap.)

My challenge is that the date aspects are all mucked up. The four values I have (initially as strings) are:

2019 04 03 17:30 startA
1899 12 30 23:30 endA

2019-04-03T13:00:00 startB
2019-04-03T14:00:00 endB

It could also be that startA is in 1899.

These times do not overlap.

So I want to completely ignore the dates as I know they refer to the same day.

Would you create four new DateTime instances at todays' date and include the time parts? Or is there a way to compare simply the times without complex expressions?

If constructing new DateTimes is there a way to simply 'graft' the time from the existing elements?

Is This A Good Question/Topic? 0
  • +

Replies To: overlapping times

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: overlapping times

Posted 09 April 2019 - 01:49 AM

TimeOfDay helped me to resolve this. I was able to extract these TimeSpans and directly compare them.

        foreach (XElement xe2 in tableD)
        {
            DateTime existingStartTime;
            DateTime existingEndTime;

            foreach (var itemStart in xe2.Elements("StartTime"))
            {
                if (DateTime.TryParse(itemStart.Value, out existingStartTime))
                {
                    foreach (var itemEnd in xe2.Elements("EndTime"))
                    {
                        if (DateTime.TryParse(itemEnd.Value, out existingEndTime))
                        {
                            TimeSpan startA = pcBookings.StartTime.TimeOfDay;
                            TimeSpan endA = pcBookings.EndTime.TimeOfDay;

                            TimeSpan startB = existingStartTime.TimeOfDay;
                            TimeSpan endB = existingEndTime.TimeOfDay;

                            // check if the times overlap
                            if ((startA <= endB) && (endA >= startB))
                            {
                                timeOverlaps = true;
                            }
                        }
                        break;      // there is only 1 item
                    }
                }
                break;      // there is only 1 item
            }

            cnt++;
        }


My parsing of XElements is a bit unnecessary, though. Each element xe2 only contains a singe StartTime and EndTime so I shouldn't have to use foreach, twice, just to get these values. If anyone has a suggestion how to get the single element(s) I would appreciate it.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6795
  • View blog
  • Posts: 28,082
  • Joined: 12-December 12

Re: overlapping times

Posted 09 April 2019 - 02:00 AM

Nevermind, I can use Element (Doh!) - I hadn't initially picked up that this would accept a string value.

foreach (XElement xe2 in tableD)
{
    DateTime existingStartTime;
    DateTime existingEndTime;

    var itemStart = xe2.Element("StartTime");
    
    if (DateTime.TryParse(itemStart.Value, out existingStartTime))
    {
        var itemEnd = xe2.Element("EndTime");
            
        if (DateTime.TryParse(itemEnd.Value, out existingEndTime))
        {
            TimeSpan startA = pcBookings.StartTime.TimeOfDay;
            TimeSpan endA = pcBookings.EndTime.TimeOfDay;

            TimeSpan startB = existingStartTime.TimeOfDay;
            TimeSpan endB = existingEndTime.TimeOfDay;

            // check if the times overlap
            if ((startA <= endB) && (endA >= startB))
            {
                timeOverlaps = true;
            }
        }
            
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1