Skip to content

Commit

Permalink
Merge pull request #1 from ninja-software/darwin-holiday
Browse files Browse the repository at this point in the history
add alternative holidays
  • Loading branch information
sanamlimbu authored Nov 10, 2022
2 parents 67267fd + 0fc23ef commit e5dd527
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
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

0 comments on commit e5dd527

Please sign in to comment.