1 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個贊
檢查時間是否屬于周末時間,即從UTC 時間周五晚上 10 點(diǎn)到周日晚上 10:05。
使用 Gotime
?包。
例如,
package main
import (
? ? "fmt"
? ? "time"
)
// A weekend is Friday 10pm UTC to Sunday 10:05pm UTC
func isWeekend(t time.Time) bool {
? ? t = t.UTC()
? ? switch t.Weekday() {
? ? case time.Friday:
? ? ? ? h, _, _ := t.Clock()
? ? ? ? if h >= 12+10 {
? ? ? ? ? ? return true
? ? ? ? }
? ? case time.Saturday:
? ? ? ? return true
? ? case time.Sunday:
? ? ? ? h, m, _ := t.Clock()
? ? ? ? if h < 12+10 {
? ? ? ? ? ? return true
? ? ? ? }
? ? ? ? if h == 12+10 && m <= 5 {
? ? ? ? ? ? return true
? ? ? ? }
? ? }
? ? return false
}
func main() {
? ? t := time.Date(2019, 11, 22, 12+10, 5, 0, 0, time.UTC)
? ? fmt.Println(t)
? ? w := isWeekend(t)
? ? fmt.Println(w)
}
游樂場:https://play.golang.org/p/TZBoNcwH-qU
輸出:
2019-11-22 22:05:00 +0000 UTC
true
- 1 回答
- 0 關(guān)注
- 151 瀏覽
添加回答
舉報