diff --git a/ausholiday.go b/ausholiday.go index 59c1b95..4c3fab4 100644 --- a/ausholiday.go +++ b/ausholiday.go @@ -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 +} diff --git a/ausholiday_test.go b/ausholiday_test.go index f36608c..81db01e 100644 --- a/ausholiday_test.go +++ b/ausholiday_test.go @@ -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 diff --git a/builtin.go b/builtin.go index a206518..d94844b 100644 --- a/builtin.go +++ b/builtin.go @@ -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