fix: weekDayKeys formula breaks when Jan 4 falls on Sunday

The naive formula used (jan4.Weekday() - time.Monday) produces -1
when Jan 4 is a Sunday (Weekday()==0), shifting the computed Monday
one week forward. 2026 is affected: Jan 4 is a Sunday, so every
week key in 2026 was mapped to the wrong 7-day range, causing
CloseWeek to look for closed_days on the wrong dates and finding
nothing — resulting in worked_ms=0 and a full -Nh delta.

Fix: use (weekday+6)%7 to get days-since-Monday (Mon=0…Sun=6),
which is always non-negative.

Adds table-driven TestWeekDayKeys covering 2024 (Thu), 2026 (Sun),
and 2023 (Wed) to prevent regression.
This commit is contained in:
2026-04-30 18:20:37 +02:00
parent 185bb0c6a9
commit e37458f513
2 changed files with 27 additions and 12 deletions

View File

@@ -65,8 +65,11 @@ func weekDayKeys(weekKey string, tz *time.Location) ([]string, error) {
// Find the Monday of that ISO week.
// Jan 4 is always in week 1 of its year.
jan4 := time.Date(year, time.January, 4, 0, 0, 0, 0, tz)
// (weekday+6)%7 gives days-since-Monday (Mon=0 … Sun=6), avoiding the
// sign issue when Weekday()==0 (Sunday) with the naive subtraction.
daysSinceMonday := int(jan4.Weekday()+6) % 7
_, jan4Week := jan4.ISOWeek()
monday := jan4.AddDate(0, 0, -int(jan4.Weekday()-time.Monday)+(week-jan4Week)*7)
monday := jan4.AddDate(0, 0, -daysSinceMonday+(week-jan4Week)*7)
keys := make([]string, 7)
for i := 0; i < 7; i++ {