检查时间是否属于周末时间,即从UTC 时间周五晚上 10 点到周日晚上 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)
}
Run Code Online (Sandbox Code Playgroud)
游乐场:https://play.golang.org/p/TZBoNcwH-qU
输出:
2019-11-22 22:05:00 +0000 UTC
true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3108 次 |
| 最近记录: |