C
C#3mo ago
Jake

Im having an issue checking for collisions when comparing DateTime objects.

I have this function that's supposed to check for collisions with bookings. I am using TimeSpans and DateTime objects. I found a post on Stackoverflow that this...
private bool CheckBooking(List<Booking> sameDayBookings, DateTime newStart, DateTime newEnd)
{
Span<Booking> itemSpan = CollectionsMarshal.AsSpan(sameDayBookings);
ref var searchSpace = ref MemoryMarshal.GetReference(itemSpan);

for (var i = 0; i < itemSpan.Length; i++)
{
var item = Unsafe.Add(ref searchSpace, i);
var existingStart = item.DateTime;
var existingEnd = item.DateTime.Add(item.Duration);
bool overlap = newStart < existingEnd && existingStart < newEnd;

if (overlap)
{
return false;
}
}
return true;
}
private bool CheckBooking(List<Booking> sameDayBookings, DateTime newStart, DateTime newEnd)
{
Span<Booking> itemSpan = CollectionsMarshal.AsSpan(sameDayBookings);
ref var searchSpace = ref MemoryMarshal.GetReference(itemSpan);

for (var i = 0; i < itemSpan.Length; i++)
{
var item = Unsafe.Add(ref searchSpace, i);
var existingStart = item.DateTime;
var existingEnd = item.DateTime.Add(item.Duration);
bool overlap = newStart < existingEnd && existingStart < newEnd;

if (overlap)
{
return false;
}
}
return true;
}
When using this code during normal time periods from 12am to 11pm it works perfectly fine but when I do a check for a time thats supposed to go into a new day it doesnt take that into account. Im not sure what else I could add to this function to take that into account. For example if i have a booking that's at 11:30:00pm and the timespan is for an hour, it should in theory be booked until 12:30:00am. But for some reason when I submit a booking for 12am it returns true.
4 Replies
Jake
Jake3mo ago
I added a Library now called TimePeriodLibrary.NET, it doesnt help either. Same issue. Does anyone have any ideas?
canton7
canton73mo ago
All the stuff with CollectionsMarshal and Unsafe is completely unnecessary. Just loop through your list The rest looks fine though. There's nothing in that code which cares about what day it is
Jake
Jake3mo ago
The problem is, I need to take the day into account. I want to make sure that there is a collision when there is an overlap Do you know a way I can do that? I need to make sure that there is a collision error on the situation i specified in the post when it returns true. it should be false because its an overlap. I managed to fix it, it wasnt the comparison after all. It was me looping through an empty array. I wasnt fetching the correct bookings lol
lycian
lycian3mo ago
I think canton7 was mentioning that you're basically getting unsafe references for no reason. you're whole thing can basically be
c#
var overlap = sameDayBookings
.Any(item =>
item.Date < newEnd && // existingStart < newEnd
newStart < item.DateTime.Add(item.Duration) // newStart < existingEnd
);

return !overlap;
c#
var overlap = sameDayBookings
.Any(item =>
item.Date < newEnd && // existingStart < newEnd
newStart < item.DateTime.Add(item.Duration) // newStart < existingEnd
);

return !overlap;
or if you still want to loop
c#
foreach (var item in sameDayBookings)
{
var existingStart = item.DateTime;
var existingEnd = item.DateTime.Add(item.Duration);
bool overlap = newStart < existingEnd && existingStart < newEnd;

if (overlap)
{
return false;
}
}

return true;
c#
foreach (var item in sameDayBookings)
{
var existingStart = item.DateTime;
var existingEnd = item.DateTime.Add(item.Duration);
bool overlap = newStart < existingEnd && existingStart < newEnd;

if (overlap)
{
return false;
}
}

return true;