簡體   English   中英

在 go 中查找一個月的第 2 個和第 4 個星期六以檢查銀行假期

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

我想找一個月的第二個和第四個星期六來檢查銀行假期。 我現在已經添加了周日的支票。 目前這是我的 function 在 Go 中以日期作為輸入的外觀

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

這就是實現 function 查找備用星期六的方式。 它找到第一個星期六日期,然后將輸入日期與第二個和第四個星期六進行比較。

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

然后將其與主要的 IsHoliday function 集成:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM