Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add alternative holidays #1

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions ausholiday.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,81 @@ func init() {
xholidays = days.Holidays
})
}

// Holiday alternative
type HolidayAlt struct {
HolidayName string
Information string
MoreInformation string
Jurisdiction State
Timezone string

Month time.Month
Week int
Weekday time.Weekday
}

var holidayAlts = []*HolidayAlt{
{
HolidayName: "Dawin Show Day",
Information: "",
MoreInformation: "",
Jurisdiction: NT,
Timezone: "Australia/Darwin",

Month: time.July,
Week: 4,
Weekday: time.Friday,
},
}

func IsHolidayAlt(state State, date time.Time) bool {

for _, holidayAlt := range holidayAlts {
if holidayAlt.Jurisdiction != state {
continue
}

location, err := time.LoadLocation(holidayAlt.Timezone)
if err != nil {
continue
}

// convert time in the timezone
timeWithTimezone := date.In(location)

y := timeWithTimezone.Year()
m := timeWithTimezone.Month()
d := timeWithTimezone.Day()
wd := timeWithTimezone.Weekday()

// check month
if holidayAlt.Month != m {
continue
}

// check weekday
if holidayAlt.Weekday != wd {
continue
}

// check date
firstDayOfTheMonth := time.Date(y, m, 1, 0, 0, 0, 0, time.UTC)

firstWeekdayOfTheMonth := int(firstDayOfTheMonth.Weekday())

diffOfTheDays := int(wd) - firstWeekdayOfTheMonth
if diffOfTheDays < 0 {
diffOfTheDays += 7
}

if d != firstDayOfTheMonth.Add(time.Duration(diffOfTheDays*24)*time.Hour).Add(time.Duration((holidayAlt.Week-1)*7*24)*time.Hour).Day() {
continue
}

return true

}

return false
}
32 changes: 32 additions & 0 deletions ausholiday_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,38 @@ func testIsHolidayFunc(hday *AusHoliday, t1 time.Time, expect bool) func(t *test
}
}

func TestIsHolidayAlt(t *testing.T) {
var t1 time.Time

t1 = ezTime(2022, 7, 22)
t.Run("Darwin Show Day Check 2022", testIsHolidayAlt(NT, t1, true))

t1 = ezTime(2023, 7, 28)
t.Run("Darwin Show Day Check 2023", testIsHolidayAlt(NT, t1, true))

t1 = ezTime(2024, 7, 26)
t.Run("Darwin Show Day Check 2024", testIsHolidayAlt(NT, t1, true))

t1 = ezTime(2025, 7, 25)
t.Run("Darwin Show Day Check 2025", testIsHolidayAlt(NT, t1, true))

t1 = ezTime(2026, 7, 24)
t.Run("Darwin Show Day Check 2026", testIsHolidayAlt(NT, t1, true))

t1 = ezTime(2026, 7, 25)
t.Run("Darwin Show Day Check False", testIsHolidayAlt(NT, t1, false))
}

func testIsHolidayAlt(state State, date time.Time, expect bool) func(t *testing.T) {
return func(t *testing.T) {
b := IsHolidayAlt(state, date)
if b != expect {
t.Errorf("IsHoliday was incorrect, got: %t, want: %t", b, expect)
}
}

}

func TestBusinessDays(t *testing.T) {
var t1, t2 time.Time

Expand Down
3 changes: 2 additions & 1 deletion builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ func IsHoliday(state State, t time.Time, holidays ...*Holiday) bool {
}
}

return false
// final check if it is a alt holiday
return IsHolidayAlt(state, t)
}

// IsHolidayAny is it a holiday in any state
Expand Down