From be6893e0232db78de32c630b4de78eeedd2b4ca4 Mon Sep 17 00:00:00 2001 From: MohammadHadi Attarieh Date: Wed, 28 Oct 2026 21:01:55 +0330 Subject: [PATCH] fix(slots): align slot starts to local wall clock, not the UTC epoch alignUp rounded starts to multiples of the interval counted from the Unix epoch, i.e. on the UTC clock. For a host whose UTC offset is not a whole number of hours (Tehran +03:30, India +05:30, Nepal +05:45, Adelaide, Newfoundland, ...) every hourly slot started at :30/: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 min) also drifted from one day to the next, even in UTC. Align to local midnight on one shared wall clock - the first pool host's timezone - so collective and round-robin hosts still meet on identical start instants. UTC hosts keep exactly the old grid for intervals dividing a day. Wall-clock arithmetic keeps the grid stable across DST change days. Co-Authored-By: Claude Opus 5.5 --- internal/slots/availability_test.go | 48 ++++++++++++++++++++++++++--- internal/slots/generate.go | 47 ++++++++++++++++++++-------- internal/slots/generate_test.go | 28 +++++++++++++++++ 3 files changed, 107 insertions(+), 16 deletions(-) diff --git a/internal/slots/availability_test.go b/internal/slots/availability_test.go index f75a48ac..9b2fdb3f 100644 --- a/internal/slots/availability_test.go +++ b/internal/slots/availability_test.go @@ -286,7 +286,7 @@ 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) } @@ -294,7 +294,7 @@ func TestAlignUp_alreadyAligned(t *testing.T) { 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) @@ -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) @@ -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. + 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)) + } +} diff --git a/internal/slots/generate.go b/internal/slots/generate.go index 7f6fb097..b3b6a87b 100644 --- a/internal/slots/generate.go +++ b/internal/slots/generate.go @@ -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 { @@ -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) @@ -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 @@ -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. +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) + for out.Before(t) { // a DST gap/overlap can land the wall-clock instant behind t + out = out.Add(interval) + } + return out.In(t.Location()) } diff --git a/internal/slots/generate_test.go b/internal/slots/generate_test.go index bd709bc8..4f171955 100644 --- a/internal/slots/generate_test.go +++ b/internal/slots/generate_test.go @@ -1,6 +1,7 @@ package slots_test import ( + "strings" "testing" "time" @@ -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) + } +}