🐛 Reject week keys that are not real ISO weeks
WeekDayKeys accepted W01 to W53 without checking that the year has that week. Closing 2025-W53, a year with 52 weeks, closed the days of 2026-W01 under a second key and the balance counted them twice. The computed Monday must now map back to the requested week key.
This commit is contained in:
@@ -26,7 +26,8 @@ func WeekKeyForDayKey(dayKey string) string {
|
||||
}
|
||||
|
||||
// WeekDayKeys returns the day keys (YYYY-MM-DD) of Monday to Sunday of the ISO week
|
||||
// weekKey (YYYY-Www). It returns ErrInvalidWeekKey for a malformed week key.
|
||||
// weekKey (YYYY-Www). It returns ErrInvalidWeekKey for a malformed week key and
|
||||
// for a week the year does not have, for example 2025-W53.
|
||||
func WeekDayKeys(weekKey string) ([]string, error) {
|
||||
m := weekKeyPattern.FindStringSubmatch(weekKey)
|
||||
if m == nil {
|
||||
@@ -49,6 +50,11 @@ func WeekDayKeys(weekKey string) ([]string, error) {
|
||||
for i := range keys {
|
||||
keys[i] = monday.AddDate(0, 0, i).Format(dayKeyLayout)
|
||||
}
|
||||
// Every year has 52 ISO weeks, but only some have a 53rd one. Without this
|
||||
// check the days of the following W01 would be closed under a second key.
|
||||
if WeekKeyForDayKey(keys[0]) != weekKey {
|
||||
return nil, ErrInvalidWeekKey
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user