github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/common/clock.go (about) 1 package common 2 3 import ( 4 "github.com/name5566/leaf/log" 5 "time" 6 ) 7 8 func HourClock(d time.Duration, fs ...func()) { 9 if d < 1*time.Hour { 10 log.Error("定时器传入时间不足一小时") 11 return 12 } 13 go func() { 14 for { 15 for i := 0; i < len(fs); i++ { 16 fs[i]() 17 } 18 now := time.Now() 19 next := now.Add(d) 20 //下一整点小时 21 next = time.Date(next.Year(), next.Month(), next.Day(), next.Hour(), 0, 0, 0, next.Location()) 22 t := time.NewTicker(next.Sub(now)) 23 <-t.C 24 } 25 }() 26 }