I'm working on an Angular project where I need to verify if the time intervals of different courses overlap. I'm using the areIntervalsOverlapping function from the date-fns library. However, it seems that the function is returning true for cases where there shouldn't be an overlap.
Here is the code I'm using:
import { areIntervalsOverlapping } from 'date-fns';
const selectedCourse = {
id: 1,
name: 'Chemical Engineering',
week: 1,
capacity: 12,
active: 1,
start_time_1: 1719241200,
end_time_1: 1719248400,
start_time_2: 1719586800,
end_time_2: 1719594000
};
const courses = [
{ id: 1, name: 'Chemical Engineering', week: 1, capacity: 12, active: 1, start_time_1: 1719241200, end_time_1: 1719248400, start_time_2: 1719586800, end_time_2: 1719594000 },
{ id: 2, name: 'Mechanical Engineering', week: 1, capacity: 25, active: 1, start_time_1: 1719248400, end_time_1: 1719255600, start_time_2: 1719594000, end_time_2: 1719601200 },
{ id: 3, name: 'Industrial Engineering', week: 1, capacity: 25, active: 1, start_time_1: 1719241200, end_time_1: 1719259200, start_time_2: 1719586800, end_time_2: 1719604800 },
{ id: 4, name: 'Mechatronics Engineering Group 1', week: 1, capacity: 24, active: 1, start_time_1: 1719241200, end_time_1: 1719248400, start_time_2: 1719586800, end_time_2: 1719594000 },
{ id: 5, name: 'Mechatronics Engineering Group 2', week: 1, capacity: 24, active: 1, start_time_1: 1719252000, end_time_1: 1719259200, start_time_2: 1719597600, end_time_2: 1719604800 },
{ id: 6, name: 'Fashion Design', week: 1, capacity: 30, active: 1, start_time_1: 1719241200, end_time_1: 1719255600, start_time_2: 1719500400, end_time_2: 1719514800 },
{ id: 7, name: 'Financial Engineering', week: 1, capacity: 30, active: 1, start_time_1: 1719241200, end_time_1: 1719255600, start_time_2: 1719500400, end_time_2: 1719514800 },
{ id: 8, name: 'Communication, Art and Culture Rally (Morning Group)', week: 1, capacity: 60, active: 1, start_time_1: 1719244800, end_time_1: 1719259200, start_time_2: 1719590400, end_time_2: 1719604800 },
{ id: 9, name: 'Communication, Art and Culture Rally (Afternoon Group)', week: 1, capacity: 60, active: 1, start_time_1: 1719262800, end_time_1: 1719277200, start_time_2: 1719608400, end_time_2: 1719622800 },
{ id: 10, name: 'Education Sciences', week: 1, capacity: 25, active: 1, start_time_1: 1719331200, end_time_1: 1719342000, start_time_2: 1719504000, end_time_2: 1719514800 },
{ id: 11, name: 'Architecture', week: 1, capacity: 60, active: 1, start_time_1: 1719331200, end_time_1: 1719342000, start_time_2: 1719504000, end_time_2: 1719514800 },
{ id: 12, name: 'Philosophy and Social Sciences', week: 1, capacity: 25, active: 1, start_time_1: 1719500400, end_time_1: 1719509400, start_time_2: 1719586800, end_time_2: 1719595800 }
];
courses.forEach(c => {
const selectedStart = new Date(selectedCourse.start_time_1 * 1000);
const selectedEnd = new Date(selectedCourse.end_time_2 * 1000);
const courseStart = new Date(c.start_time_1 * 1000);
const courseEnd = new Date(c.end_time_2 * 1000);
console.log(`Comparing intervals:\nCourse: ${c.name} (${c.id}) ${courseStart} - ${courseEnd}\nSelected: ${selectedCourse.name} (${selectedCourse.id}) ${selectedStart} - ${selectedEnd}`);
const isOverlapping = areIntervalsOverlapping(
{ start: courseStart, end: courseEnd },
{ start: selectedStart, end: selectedEnd }
);
console.log(`Course ${c.name} (${c.id}) overlaps with selected: ${isOverlapping}`);
});
and all the results were (TRUE)