Find the 2nd and 4th saturday for a month to check for bank holiday in go

644 Views Asked by At

I want to find the 2nd and 4th saturday for a month to check bank holiday. I have added check for sundays for now. Currently this how my function looks with date as input in implemented in Go

func IsHoliday(date time.Time) bool {
    return date.Weekday() == time.Sunday
}

1

There are 1 best solutions below

0
vikas suryavanshi On

This is how implemented function find alternate saturdays. It finds the first saturday date, then compares the input date with 2nd and 4th saturdays.

func isAlternateSaturday(date time.Time) bool {
    firstDateOfMonth := time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, nil)
    firstSaturday := (6-int(firstDateOfMonth.Weekday())) + firstDateOfMonth.Day()
    return (date.Day() == firstSaturday + 7) || (date.Day() == firstSaturday + 21)
}

Then integrated it with main IsHoliday function:

func IsHoliday(date time.Time) bool {
    return date.Weekday() == time.Sunday || isAlternateSaturday(date)
}