Skip to content
Advertisement

Convert a list of objects with a range of minutes to an hourly list

I have a list with minute blocks objects, it contains the start and end hour/minute, according to this I must build a new list of blocks by hour and minutes occupied. Always in the same range below one hour, for this example only 30 minutes, and they cannot overlap.

Example of minute block (30-minute block starting at 00:00 and ending at 0:30).

let blockMinute = {
  hourInit: 0,
  minuteInit: 0,
  hourEnd: 0,
  minuteEnd: 30
}

Example hour block (expected).

[
{
  hourInit: 0,
  hourEnd: 1,
  accuMinutes: 30
}
]

A new time block must be created, because it starts at 0:00 and ends at 0:30, then the occupied hour is from 0 to 1, and occupies 30 minutes in that hour.

Another example is for a 30-minute block starting at 0:45 and ending at 1:15.

let blockMinute2 = {
    hourInit: 0,
    minuteInit: 45,
    hourEnd: 1,
    minuteEnd: 15
}
// Expected result
[
{
    hourInit: 0,
    hourEnd: 1,
    accuMinutes: 15 //unresolved
},
{
    hourInit: 1,
    hourEnd: 2,
    accuMinutes: 15 //unresolved
}
]

Two hour blocks must be created, because it starts at 0:45 and ends at 1:15, so it creates two hours from 0 to 1 and 1 to 2, and of these hours it occupies 15 minutes in each one. The block was 30 minutes, but it was divided between the two hours.

This is my solution:

I have tried to create a solution but it gives me a lot of problems when a block of minutes crosses an hour it should generate blocks of hours and the minutes it occupies, I can’t determine them. And also when they are at the limit of an hour, for example from 0:30 to 1:00, it counts as two hours, when it should be one from 0 to 1.

let blockMinute = {
    hourInit: 0,
    minuteInit: 0,
    hourEnd: 0,
    minuteEnd: 30
}

let blockMinute2 = {
    hourInit: 0,
    minuteInit: 45,
    hourEnd: 1,
    minuteEnd: 15
}
// Always the same range, for example all 30 minutes
let blocksMinute = [blockMinute, blockMinute2];

let blockHourExpected = [];
blocksMinute.forEach((blockMinute) => {
  let hourInit = blockMinute.hourInit;
  let minuteInit = blockMinute.minuteInit;
  let hourEnd = blockMinute.hourEnd;
  let minuteEnd = blockMinute.minuteEnd;
  for (let i = hourInit; i <= hourEnd; i++) {
    let exist = blockHourExpected.find((blockHour) => blockHour.hourInit == i);
    if (!exist) {
      blockHourExpected.push({
        hourInit: i,
        hourEnd: i + 1,
        accuMinutes: 0 //unresolved
      })
    }
  }
});
console.log(blockHourExpected)

Expected result

let blockHourExpectedExample = [{
    hourInit: 0,
    hourEnd: 1,
    accuMinutes: 45
 },
  {
    hourInit: 1,
    hourEnd: 2,
    accuMinutes: 15
 }
]

Advertisement

Answer

Seems you’re already almost there. It’s kinda simple algorithm puzzle. I’ve added a few lines to your codes and seems like it gives the result what you want to get. This should work for all cases even when the meeting is over 1 hour.

let blockMinute1 = { // 0:00 - 0:30
  hourInit: 0,
  minuteInit: 0,
  hourEnd: 0,
  minuteEnd: 30
}
let blockMinute2 = { // 0:50 - 1:20
  hourInit: 0,
  minuteInit: 50,
  hourEnd: 1,
  minuteEnd: 20
}
let blockMinute3 = { // 1:30 - 2:10
  hourInit: 1,
  minuteInit: 30,
  hourEnd: 2,
  minuteEnd: 10
}

let blocksMinutes = [blockMinute1, blockMinute2, blockMinute3];
let blockHourExpected = [];

blocksMinutes.forEach((blockMinute) => {
  let hourInit = blockMinute.hourInit;
  let minuteInit = blockMinute.minuteInit;
  let hourEnd = blockMinute.hourEnd;
  let minuteEnd = blockMinute.minuteEnd;

  for (let i = hourInit; i <= hourEnd; i++) {
    let accuMinutes = 0;
    if (hourInit === i && hourEnd === i)
      accuMinutes = minuteEnd - minuteInit;
    else if (hourInit === i && i < hourEnd)
      accuMinutes = 60 - minuteInit;
    else if (hourInit < i && i < hourEnd)
      accuMinutes = 60;
    else
      accuMinutes = minuteEnd;

    let exist = blockHourExpected.find((blockHour) => blockHour.hourInit == i);
    if (exist) {
      exist.accuMinutes += accuMinutes;
    } else {
      blockHourExpected.push({
        hourInit: i,
        hourEnd: i + 1,
        accuMinutes: accuMinutes
      })
    }
  }
});

console.log(blockHourExpected);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement