0

given startTime is 3:00 AM given endTime is 5:00 AM

//        3am |----------------------------| 5am

///these four conditions should be not matched, these four comes partially in the given range,

//             3:30am |-------------| 4:30am

//    2am|---------------|4am


//               3:45am|----------------------|6am

// 1am|-------------------------------------------------|7am

previously what I have done is,

"starttime": {"$gte": starttime, "$lte": endtime},
"endtime": {"$gte": starttime, "$lte": endtime},

in mongo query to find check existing slots. if query gives result then i don't insert slot and give response that slot not available.

2 Answers2

0

Basically you want to check if there are any conflicts for a given time range. You have a reference range (3am to 5am) and different cases which colide with it.

This is a very naive approach to solve the problem:

# for the sake of simplicity I use just numbers in the example
reference = (3, 5)
case_a = (3.5, 4.5)
case_b = (2, 4)
case_c = (3.75, 6)
case_d = (1, 7)

def check_for_conflicts(reference, case):
    if case[0] > reference[0] and case[0] < reference[1] \
    or case[1] > reference[0] and case[1] < reference[1] \
    or case[0] < reference[0] and case[1] > reference[1]:
        return True
    return False

There are 3 lines in the if condition, the first one checks if the start is within the reference range, the second if the end is within the reference range and the third if the reference range is enclosed within the given case.

I don't know how this translates to a MongoDB query. I reckon there are Python libraries that solve the problem with more elegant code.

cezar
  • 11,616
  • 6
  • 48
  • 84
  • thanks for your kind efforts. your logic is correct, btw i found a solution. – KAUSHAL KORADIYA Oct 11 '19 at 09:27
  • @KAUSHALKORADIYA It's great that you've found a solution. Please post your solution as an answer, other users will be grateful. – cezar Oct 11 '19 at 09:38
  • 1
    You only need to check that (case[0] <= reference[1] AND case[1] >= reference[0]) to determine that the two ranges overlap. See [here](https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap) – dirkgroten Oct 11 '19 at 11:33
  • no it doesn't work check different conditions by putting values. – KAUSHAL KORADIYA Oct 11 '19 at 11:37
  • @dirkgroten thanks for the comment! Yes, indeed, that should work fine, and it's much more elegant. – cezar Oct 24 '19 at 09:24
0

update:

new solution : perfect solution Logic: (Max(StartA, StartB) <= Min(EndA, EndB)

older solution:

if you want to find that given slot is already filled

st = givenstarttime in timstamp et = givenendtime in timstamp

db.getCollection('exam').find({
$or : [
        { 
            $and : [ 
            {"startTimeStamp": {"$gte": st, "$gte":et  }, 
            {"endTimeStamp": {"$gte": st, "$gte":et  }} 
            ] 
        },
        { 
            $and : [
            {"startTimeStamp": {"$lte": st, "$lte": et }}, 
            {"endTimeStamp": {"$lte": st, "$lte": et}} 
            ] 
        }
      ]
    })

explaination:

Ex:

  • startTimeStamp = 3:00 AM (already created and stored in db)
  • endTimeStamp = 5:00 AM (already created and stored in db)

  • givenStartTimeStamp(st) = 2:00 AM (we want to store in db)

  • givenEndTimeStamp(et) = 4:00 AM (we want to store in db)

which are described below:

        3am |------------------| 5am


 2am|---------------|4am

so it shoudn't insert in db, because if it insert's now then when we fetch it give multiple data for some time period.

so what i did is i checked in first and condition is that st and et is grater than startTimestamp, and also endtime is grater than st and et, then in another and condition checked if both time is less than startTime and Endtime,

so if data is bad than whole condition becomes false else any one condition becomes True.

[Note: it solves all four conditions.]

update: (shorter solution)

(Max(StartA, StartB) <= Min(EndA, EndB)