检查时间是否在周末

rvl007:

我有一个自定义的周末时间,从Friday 10pm UTCSunday 10:05pm UTC我有UTC的当前时间戳,只是想查看时间是否在周末时间。任何技巧将不胜感激。我尝试使用,weekdays() and time但无法达到预期的效果。

peterSO:

检查时间是否在UTC星期五晚上10点到UTC星期日10:05的周末时间。


使用Go time包。

例如,

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章