From d7e00c69f59c9e2b3ceb2f222798cac266e5150c Mon Sep 17 00:00:00 2001 From: Rick Arnold Date: Fri, 11 Nov 2022 10:59:55 -0500 Subject: [PATCH] fix New Year calculation when observed crosses year Fixes #98 --- v2/cal.go | 4 ++-- v2/cal_business.go | 7 ++++--- v2/cal_funcs.go | 11 ++++++----- v2/tests/issue_98_test.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 v2/tests/issue_98_test.go diff --git a/v2/cal.go b/v2/cal.go index 6af0d28..dd85c4d 100644 --- a/v2/cal.go +++ b/v2/cal.go @@ -94,8 +94,8 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday) } obsMatch := !obs.IsZero() if obsMatch { - _, obsMonth, obsDay := obs.Date() - obsMatch = obsMonth == month && obsDay == day + obsYear, obsMonth, obsDay := obs.Date() + obsMatch = obsYear == year && obsMonth == month && obsDay == day } if actMatch || obsMatch { if c.Cacheable { diff --git a/v2/cal_business.go b/v2/cal_business.go index 4dec967..a423a95 100644 --- a/v2/cal_business.go +++ b/v2/cal_business.go @@ -186,9 +186,10 @@ func (c *BusinessCalendar) WorkdaysInRange(start, end time.Time) int { // for the given year and month. // // The value of n affects the direction of counting: -// n > 0: counting begins at the first day of the month. -// n == 0: the result is always 0. -// n < 0: counting begins at the end of the month. +// +// n > 0: counting begins at the first day of the month. +// n == 0: the result is always 0. +// n < 0: counting begins at the end of the month. func (c *BusinessCalendar) WorkdayN(year int, month time.Month, n int) int { var date time.Time var add int diff --git a/v2/cal_funcs.go b/v2/cal_funcs.go index d137118..b9da403 100644 --- a/v2/cal_funcs.go +++ b/v2/cal_funcs.go @@ -16,11 +16,12 @@ func IsWeekend(t time.Time) bool { // given time. // // The value of n affects the direction of counting: -// n > 0: the result is the nth occurrence counting forwards from the -// given time. -// n == 0: the result is always the zero time. -// n < 0: the result is the nth occurrence counting backwards from the -// given time. +// +// n > 0: the result is the nth occurrence counting forwards from the +// given time. +// n == 0: the result is always the zero time. +// n < 0: the result is the nth occurrence counting backwards from the +// given time. // // The given time is considered an occurrence of the weekday; if n == 1 or -1 // and the given time matches the desired weekday, it will be returned diff --git a/v2/tests/issue_98_test.go b/v2/tests/issue_98_test.go new file mode 100644 index 0000000..1d21dcb --- /dev/null +++ b/v2/tests/issue_98_test.go @@ -0,0 +1,34 @@ +package tests + +import ( + "testing" + "time" + + "github.com/rickar/cal/v2" + "github.com/rickar/cal/v2/us" +) + +// New Year calculation was not checking that the observed year was correct so +// 12/31/2022 was treated as a holiday because 12/31/2021 was. +func TestNewYear2022(t *testing.T) { + c := cal.NewBusinessCalendar() + c.AddHoliday(us.NewYear) + + tests := []struct { + date time.Time + wantAct bool + wantObs bool + }{ + {time.Date(2022, 12, 31, 12, 30, 0, 0, time.UTC), false, false}, + {time.Date(2023, 1, 1, 12, 30, 0, 0, time.UTC), true, false}, + {time.Date(2023, 1, 2, 12, 30, 0, 0, time.UTC), false, true}, + } + + for i, test := range tests { + gotAct, gotObs, _ := c.IsHoliday(test.date) + if gotAct != test.wantAct || gotObs != test.wantObs { + t.Errorf("[%d] gotAct: %t, wantAct: %t; gotObs: %t, wantObs: %t", i, gotAct, test.wantAct, gotObs, + test.wantObs) + } + } +}