1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
expire.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-08-12 18:33 . token
package timer
import (
"time"
)
type Expire struct {
StartIn int64 `json:"startIn"`
ExpireIn int64 `json:"expireIn"` //过期时间点
}
func NowExpireWithMillSecond(msec int64) Expire {
return NowMillExpireWithDuration(time.Millisecond * time.Duration(msec))
}
func NowExpireWithSecond(secondIn int64) Expire {
return NowExpireWithDuration(time.Second * time.Duration(secondIn))
}
func NowExpireWithMinute(minuteIn int64) Expire {
return NowExpireWithDuration(time.Minute * time.Duration(minuteIn))
}
func NowExpireWithHour(hourIn int64) Expire {
return NowExpireWithDuration(time.Hour * time.Duration(hourIn))
}
// NowExpireWithDuration 秒
func NowExpireWithDuration(duration time.Duration) Expire {
now := time.Now()
expireIn := now.Add(duration).Unix()
return Expire{StartIn: now.Unix(), ExpireIn: expireIn}
}
// NowMillExpireWithDuration 豪秒
func NowMillExpireWithDuration(duration time.Duration) Expire {
now := time.Now()
expireIn := now.Add(duration).UnixMilli()
return Expire{StartIn: now.UnixMilli(), ExpireIn: expireIn}
}
func (e *Expire) IsExpired() bool {
return e.IsDiff(0)
}
func (e *Expire) IsDiff(diff int64) bool {
if e.ExpireIn == -1 { //永久不过期
return false
}
return CurrentS()+diff >= e.ExpireIn
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.8.80

搜索帮助