Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions internal/slots/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ func TestResolveDay_overrideDoesNotMatchOtherDate(t *testing.T) {
func TestAlignUp_alreadyAligned(t *testing.T) {
// 09:00:00 UTC is exactly on a 30-min boundary (since epoch).
base := time.Date(2026, 1, 1, 9, 0, 0, 0, time.UTC)
got := alignUp(base, 30*time.Minute)
got := alignUp(base, 30*time.Minute, time.UTC)
if !got.Equal(base) {
t.Errorf("got %v; want %v", got, base)
}
}

func TestAlignUp_roundsUp(t *testing.T) {
base := time.Date(2026, 1, 1, 9, 5, 0, 0, time.UTC) // 09:05
got := alignUp(base, 30*time.Minute)
got := alignUp(base, 30*time.Minute, time.UTC)
want := time.Date(2026, 1, 1, 9, 30, 0, 0, time.UTC)
if !got.Equal(want) {
t.Errorf("got %v; want %v", got, want)
Expand All @@ -303,7 +303,7 @@ func TestAlignUp_roundsUp(t *testing.T) {

func TestAlignUp_15minInterval(t *testing.T) {
base := time.Date(2026, 1, 1, 9, 7, 0, 0, time.UTC)
got := alignUp(base, 15*time.Minute)
got := alignUp(base, 15*time.Minute, time.UTC)
want := time.Date(2026, 1, 1, 9, 15, 0, 0, time.UTC)
if !got.Equal(want) {
t.Errorf("got %v; want %v", got, want)
Expand All @@ -312,9 +312,49 @@ func TestAlignUp_15minInterval(t *testing.T) {

func TestAlignUp_hourInterval(t *testing.T) {
base := time.Date(2026, 1, 1, 9, 1, 0, 0, time.UTC)
got := alignUp(base, 60*time.Minute)
got := alignUp(base, 60*time.Minute, time.UTC)
want := time.Date(2026, 1, 1, 10, 0, 0, 0, time.UTC)
if !got.Equal(want) {
t.Errorf("got %v; want %v", got, want)
}
}

func TestAlignUp_halfHourOffsetZone(t *testing.T) {
tehran, err := time.LoadLocation("Asia/Tehran") // +03:30, no DST since 2022
if err != nil {
t.Skip(err)
}
// 09:00 local is 05:30 UTC: on the old epoch grid an hourly interval moved it to 10:00.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old epoch grid rounded 05:30 UTC up to 06:00 UTC, which is 09:30 Tehran, not 10:00. The assertion on the next line is right; only this lead-in comment is off by one grid step.

Suggested change
// 09:00 local is 05:30 UTC: on the old epoch grid an hourly interval moved it to 10:00.
// 09:00 local is 05:30 UTC: on the old epoch grid an hourly interval moved it to 09:30.

base := time.Date(2026, 6, 15, 9, 0, 0, 0, tehran)
if got := alignUp(base, time.Hour, tehran); !got.Equal(base) {
t.Errorf("09:00 Tehran: got %v; want unchanged", got.In(tehran))
}
if got, want := alignUp(base.Add(time.Minute), time.Hour, tehran), time.Date(2026, 6, 15, 10, 0, 0, 0, tehran); !got.Equal(want) {
t.Errorf("09:01 Tehran: got %v; want %v", got.In(tehran), want)
}
}

func TestAlignUp_nonDividingIntervalRestartsDaily(t *testing.T) {
// 50 minutes does not divide a day; the epoch grid drifted day to day.
for day := 1; day <= 3; day++ {
base := time.Date(2026, 6, day, 9, 0, 0, 0, time.UTC)
got := alignUp(base, 50*time.Minute, time.UTC)
want := time.Date(2026, 6, day, 9, 10, 0, 0, time.UTC) // 00:00 + 11*50m
if !got.Equal(want) {
t.Errorf("day %d: got %v; want %v", day, got, want)
}
}
}

func TestAlignUp_dstDayKeepsWallClockGrid(t *testing.T) {
ny, err := time.LoadLocation("America/New_York")
if err != nil {
t.Skip(err)
}
// 2026-03-08 springs forward at 02:00. 45 minutes does not divide the lost hour, so an
// elapsed-time grid would put 09:00 off-grid; the wall clock keeps it on.
base := time.Date(2026, 3, 8, 9, 0, 0, 0, ny)
if got := alignUp(base, 45*time.Minute, ny); !got.Equal(base) {
t.Errorf("got %v; want 09:00 unchanged", got.In(ny))
}
}
47 changes: 35 additions & 12 deletions internal/slots/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ type params struct {
minNotice time.Time
maxFuture time.Time
dateFrom, dateTo time.Time
// alignLoc is the clock slot starts are aligned to. One shared clock for every host,
// because collective/round-robin routing joins hosts on identical start instants.
alignLoc *time.Location
}

func newParams(req Request) params {
Expand All @@ -155,6 +158,12 @@ func newParams(req Request) params {
// consistent regardless of what time-of-day the caller passes.
dateFrom: req.DateFrom.UTC().Truncate(24 * time.Hour),
dateTo: req.DateTo.UTC().Truncate(24 * time.Hour),
alignLoc: time.UTC,
}
if len(req.Hosts) > 0 && req.Hosts[0].Location != nil {
// The first pool host is the primary (priority order), so the grid follows the
// team's lead host. A UTC host keeps the old epoch grid exactly.
p.alignLoc = req.Hosts[0].Location
}
if req.Event.MaxFutureDays > 0 {
p.maxFuture = req.Now.Add(time.Duration(req.Event.MaxFutureDays) * 24 * time.Hour)
Expand Down Expand Up @@ -249,8 +258,8 @@ func hostsByStart(req Request, p params, applyBusy bool, belowNotice map[time.Ti

for _, f := range avail {
// Align the first slot start up to the nearest interval boundary
// (epoch-aligned so slots land on :00/:15/:30/:45 etc.).
t := alignUp(f.Start, p.interval)
// on the shared wall clock, so slots land on :00/:15/:30/:45 etc.
t := alignUp(f.Start, p.interval, p.alignLoc)
for ; !t.Add(p.dur).After(f.End); t = t.Add(p.interval) {
if t.Before(p.minNotice) {
// !t.Before(req.Now) is what keeps the attribution honest: a start
Expand Down Expand Up @@ -381,21 +390,35 @@ func pickHosts(hosts []HostAvailability, available map[string]bool, mode string)
}
}

// alignUp rounds t up to the next epoch-aligned multiple of interval.
// Epoch alignment means slots land on :00/:15/:30/:45 for minute-granularity
// intervals, regardless of when the free window starts.
func alignUp(t time.Time, interval time.Duration) time.Time {
// alignUp rounds t up to the next multiple of interval counted from local midnight on
// loc's wall clock, so slots land on :00/:15/:30/:45 for minute-granularity intervals,
// regardless of when the free window starts.
//
// This used to count from the Unix epoch, i.e. on the UTC clock. For a host whose offset
// is not a whole number of hours (Tehran +03:30, India +05:30, Nepal +05:45, Adelaide,
// Newfoundland, ...) every hourly slot then started at :30 or :45 local - a 09:00-17:00
// day offered 09:30, 10:30, ... and never 09:00. Intervals that do not divide a day (50,
// 35 minutes) also drifted from day to day; counting from local midnight restarts the
// grid each day. Wall-clock (not elapsed) arithmetic keeps the grid on DST change days.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This claims more than the change delivers: the DST fallback below (out.Add(interval)) and the per-slot advance in hostsByStart both use elapsed arithmetic, so a generation pass still drifts off the wall grid after a transition. A 45-minute interval in New York on 2026-03-08 yields 00:45, 01:30, 03:15, 04:00 — 03:15 is off-grid. Either make the stepping wall-clock-based or soften the claim and add a generation-level DST test.

Suggested change
// grid each day. Wall-clock (not elapsed) arithmetic keeps the grid on DST change days.
// grid each day. Note: only the first start is re-aligned here; the per-slot advance in
// hostsByStart still uses elapsed time, so starts can drift off the wall grid after a DST
// transition (e.g. a 45-minute interval on a spring-forward day).

func alignUp(t time.Time, interval time.Duration, loc *time.Location) time.Time {
secs := int64(interval.Seconds())
if secs <= 0 {
return t
}
unix := t.Unix()
rem := unix % secs
if rem < 0 {
rem += secs // Go's % returns negative remainder for negative dividend
if loc == nil {
loc = time.UTC
}
if rem == 0 {
lt := t.In(loc)
wall := int64(lt.Hour()*3600 + lt.Minute()*60 + lt.Second()) // seconds past local midnight
frac := lt.Nanosecond() != 0
if wall%secs == 0 && !frac {
return t
}
return t.Add(time.Duration(secs-rem) * time.Second)
steps := wall/secs + 1
y, m, d := lt.Date()
out := time.Date(y, m, d, 0, 0, int(steps*secs), 0, loc)
Comment on lines +417 to +419
for out.Before(t) { // a DST gap/overlap can land the wall-clock instant behind t
out = out.Add(interval)
Comment on lines +419 to +421
}
return out.In(t.Location())
}
28 changes: 28 additions & 0 deletions internal/slots/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slots_test

import (
"strings"
"testing"
"time"

Expand Down Expand Up @@ -899,3 +900,30 @@ func assertSlotStarts(t *testing.T, got []slots.Slot, wantUTC []time.Time) {
}
}
}

// A Tehran (+03:30) host working 09:00-12:00 with hourly slots must be offered 09:00,
// 10:00 and 11:00 local. On the old UTC/epoch grid the same day offered only 09:30 and
// 10:30, because every UTC hour boundary falls on :30 in Tehran.
func TestGenerate_halfHourOffsetHostGetsWholeHourSlots(t *testing.T) {
tehran, err := time.LoadLocation("Asia/Tehran")
if err != nil {
t.Skip(err)
}
day := time.Date(2026, 6, 15, 0, 0, 0, 0, time.UTC) // a Monday
got, err := slots.Generate(slots.Request{
Event: slots.EventConfig{DurationMinutes: 60, SlotIntervalMinutes: 60, RoutingMode: "fixed"},
Hosts: []slots.HostAvailability{{HostID: "h", Location: tehran,
Rules: []slots.AvailabilityRule{{DayOfWeek: time.Monday, StartTime: "09:00", EndTime: "12:00"}}}},
DateFrom: day, DateTo: day, BookerTZ: tehran, Now: day.Add(-48 * time.Hour),
})
if err != nil {
t.Fatal(err)
}
var starts []string
for _, s := range got {
starts = append(starts, s.Start.In(tehran).Format("15:04"))
}
if want := []string{"09:00", "10:00", "11:00"}; strings.Join(starts, ",") != strings.Join(want, ",") {
t.Fatalf("starts = %v, want %v", starts, want)
}
}
Loading