Documentation
¶
Overview ¶
Package scheduling provides time and scheduling primitives for booking systems. It includes types for representing time-of-day, weekdays, opening hours, time slots, and orphan gap detection.
Index ¶
- Variables
- type DayHours
- type DaySlots
- type OpeningHours
- func (a *OpeningHours) GetDayHours(weekday Weekday) *DayHours
- func (a *OpeningHours) GetDayHoursForDate(date time.Time) *DayHours
- func (a *OpeningHours) IsOpenOn(weekday Weekday) bool
- func (a *OpeningHours) SetClosed(weekday Weekday)
- func (a *OpeningHours) SetDayHours(hours *DayHours) error
- func (a *OpeningHours) SetOpen(weekday Weekday, openTime, closeTime TimeOfDay) error
- func (a *OpeningHours) Validate() error
- type OrphanGap
- type OrphanGapDetector
- type OrphanGapReason
- type Slot
- type SlotState
- type TimeOfDay
- func (a TimeOfDay) Add(minutes int) TimeOfDay
- func (a TimeOfDay) After(other TimeOfDay) bool
- func (a TimeOfDay) Before(other TimeOfDay) bool
- func (a TimeOfDay) Equal(other TimeOfDay) bool
- func (a TimeOfDay) Hours() int
- func (a TimeOfDay) Minutes() int
- func (a TimeOfDay) String() string
- func (a TimeOfDay) TotalMinutes() int
- type TimeRange
- func (a *TimeRange) Contains(t TimeOfDay) bool
- func (a *TimeRange) Date() time.Time
- func (a *TimeRange) Duration() int
- func (a *TimeRange) EndTime() TimeOfDay
- func (a *TimeRange) Overlaps(other *TimeRange) bool
- func (a *TimeRange) SlotCount(slotDurationMinutes int) int
- func (a *TimeRange) StartTime() TimeOfDay
- func (a *TimeRange) String() string
- type Weekday
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCloseBeforeOpen is returned when close time is not after open time. ErrCloseBeforeOpen = errors.New("close time must be after open time") )
Errors for opening hours validation.
var ErrEndBeforeStart = errors.New("end time must be after start time")
Functions ¶
This section is empty.
Types ¶
type DayHours ¶
type DayHours struct {
// CloseTime is when the facility closes.
CloseTime TimeOfDay `json:"close_time"`
// IsClosed indicates if the facility is closed this day.
IsClosed bool `json:"is_closed"`
// OpenTime is when the facility opens.
OpenTime TimeOfDay `json:"open_time"`
// Weekday is the day of the week.
Weekday Weekday `json:"weekday"`
}
DayHours represents the opening hours for a single day.
func NewClosedDay ¶
NewClosedDay creates a closed day for a specific weekday.
func NewDayHours ¶
NewDayHours creates opening hours for a specific day. Returns error if close time is not after open time.
type DaySlots ¶
DaySlots represents all slots for a single day.
func NewDaySlots ¶
NewDaySlots creates slots for a day based on opening hours and slot duration.
func (*DaySlots) AvailableCount ¶
AvailableCount returns the number of available slots.
func (*DaySlots) MarkBooked ¶
MarkBooked marks a slot as booked.
type OpeningHours ¶
type OpeningHours struct {
// Days maps weekday to opening hours.
Days map[Weekday]*DayHours `json:"days"`
}
OpeningHours represents the weekly opening hours for a facility.
func NewOpeningHours ¶
func NewOpeningHours() *OpeningHours
NewOpeningHours creates a new opening hours schedule. All days default to closed.
func (*OpeningHours) GetDayHours ¶
func (a *OpeningHours) GetDayHours(weekday Weekday) *DayHours
GetDayHours returns the opening hours for a specific weekday.
func (*OpeningHours) GetDayHoursForDate ¶
func (a *OpeningHours) GetDayHoursForDate(date time.Time) *DayHours
GetDayHoursForDate returns the opening hours for a specific date.
func (*OpeningHours) IsOpenOn ¶
func (a *OpeningHours) IsOpenOn(weekday Weekday) bool
IsOpenOn returns true if the facility is open on the given weekday.
func (*OpeningHours) SetClosed ¶
func (a *OpeningHours) SetClosed(weekday Weekday)
SetClosed sets the facility as closed for a specific day.
func (*OpeningHours) SetDayHours ¶
func (a *OpeningHours) SetDayHours(hours *DayHours) error
SetDayHours sets the opening hours for a specific day.
func (*OpeningHours) SetOpen ¶
func (a *OpeningHours) SetOpen(weekday Weekday, openTime, closeTime TimeOfDay) error
SetOpen sets the facility as open for a specific day.
func (*OpeningHours) Validate ¶
func (a *OpeningHours) Validate() error
Validate checks if all opening hours are valid.
type OrphanGap ¶
type OrphanGap struct {
// EndTime is the end time of the gap.
EndTime TimeOfDay `json:"end_time"`
// Reason is the i18n key explaining why this gap is an orphan.
Reason OrphanGapReason `json:"reason"`
// StartTime is the start time of the gap.
StartTime TimeOfDay `json:"start_time"`
}
OrphanGap represents an unusable 30-minute gap created by a selection.
type OrphanGapDetector ¶
type OrphanGapDetector struct {
// contains filtered or unexported fields
}
OrphanGapDetector detects orphan gaps created by a selection.
func NewOrphanGapDetector ¶
func NewOrphanGapDetector(dayHours *DayHours, existingBookings []*TimeRange, slotDuration int) *OrphanGapDetector
NewOrphanGapDetector creates a new orphan gap detector.
func (*OrphanGapDetector) Detect ¶
func (a *OrphanGapDetector) Detect(selection *TimeRange) []*OrphanGap
Detect finds all orphan gaps that would be created by the given selection. An orphan gap is a 30-minute slot that becomes unbookable because: 1. It's between the selection and the start of the booking window 2. It's between the selection and the end of the booking window 3. It's between the selection and an existing booking
Returns nil if no orphan gaps are detected.
func (*OrphanGapDetector) GetFirstOrphanGap ¶
func (a *OrphanGapDetector) GetFirstOrphanGap(selection *TimeRange) *OrphanGap
GetFirstOrphanGap returns the first detected orphan gap, or nil if none.
func (*OrphanGapDetector) HasOrphanGaps ¶
func (a *OrphanGapDetector) HasOrphanGaps(selection *TimeRange) bool
HasOrphanGaps returns true if the selection would create any orphan gaps.
type OrphanGapReason ¶
type OrphanGapReason string
OrphanGapReason represents the reason for an orphan gap.
const ( OrphanGapReasonBetweenBookings OrphanGapReason = "reason.orphan_gap.between_bookings" OrphanGapReasonEndOfWindow OrphanGapReason = "reason.orphan_gap.end_of_window" OrphanGapReasonStartOfWindow OrphanGapReason = "reason.orphan_gap.start_of_window" )
Orphan gap reason keys (i18n keys).
type Slot ¶
type Slot struct {
Date time.Time `json:"date"`
EndTime TimeOfDay `json:"end_time"`
ID string `json:"id"`
StartTime TimeOfDay `json:"start_time"`
State SlotState `json:"state"`
}
Slot represents a single bookable time slot.
func (*Slot) IsAvailable ¶
IsAvailable returns true if the slot can be booked.
type TimeOfDay ¶
type TimeOfDay int
TimeOfDay represents a time without date (hours and minutes). Minutes are stored as offset from midnight (0-1439).
func MustTimeOfDay ¶
MustTimeOfDay creates a TimeOfDay from hours and minutes, panics on error.
func NewTimeOfDay ¶
NewTimeOfDay creates a TimeOfDay from hours and minutes. Returns error if hours or minutes are out of range.
func ParseTimeOfDay ¶
ParseTimeOfDay parses a time-of-day string in "HH:MM" format.
func (TimeOfDay) Add ¶
Add adds minutes to the time and returns a new TimeOfDay. Does not wrap around midnight.
func (TimeOfDay) TotalMinutes ¶
TotalMinutes returns the total minutes from midnight.
type TimeRange ¶
type TimeRange struct {
// contains filtered or unexported fields
}
TimeRange represents a contiguous time range for a selection. This is a value object - immutable once created.
func NewTimeRange ¶
NewTimeRange creates a new time range. Returns an error if end time is not after start time.