an qua la ngu
This commit is contained in:
21
src/common/custom_types/schedule.date.type.d.ts
vendored
Normal file
21
src/common/custom_types/schedule.date.type.d.ts
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
export type ScheduleDateJSON = {};
|
||||
|
||||
export type ScheduleDate = Date | ScheduleDateJSON;
|
||||
|
||||
/*
|
||||
|
||||
[id:startTimeStamp - endTimeStamp, id:startTimeStamp - endTimeStamp, ...]
|
||||
|
||||
*/
|
||||
|
||||
|
||||
1728206200 - 1728206300
|
||||
1728206400 - 1728206500
|
||||
1728206600 - 1728206700
|
||||
1728206800 - 1728206900
|
||||
1728207000 - 1728207100
|
||||
1728207200 - 1728207300
|
||||
1728207400 - 1728207500
|
||||
1728207600 - 1728207700
|
||||
|
||||
|
||||
69
src/common/utils/datetime.utils.ts
Normal file
69
src/common/utils/datetime.utils.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
function getOverlapRange(
|
||||
startA: number,
|
||||
endA: number,
|
||||
startB: number,
|
||||
endB: number
|
||||
) {
|
||||
const overlapStart = Math.max(startA, startB);
|
||||
const overlapEnd = Math.min(endA, endB);
|
||||
|
||||
return overlapStart < overlapEnd ? { overlapStart, overlapEnd } : null;
|
||||
}
|
||||
|
||||
function isOverlap(
|
||||
startA: number,
|
||||
endA: number,
|
||||
startB: number,
|
||||
endB: number
|
||||
) {
|
||||
return getOverlapRange(startA, endA, startB, endB) !== null;
|
||||
}
|
||||
|
||||
// Generate random time ranges for the schedulers
|
||||
function generateTimeRanges(count: number): { start: number; end: number }[] {
|
||||
const ranges = [];
|
||||
const startDate = new Date('2024-10-06T00:00:00Z').getTime();
|
||||
for (let i = 0; i < count; i++) {
|
||||
const start = startDate + Math.floor(Math.random() * 10000000); // Random start time
|
||||
const end = start + Math.floor(Math.random() * 10000000); // Random end time after start
|
||||
ranges.push({ start, end });
|
||||
}
|
||||
return ranges;
|
||||
}
|
||||
|
||||
// Benchmark performance of overlap detection between two schedulers
|
||||
const schedulerA = generateTimeRanges(50);
|
||||
const schedulerB = generateTimeRanges(50);
|
||||
|
||||
console.time('Overlap Benchmark');
|
||||
let overlapCount = 0;
|
||||
for (const rangeA of schedulerA) {
|
||||
for (const rangeB of schedulerB) {
|
||||
if (isOverlap(rangeA.start, rangeA.end, rangeB.start, rangeB.end)) {
|
||||
overlapCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.timeEnd('Overlap Benchmark');
|
||||
|
||||
console.log(`Total Overlaps Found: ${overlapCount}`);
|
||||
|
||||
// Example usage with specific time ranges
|
||||
const startA = new Date('2024-10-06T10:00:00Z').getTime();
|
||||
const endA = new Date('2024-10-06T12:00:00Z').getTime();
|
||||
const startB = new Date('2024-10-06T11:00:00Z').getTime();
|
||||
const endB = new Date('2024-10-06T13:00:00Z').getTime();
|
||||
|
||||
const overlapRange = getOverlapRange(startA, endA, startB, endB);
|
||||
if (overlapRange) {
|
||||
console.log(
|
||||
`Overlap Start: ${new Date(overlapRange.overlapStart).toISOString()}`
|
||||
);
|
||||
console.log(
|
||||
`Overlap End: ${new Date(overlapRange.overlapEnd).toISOString()}`
|
||||
);
|
||||
|
||||
console.log('Is overlap: true');
|
||||
} else {
|
||||
console.log('No overlap');
|
||||
}
|
||||
Reference in New Issue
Block a user