Define the capacity being scheduled
First decide what a reservation consumes: one staff member, a room, equipment, or several resources together. A candidate is valid only when every required resource is eligible and free for the full interval.
Opening hours are usually a business-level outer boundary. Staff working hours narrow that boundary; service eligibility determines which staff are candidates. Split shifts and overnight windows need explicit normalization rather than assumptions based on one start/end pair.
Build the candidate interval
For each eligible resource, intersect business opening windows with working schedules. Remove breaks, time off, holidays, and other schedule exceptions. For a service duration d and buffers before/after, reserve [start − before, start + d + after) when checking collisions, while showing the customer the service's actual start and end.
Generate start times on a declared increment, such as every 15 minutes, only if that increment matches the business rules. A 15-minute grid does not imply every appointment lasts 15 minutes.
Account for existing reservations
Use half-open intervals [start, end) so one appointment ending at 10:00 can be followed by another beginning at 10:00 when there is no buffer. Overlap exists when candidateStart < bookedEnd and candidateEnd > bookedStart. Decide which statuses consume capacity; a cancelled appointment normally should not, while a pending hold may consume it for a limited time.
- Exclude inactive staff and services.
- Filter by location and all resource requirements.
- Apply buffers consistently to candidate and existing intervals.
- Cap result windows and paginate for large calendars.
Resolve time zones before comparing
Store appointment instants in a consistent absolute representation such as UTC. Store the business's IANA zone for recurring local opening hours and display. Convert local schedule boundaries into instants for the requested date before comparing intervals.
Daylight-saving transitions create nonexistent and repeated local times. Decide whether to skip, shift, or disambiguate them; test both transition directions. A customer's device zone should not silently redefine the business's schedule.
Treat a displayed slot as a short-lived proposal
The availability response can be stale by the time someone clicks. On confirmation, re-check the current rules and reserve the required resource in one conflict-safe database operation. Depending on the model, this may use an exclusion constraint, transaction with locking, serializable operation, or short-lived hold. A read-then-write in separate unprotected requests can double-book under concurrency.
Use idempotency for retrying confirmations and define how expired holds release capacity. Tell the customer when a slot was taken and offer refreshed alternatives rather than returning a generic server error.
A compact interval check
Generic TypeScript sketch; it illustrates interval arithmetic only and is not production booking code:
- type Interval = { start: number; end: number };
- const overlaps = (a: Interval, b: Interval) => a.start < b.end && a.end > b.start;
- // Convert local schedule boundaries to instants before calling overlaps.
Test the edges, not only the happy path
Test adjacent appointments, buffers, variable durations, split shifts, time off, a multi-resource booking, cancellations, reschedules, DST gaps/repeats, concurrent confirmation, duplicate retries, and an admin edit racing a customer booking. Log decisions with stable identifiers, not customer details.