1 回答
TA贡献1850条经验 获得超11个赞
首先,要比较时间值,请使用Time.Equal、Time.Before和time.After方法。比较各个组件根本不可靠:
newYork, _ := time.LoadLocation("America/New_York")
t1 := time.Date(2018, 11, 8, 4, 0, 0, 0, time.UTC)
t2 := t1.In(newYork)
fmt.Printf("%v == %v?\n", t1, t2) // 2018-11-08 04:00:00 +0000 UTC == 2018-11-07 23:00:00 -0500 EST?
fmt.Println(t1.Day() == t2.Day()) // false
fmt.Println(t2.Equal(t1)) // true
https://play.golang.org/p/06RcvuI_1Ha
对于调度问题,我会使用time.Timer。
找出下一个通知
相应地设置或重置定时器
计时器触发后,转到 1
如果添加通知,转到1
如果通知被删除,转到1
这是一个草图:
package main
import "time"
func main() {
t := time.NewTimer(0)
go func() {
for range t.C {
nextTwo := db.GetNextNotifications(2)
// Sanity check
if time.Until(nextTwo[0].Start) > 1*time.Second {
// The timer went off early. Perhaps the notification has been
// deleted?
t.Reset(time.Until(nextTwo[0].Start))
continue
}
go send(nextTwo[0])
t.Reset(time.Until(nextTwo[1].Start))
}
}()
resetTimer(t) // call as required whenever a notification is added or removed
}
func resetTimer(t *time.Timer) {
next := db.GetNextNotification()
t.Reset(time.Until(next.Start))
}
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报