1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
time.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-04-03 15:06 . dao
package wrapper
import "strings"
// Time 时间范围
type Time struct {
Range[int64]
}
func (t *Time) HasValid() bool {
if !t.Range.HasValid() {
return false
}
return t.Min > 0 || t.Max > 0
}
type TimeV2 struct {
TimCol string `form:"time_col" json:"time_col"`
TimOp string `form:"time_op" json:"time_op"`
Start int64 `form:"start" json:"start"`
End int64 `form:"end" json:"end"`
}
func (t *TimeV2) HasValid() bool {
// 时间有效,操作符长度不能太长,太长就是无效,在进行ToUpper时,造成时间消耗
return len(t.TimOp) > 0 && len(t.TimOp) < 5 && len(t.TimCol) > 0 && (t.Start > 0 || t.End > 0)
}
func (t *TimeV2) BuildCond(cond *Condition) {
if !t.HasValid() {
return
}
op := strings.ToUpper(t.TimOp)
if op == "GT" {
cond.Gt(t.TimCol, t.Start)
} else if op == "GTE" {
cond.GtEq(t.TimCol, t.Start)
} else if op == "LT" {
cond.Lt(t.TimCol, t.End)
} else if op == "LTE" {
cond.LtEq(t.TimCol, t.End)
} else if op == "BET" {
cond.Between(t.TimCol, t.Start, t.End)
}
}
func (t *TimeV2) Adjust() {
if t.Start > t.End {
t.Start, t.End = t.End, t.Start
}
}
func (t *TimeV2) RangeLegal(legal int64) {
t.Adjust()
if t.End-t.Start <= legal {
return
}
t.Start = t.End - legal
if t.Start < 0 {
t.Start = 0
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.114

搜索帮助