2 Star 4 Fork 10

王布衣/engine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
range_time.go 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2024-01-03 09:59 . 修复f10季报死锁的bug
package config
import (
"fmt"
"gitee.com/quant1x/gox/exception"
"gitee.com/quant1x/pkg/yaml"
"regexp"
"strings"
"time"
)
// 值范围正则表达式
var (
stringRangePattern = "[~-]\\s*"
stringRangeRegexp = regexp.MustCompile(stringRangePattern)
)
var (
ErrTimeFormat = exception.New(errnoConfig+1, "时间格式错误")
formatOfTimestamp = time.TimeOnly
)
func getTradingTimestamp() string {
now := time.Now()
return now.Format(formatOfTimestamp)
}
// TimeRange 时间范围
type TimeRange struct {
begin string // 开始时间
end string // 结束时间
}
func (this TimeRange) String() string {
return fmt.Sprintf("{begin: %s, end: %s}", this.begin, this.end)
}
func (this TimeRange) v2String() string {
return fmt.Sprintf("%s~%s", this.begin, this.end)
}
func (this *TimeRange) Parse(text string) error {
text = strings.TrimSpace(text)
arr := stringRangeRegexp.Split(text, -1)
if len(arr) != 2 {
return ErrTimeFormat
}
this.begin = strings.TrimSpace(arr[0])
this.end = strings.TrimSpace(arr[1])
if this.begin > this.end {
this.begin, this.end = this.end, this.begin
}
return nil
}
//// UnmarshalText 设置默认值调用
//func (this *TimeRange) UnmarshalText(text []byte) error {
// //TODO implement me
// panic("implement me")
//}
// UnmarshalYAML YAML自定义解析
func (this *TimeRange) UnmarshalYAML(node *yaml.Node) error {
var key, value string
if len(node.Content) == 0 {
value = node.Value
} else if len(node.Content) == 2 {
key = node.Content[0].Value
value = node.Content[1].Value
}
_ = key
return this.Parse(value)
}
func (this *TimeRange) IsTrading(timestamp ...string) bool {
var tm string
if len(timestamp) > 0 {
tm = strings.TrimSpace(timestamp[0])
} else {
tm = getTradingTimestamp()
}
if tm >= this.begin && tm <= this.end {
return true
}
return false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quant1x/engine.git
git@gitee.com:quant1x/engine.git
quant1x
engine
engine
v1.4.3

搜索帮助

Cb406eda 1850385 E526c682 1850385