Skip to content

Commit

Permalink
fix New Year calculation when observed crosses year
Browse files Browse the repository at this point in the history
Fixes #98
  • Loading branch information
rickar committed Nov 11, 2022
1 parent 75ff87b commit d7e00c6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
4 changes: 2 additions & 2 deletions v2/cal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions v2/cal_business.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions v2/cal_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions v2/tests/issue_98_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit d7e00c6

Please sign in to comment.