2 Star 4 Fork 10

王布衣/engine

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
range_time.go 1.77 KB
Copy Edit Raw Blame History
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 (
errnoConfig = 0
ErrTimeFormat = exception.New(errnoConfig+1, "时间格式错误")
ErrRangeFormat = exception.New(errnoConfig+2, "数值范围格式错误")
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) 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")
}
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.0.3

Search

Cb406eda 1850385 E526c682 1850385