第一件事首先,比较时间值,使用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()) // falsefmt.Println(t2.Equal(t1)) // true
https://play.golang.org/p/06RcvuI_1Ha
对于计划问题,我将使用time.Timer。
- 找出接下来要发出的通知
- 相应地设置或重置计时器
- 计时器触发后,转到1
- 如果添加了通知,请转到1
- 如果删除通知,则转到1
这是一个草图:
package mainimport "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))}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)