To calculate open slots in a calendar, you will need to iterate over the given time range and cross-reference it with the provided weekday times and blocked slots. Let's create a TypeScript function to do this. We will represent the time as simple strings in the format "HH:mm".
Here is an example TypeScript code snippet to calculate open slots in a calendar:
typescript
type Weekday = 'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday';
type TimeSlot = { start: string; end: string };
type BlockedSlot = { date: string; start: string; end: string };
function parseTime(time: string): Date {
const [hours, minutes] = time.split(':').map(Number);
return new Date(0, 0, 0, hours, minutes);
}
function isTimeInRange(time: Date, start: Date, end: Date): boolean {
return time >= start && time <= end;
}
function calculateOpenSlots(
weekdayTimes: Record<Weekday, TimeSlot[]>,
blockedSlots: BlockedSlot[],
rangeStart: string,
rangeEnd: string
): TimeSlot[] {
const startDate = new Date(rangeStart);
const endDate = new Date(rangeEnd);
let openSlots: TimeSlot[] = [];
for (let date = new Date(startDate); date <= endDate; date.setDate(date.getDate() + 1)) {
const dayOfWeek = date.toLocaleString('en-US', { weekday: 'long' }) as Weekday;
const dailySlots = weekdayTimes[dayOfWeek] || [];
dailySlots.forEach((slot) => {
const slotStart = new Date(date);
const slotEnd = new Date(date);
const [startHours, startMinutes] = slot.start.split(':').map(Number);
const [endHours, endMinutes] = slot.end.split(':').map(Number);
slotStart.setHours(startHours, startMinutes);
slotEnd.setHours(endHours, endMinutes);
// Check if slot is within the range
if (slotEnd < startDate || slotStart > endDate) {
return;
}
// Check against blocked slots
let isBlocked = false;
for (const blocked of blockedSlots) {
const blockedDate = new Date(blocked.date);
if (date.toDateString() === blockedDate.toDateString()) {
const blockedStart = parseTime(blocked.start);
const blockedEnd = parseTime(blocked.end);
blockedStart.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
blockedEnd.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
if (
isTimeInRange(slotStart, blockedStart, blockedEnd) ||
isTimeInRange(slotEnd, blockedStart, blockedEnd) ||
(slotStart <= blockedStart && slotEnd >= blockedEnd)
) {
isBlocked = true;
break;
}
}
}
if (!isBlocked) {
openSlots.push({
start: slot.start,
end: slot.end
});
}
});
}
return openSlots;
}
// Usage example:
const weekdayTimes: Record<Weekday, TimeSlot[]> = {
'Monday': [{ start: '09:00', end: '17:00' }],
'Tuesday': [{ start: '09:00', end: '17:00' }],
'Wednesday': [{ start: '09:00', end: '17:00' }],
'Thursday': [{ start: '09:00', end: '17:00' }],
'Friday': [{ start: '09:00', end: '17:00' }],
// Assuming no slots on weekends for simplicity
'Saturday': [],
'Sunday': []
};
const blockedSlots: BlockedSlot[] = [
{ date: '2023-04-10', start: '10:00', end: '12:00' },
{ date: '2023-04-11', start: '14:00', end: '16:00' }
// More blocked slots as needed
];
const openSlots = calculateOpenSlots(weekdayTimes, blockedSlots, '2023-04-10T00:00:00Z', '2023-04-12T23:59:59Z');
console.log(openSlots);
This TypeScript code will provide you with the open slots within the given date range, excluding the blocked slots.
Keep in mind that this is a simplified version and assumes all times are in the same timezone. You may need to adjust it to handle different timezones, daylight saving changes, or more complex scheduling logic.