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 關注
- 124 瀏覽
添加回答
舉報