Check if date range is filled in Javascript with Node.js

I am building a scheduling application with Express. I have a calendar view that displays a monthly calendar.

Each day must be completely covered; whether shifts overlap or not, there must be at least one person on a shift at all times.

Given an array of shifts for the day that look like this

{
    start: [javascript Date],
    end: [javascript Date],
    name: "James Bond",
    ...
}

I want to conditionally add a CSS class to highlight the day if more shifts are needed to fill it.

What is the best way tell if the time between x and y is completely filled?

You need to show what you have tried, but I will give you pseudocode for two basic approaches for this problem.

With either approach, make sure that you are using half-opened intervals for your shifts. This is represented a few different ways:

// interval notation:
[start, end)

// algebraicly
start <= value < end

// javascript and other code
start <= value && end > value 

Approach #1

  • Use Date.getTime() on each date to get a simple numeric representation of each date.
  • Find a library that implements an Interval Tree.
  • Map all your shifts onto the tree.
  • Flatten/merge the tree.
  • Look for gaps.

Approach #2

  • Initialize a counter i = 0.
  • Initialize a dictionary or list of key/value pairs.
  • For each shift in the list:
    • If your start date x is in the shift, then increment i.
    • See if the start time is in the dictionary.
      • If not, add it with the key being the start datetime and the value being 1.
      • If it is already in the dictionary, increment the value.
    • See if the end time is in the dictionary.
      • If not, add it with the key being the start datetime and the value being -1.
      • If it is already in the dictionary, decrement the value.
  • Sort the dictionary by the datetime keys.
  • Walk the dictionary, starting from x.
    • i should be starting with the number of people currently working.
    • Add the value of each dictionary item to your counter i.
    • If i == 0 then nobody is working. Return false or error.
    • If you've followed the logic, then i should never be negative. Error if it is.
    • Continue until your end date y is reached.