mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
简化timer定时器
This commit is contained in:
@@ -5,49 +5,41 @@ import "time"
|
||||
type Timer struct {
|
||||
lasttime int64
|
||||
timeinterval int64
|
||||
|
||||
setupZeroDBase time.Duration //0表示普通模式 1表示切换分钟模式
|
||||
}
|
||||
|
||||
func (slf *Timer) SetupTimer(ms int32) {
|
||||
|
||||
slf.lasttime = time.Now().UnixNano()
|
||||
slf.timeinterval = int64(ms) * 1e6
|
||||
}
|
||||
|
||||
func (slf *Timer) SetupTimerOnlyInterval(ms int32) {
|
||||
slf.timeinterval = int64(ms) * 1e6
|
||||
//检查整点分钟数触发
|
||||
func (slf *Timer) SetupZeroTimer(baseD time.Duration, interval int64) {
|
||||
timeNow := time.Now()
|
||||
nt := timeNow.Truncate(baseD)
|
||||
slf.lasttime = nt.UnixNano()
|
||||
slf.timeinterval = baseD.Nanoseconds() * interval
|
||||
slf.setupZeroDBase = baseD
|
||||
}
|
||||
|
||||
func (slf *Timer) SetupTimerTheHour(hour int32) {
|
||||
timeNow := time.Now()
|
||||
nt := timeNow.Truncate(time.Hour * 1)
|
||||
slf.lasttime = nt.UnixNano()
|
||||
slf.timeinterval = int64(hour) * 3600 * 1e9
|
||||
func (slf *Timer) ResetStartTime() {
|
||||
slf.lasttime = 0
|
||||
}
|
||||
|
||||
func (slf *Timer) CheckTimeOut() bool {
|
||||
now := time.Now().UnixNano()
|
||||
if now-slf.lasttime > slf.timeinterval {
|
||||
slf.lasttime = now
|
||||
|
||||
return true
|
||||
now := time.Now()
|
||||
if slf.setupZeroDBase.Nanoseconds() == 0 {
|
||||
if now.UnixNano() > slf.lasttime+slf.timeinterval {
|
||||
slf.lasttime = now.UnixNano()
|
||||
return true
|
||||
}
|
||||
} else { //整点模式
|
||||
if now.UnixNano() > slf.lasttime+slf.timeinterval {
|
||||
slf.SetupZeroTimer(slf.setupZeroDBase, slf.timeinterval/slf.setupZeroDBase.Nanoseconds())
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (slf *Timer) CheckTimeOutHour() bool {
|
||||
now := time.Now().UnixNano()
|
||||
if now-slf.lasttime > slf.timeinterval {
|
||||
timeNow := time.Now()
|
||||
nt := timeNow.Truncate(time.Hour * 1)
|
||||
slf.lasttime = nt.UnixNano()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (slf *Timer) Reset() {
|
||||
slf.lasttime = time.Now().UnixNano()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user