-
Notifications
You must be signed in to change notification settings - Fork 0
/
hour_expression.go
executable file
·174 lines (145 loc) · 3.88 KB
/
hour_expression.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package timeexpression
import (
"errors"
"sort"
"strings"
)
//hourExpression 解析小时的表达式
type hourExpression struct {
hourUnits []*hourUnitExpression
isAll bool
}
// newHourExpression 格式为 *,hh:mm:ss-hh:mm:ss[,hh:mm:ss-hh:mm:ss]...
func newHourExpression(hourStr string) (*hourExpression, error) {
hourStr = strings.Trim(hourStr, " ")
var hourUnits []*hourUnitExpression
isAll := false
splitHourStr := strings.Split(hourStr, ",")
for _, splitStr := range splitHourStr {
unitExpression, err := newHourUnitExpression(splitStr)
if err != nil {
return nil, err
}
if unitExpression.isAll {
isAll = true
}
hourUnits = append(hourUnits, unitExpression)
}
// 简单对开始时间排序(从小到大)
sort.Slice(hourUnits, func(i, j int) bool {
return hourUnits[i].start.toSec() < hourUnits[j].start.toSec()
})
expression := &hourExpression{
hourUnits: hourUnits,
isAll: isAll,
}
err := expression.check()
if err != nil {
return nil, err
}
return expression, nil
}
// check 检查参数
func (expression *hourExpression) check() error {
// 时间不能有重叠
// 时间已经被排序过了
var preUnit *hourUnitExpression
for _, unit := range expression.hourUnits {
if preUnit == nil {
preUnit = unit
} else {
if preUnit.end.toSec() >= unit.start.toSec() {
return errors.New("hour error: time overlapping")
}
}
}
return nil
}
func (expression *hourExpression) isIn(hour, minute, sec int) bool {
for _, unitExpression := range expression.hourUnits {
if unitExpression.isIn(hour, minute, sec) {
return true
}
}
return false
}
// getStart 获取开始的时分秒
// 1. 如果在周期内,则返回本次周期的时分秒
// 2. 如果在周期外
// 在开始前,返回开始的时分秒
// 在范围后,则返回错误
func (expression *hourExpression) getStart(hour, min, sec int) (hourUint hourUnit, addDay bool, err error) {
// 尝试是否有范围内的
for _, unit := range expression.hourUnits {
if unit.isIn(hour, min, sec) {
return unit.start, false, nil
}
}
// 都在范围外了, 则找一个将来最接近的
paramUnit := hourUnit{
Hour: hour,
Minute: min,
Sec: sec,
}
paramSec := paramUnit.toSec()
var targetUnit *hourUnit
var minUnit *hourUnit
for _, unit := range expression.hourUnits {
if minUnit == nil || minUnit.toSec() > unit.start.toSec() {
minUnit = &unit.start
}
if paramSec >= unit.end.toSec() {
continue
}
if targetUnit == nil || targetUnit.toSec() > unit.start.toSec() {
targetUnit = &unit.start
}
}
if targetUnit != nil {
return *targetUnit, false, nil
}
if minUnit == nil {
return hourUnit{}, false, errors.New("hourExpression getStart get unreachable error")
}
return *minUnit, true, nil
}
// getEnd 获取结束的时分秒
// 1. 如果在周期内,则返回本次周期的结束时分秒
// 2. 如果在周期外
// 在结束前,返回结束的时分秒
// 在范围后,则返回错误
func (expression *hourExpression) getEnd(hour, min, sec int) (hourUint hourUnit, addDay bool, err error) {
// 尝试是否有范围内的
for _, unit := range expression.hourUnits {
if unit.isIn(hour, min, sec) {
return unit.end, false, nil
}
}
// 都在范围外了, 则找一个将来最接近的
paramUnit := hourUnit{
Hour: hour,
Minute: min,
Sec: sec,
}
paramSec := paramUnit.toSec()
var targetUnit *hourUnit
var minUnit *hourUnit
for _, unit := range expression.hourUnits {
if minUnit == nil || minUnit.toSec() > unit.end.toSec() {
minUnit = &unit.end
}
if paramSec > unit.end.toSec() {
continue
}
if targetUnit == nil || targetUnit.toSec() > unit.end.toSec() {
targetUnit = &unit.end
}
}
if targetUnit != nil {
return *targetUnit, false, nil
}
if minUnit == nil {
return hourUnit{}, false, errors.New("hourExpression getEnd get unreachable error")
}
return *minUnit, true, nil
}