javascript - Converting busy time to free time -
i have set of data busy times during day , want predict unbusy time somehow subtracting time slots day.
i'm wondering what's best approach/algorithm that.
[ '20:30 21:30', '11:00 12:00', '07:30 08:50', '09:00 20:00' ]
what i'm thinking right make array 24 blocks of day free [1-2, 2-3, 3-4, 4-5 ..etc] , somehow process starting time , deduct spot ex. 1.30 2.00 turn free block of 1 1-2 1-1.30. start not sure if work
i had similar problem, had busy slots of person, , wanted find time slots person available ("free"). coded, hope can someone:
function getfreeofday (date, busyslots) { function samedatedifferenttime(date, hours, minutes) { return new date(date.getfullyear(), date.getmonth(), date.getdate(), hours, minutes, 0, 0); } // define range free spots var freeslots = date.getday() === 0 || date.getday() === 6 ? [] : [ { start: samedatedifferenttime(date, 10, 0), // 10:00 (am) end: samedatedifferenttime(date, 12, 30), // 12:30 (am) }, { start: samedatedifferenttime(date, 13, 30), // 13:30 (am) end: samedatedifferenttime(date, 19, 0), // 19:00 (am) } ]; // go through busy slots, remove them free spots busyslots.foreach(function (busyslot) { freeslots.foreach(function (freeslot, freeslotindex) { if (busyslot.end <= freeslot.start || busyslot.start >= freeslot.end) { // nothing, busy slot doesn't interfere free slot } else if (busyslot.start <= freeslot.start && busyslot.end >= freeslot.end) { // free slot in middle of busy slot, meaning it's not possible plan in there freeslots.splice(freeslotindex, 1); } else if (busyslot.start < freeslot.start && busyslot.end > freeslot.start) { // busy slot overlaps free slot, ends after start of free slot freeslots[freeslotindex] = { start: busyslot.end, end: freeslot.end }; } else if (busyslot.start < freeslot.end && busyslot.end > freeslot.end) { // busy slot overlaps free slot, starts before end of free slot freeslots[freeslotindex] = { start: freeslot.start, end: busyslot.start }; } else { // busy slot in middle of free slot freeslots[freeslotindex] = { start: freeslot.start, end: busyslot.start }; freeslots.splice(freeslotindex + 1, 0, { start: busyslot.end, end: freeslot.end }); } }); }); // remove empty free slots freeslots.foreach(function (freeslot, freeslotindex) { if (freeslot.start >= freeslot.end) { freeslots.splice(freeslotindex, 1); } }); return freeslots;
Comments
Post a Comment