How to calculate active shifts given daily schedule and a time window?

I’d like additional pairs of eyes to help me break through my blocker here.

I have a daily schedule of shifts for workers, that provides start times for shifts e.g. one activity has the following daily shift pattern: 7am, 3pm, 11pm (i.e. 3 8-hour shifts). Multiple activities can exist at the same time.

Given an unrestricted time window e.g. 6am to 10pm, I need to calculate what shifts are covered in the time window, and how much time of the shift is covered in that time window.

But I’m struggling and I’m terrible with maths. Can anyone help point me in the right direction of approaching this problem?

This is for a personal project and isn’t part of any course, accredited or otherwise.

Thanks!

1 Like

I think we’d need a bit more information. Perhaps a complete worked through example of what you are trying to achieve? Are all the shift types static? Would a good starting point be to have an object for each shift pattern, with the times of the shift and the number of hours in each shift saved as properties of the object?

1 Like

More info, sure. The design can change but here’s what I have so far:

A Shift contains an array of Time objects (24h representation of time) e.g. [7am, 3pm, 11pm]. These Time objects also have a member variable with time representation as milliseconds of hours (i.e. 7am is 25200000ms). I’ve attempted to use this ms value to set JS Date hour/minute/second values.

A shift pattern is static, but between each time window calculation new shifts can be added/removed, so each time window’s results must be calculated independently.

Shift pattern examples, for clarity:

  • 8-hour 3-shift pattern: 7am, 3pm, 11pm
  • 12-hour 2-shift pattern: 8am, 8pm
  • Double day shift pattern (16h, 8h): 6am, 10pm

Complete worked through example:
3 activities exist:

  • Activity1: 1 shift pattern of 7am, 3pm, 11pm
  • Activity2: 1 shift pattern of 6am, 10pm
  • Activity3: 1 shift pattern of 7am, 3pm, 11pm

Time window examples and expected results:

  • 7am to 9am:
    • Activity1 shift 1 (2 hours),
    • Activity2 shift 1 (2 hours),
    • Activity3 shift 1 (2 hours)
  • 6pm to 8pm:
    • Activity1 shift 2 (2 hours),
    • Activity2 shift 1 (2 hours),
    • Activity3 shift 2 (2 hour)
  • 11:50pm to 12:30pm:
    • Activity1 shift 3 (40 mins),
    • Activity2 shift 2 (40 mins),
    • Activity3 shift 3 (40 mins)
  • Day 1 9am to Day 2 1pm:
    • Activity1 shift 1 (8 hours),
    • Activity1 shift 2 (8 hours),
    • Activity1 shift 3 (8 hours),
    • Activity1 day 2 shift 1 (6 hours),
    • Activity2 shift 1 (13 hours),
    • Activity2 shift 2 (8 hours),
    • Activity2 day 2 shift 1 (7 hours),
    • Activity3 shift 1 (8 hours),
    • Activity3 shift 2 (8 hours),
    • Activity3 shift 3 (8 hours),
    • Activity3 day 2 shift 1 (6 hours)

I hope the above clarifies. There’s more to this, such as some shifts not being allocated a worker, and some shifts being not attached to a 24h cycle but rather different cycle (but that is hopefully just a conversion to 24h format issue).

I ended up using ChatGPT after finding the appropriate problem name ‘interval overlap’.

Here’s the solution in js, :

AI code removed by moderator

do not share code written by AI please