go - 检查时间是否在周末
问题描述
我有一个自定义的周末时间,从Friday 10pm UTC
到Sunday 10:05pm UTC
。我有一个 UTC 的当前时间戳,只是想检查时间是否属于周末时间。任何技巧将不胜感激。我尝试使用weekdays() and time
但无法达到预期的效果。
解决方案
检查时间是否属于周末时间,即从周五晚上 10 点 UTC 到周日晚上 10:05 UTC。
使用 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
推荐阅读
- google-cloud-functions - 保存本地数据履行对话流nodejs
- python - 使用 Python 3 读取 XLSM 文件
- tensorflow - Tensorflow 的 K-Means - 图形断开错误
- python - Python 对象不是 JSON 可序列化的
- spring-boot - Jms:具有多个消费者的 Pub/Sub
- reactjs - React中的MongoDB嵌入图表,过滤ObjectId不起作用
- types - 如何使用变体选项在脚本中键入函数参数?
- python - 熊猫插值 - “时间”
- php - 从现有数据库创建实体,但缺少表
- pandas - 使用 groupby 计算比率