C
C#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.
J
Jake45d ago
I added a Library now called TimePeriodLibrary.NET, it doesnt help either. Same issue. Does anyone have any ideas?
C
canton745d 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
J
Jake45d 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
L
lycian45d 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;
Want results from more Discord servers?
Add your server
More Posts
clamping how fast a input (as a float) can change.Let’s say my Input X float changes in a amount of time from -1 to 1 (-1, -0.5, 0, 0.5, 1) so as an eIValueResolver won't work with AutoMapperHi guys , hope all of you doing well, I'm recently working on a project where I need to convert a spHelp with concurrency blocking issueI have a small app that runs CPU bound on multiple threads (amount of processors) On one PC it workFrontend not workingI dont really keen on frontend but I think have some knowledge about it would be nice, I'm learning JsonSerializerOptionsTo be continuedC# Dictionary questionHi, if i have a dictionary in C# defined like so: `Dictionary<string, List<int>> test;` and I do `Li✅ Avalonia not showing rows in datagridThere are no rows in my datagrid in Avalonia, the headers are visible. I've checked if my list is emInfinite loop```c# /// ------------------------------------------------------------ /// <summary> /// Creates a sHow to connect my c# backend with the frontend?Hello, I created a ASP NET Core Web Api project with the command dotnet new webapi -minimal. In my ✅ Using `Func<ReadOnlySpan<char>,bool>` but not allowed due to Pointer TypeI am building a parser, and performance is of concern. So instead of allocating infinite strings wheconcurrency and atomicity Asp.net core web api and efcorelet's say there's an endpoint called AssingUser() ,this endpoint assigns one donation to this currenBuilding a generic repository for mongodbhello folks I was thinking build a generic repository structure for mongodb. but someday if i want Reading large xml file from archive by using XmlReader in Parallel modeHello 👋. I am looking for how can I read data from archive xml file in Parallel mode. I have archiResizing the Console window - Console ApplicationHi there, I'm trying to resize the console window in a Console Application. I have tried multiple ✅ Creating a datetime with a timezoneHow do I create a `DateTime` with `new DateTime(2010, 05, 12);` and give it a specific `TimeZoneInfoCustom Newtonsoft JSON deserializationI have a dictionary where: - The key will always be a string - The value will be either: - string Issue with Form.Close() in WinformsI have a custom form called CustomMessageBox In that form, I have a method that defines a button clIssue with IMGUI.Net.Hello, I am creating an ImGUI application and It doesn't let me move the ui across my entire screen,Issue with deserializing Enums in saved JSONHey, I've been trying to deserialize a list of cards I serialized in C#, the issue I'm having is it From HttpClient, can I save/copy the request to share with someone?Is there a way to copy raw request details? For example, I'm looking for a similar output like: -