Skip to content

Commit d7e00c6

Browse files
committed
fix New Year calculation when observed crosses year
Fixes #98
1 parent 75ff87b commit d7e00c6

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

v2/cal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func (c *Calendar) IsHoliday(date time.Time) (actual, observed bool, h *Holiday)
9494
}
9595
obsMatch := !obs.IsZero()
9696
if obsMatch {
97-
_, obsMonth, obsDay := obs.Date()
98-
obsMatch = obsMonth == month && obsDay == day
97+
obsYear, obsMonth, obsDay := obs.Date()
98+
obsMatch = obsYear == year && obsMonth == month && obsDay == day
9999
}
100100
if actMatch || obsMatch {
101101
if c.Cacheable {

v2/cal_business.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ func (c *BusinessCalendar) WorkdaysInRange(start, end time.Time) int {
186186
// for the given year and month.
187187
//
188188
// The value of n affects the direction of counting:
189-
// n > 0: counting begins at the first day of the month.
190-
// n == 0: the result is always 0.
191-
// n < 0: counting begins at the end of the month.
189+
//
190+
// n > 0: counting begins at the first day of the month.
191+
// n == 0: the result is always 0.
192+
// n < 0: counting begins at the end of the month.
192193
func (c *BusinessCalendar) WorkdayN(year int, month time.Month, n int) int {
193194
var date time.Time
194195
var add int

v2/cal_funcs.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ func IsWeekend(t time.Time) bool {
1616
// given time.
1717
//
1818
// The value of n affects the direction of counting:
19-
// n > 0: the result is the nth occurrence counting forwards from the
20-
// given time.
21-
// n == 0: the result is always the zero time.
22-
// n < 0: the result is the nth occurrence counting backwards from the
23-
// given time.
19+
//
20+
// n > 0: the result is the nth occurrence counting forwards from the
21+
// given time.
22+
// n == 0: the result is always the zero time.
23+
// n < 0: the result is the nth occurrence counting backwards from the
24+
// given time.
2425
//
2526
// The given time is considered an occurrence of the weekday; if n == 1 or -1
2627
// and the given time matches the desired weekday, it will be returned

v2/tests/issue_98_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/rickar/cal/v2"
8+
"github.com/rickar/cal/v2/us"
9+
)
10+
11+
// New Year calculation was not checking that the observed year was correct so
12+
// 12/31/2022 was treated as a holiday because 12/31/2021 was.
13+
func TestNewYear2022(t *testing.T) {
14+
c := cal.NewBusinessCalendar()
15+
c.AddHoliday(us.NewYear)
16+
17+
tests := []struct {
18+
date time.Time
19+
wantAct bool
20+
wantObs bool
21+
}{
22+
{time.Date(2022, 12, 31, 12, 30, 0, 0, time.UTC), false, false},
23+
{time.Date(2023, 1, 1, 12, 30, 0, 0, time.UTC), true, false},
24+
{time.Date(2023, 1, 2, 12, 30, 0, 0, time.UTC), false, true},
25+
}
26+
27+
for i, test := range tests {
28+
gotAct, gotObs, _ := c.IsHoliday(test.date)
29+
if gotAct != test.wantAct || gotObs != test.wantObs {
30+
t.Errorf("[%d] gotAct: %t, wantAct: %t; gotObs: %t, wantObs: %t", i, gotAct, test.wantAct, gotObs,
31+
test.wantObs)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)