简体   繁体   中英

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

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
}

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM