-
Notifications
You must be signed in to change notification settings - Fork 191
/
parse.go
231 lines (206 loc) · 5.14 KB
/
parse.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package strutil
import (
"errors"
"strconv"
"strings"
"time"
"unicode"
"github.com/gookit/goutil/byteutil"
)
// MustToTime convert date string to time.Time
func MustToTime(s string, layouts ...string) time.Time {
t, err := ToTime(s, layouts...)
if err != nil {
panic(err)
}
return t
}
// auto match use some commonly layouts.
// key is layout length.
var layoutMap = map[int][]string{
6: {"200601", "060102", time.Kitchen},
8: {"20060102", "06-01-02"},
10: {"2006-01-02"},
13: {"2006-01-02 15"},
15: {time.Stamp},
16: {"2006-01-02 15:04"},
19: {"2006-01-02 15:04:05", time.RFC822, time.StampMilli},
20: {"2006-01-02 15:04:05Z"},
21: {time.RFC822Z},
22: {time.StampMicro},
23: {"2006-01-02 15:04:05.000", "2006-01-02 15:04:05.999"},
24: {time.ANSIC},
25: {time.RFC3339, time.StampNano},
// time.Layout}, // must go >= 1.19
26: {"2006-01-02 15:04:05.000000"},
28: {time.UnixDate},
29: {time.RFC1123, "2006-01-02 15:04:05.000000000"},
30: {time.RFC850},
31: {time.RFC1123Z},
35: {time.RFC3339Nano},
}
// ToTime convert date string to time.Time
//
// NOTE: always use local timezone.
func ToTime(s string, layouts ...string) (t time.Time, err error) {
// custom layout
if len(layouts) > 0 {
if len(layouts[0]) > 0 {
return time.ParseInLocation(layouts[0], s, time.Local)
}
err = ErrDateLayout
return
}
// auto match use some commonly layouts.
strLn := len(s)
maybeLayouts, ok := layoutMap[strLn]
if !ok {
err = ErrInvalidParam
return
}
var hasAlphaT bool
if pos := strings.IndexByte(s, 'T'); pos > 0 && pos < 12 {
hasAlphaT = true
}
hasSlashR := strings.IndexByte(s, '/') > 0
for _, layout := range maybeLayouts {
// date string has "T". eg: "2006-01-02T15:04:05"
if hasAlphaT {
layout = strings.Replace(layout, " ", "T", 1)
}
// date string has "/". eg: "2006/01/02 15:04:05"
if hasSlashR {
layout = strings.Replace(layout, "-", "/", -1)
}
t, err = time.ParseInLocation(layout, s, time.Local)
if err == nil {
return
}
}
// t, err = time.ParseInLocation(layout, s, time.Local)
return
}
// ParseSizeOpt parse size expression options
type ParseSizeOpt struct {
// OneAsMax if only one size value, use it as max size. default is false
OneAsMax bool
// SepChar is the separator char for time range string. default is '~'
SepChar byte
// KeywordFn is the function for parse keyword time string.
KeywordFn func(string) (min, max uint64, err error)
}
func ensureOpt(opt *ParseSizeOpt) *ParseSizeOpt {
if opt == nil {
opt = &ParseSizeOpt{SepChar: '~'}
} else {
if opt.SepChar == 0 {
opt.SepChar = '~'
}
}
return opt
}
// ErrInvalidSizeExpr invalid size expression error
var ErrInvalidSizeExpr = errors.New("invalid size expr")
// ParseSizeRange parse range size expression to min and max size.
//
// Expression format:
//
// "1KB~2MB" => 1KB to 2MB
// "-1KB" => <1KB
// "~1MB" => <1MB
// "< 1KB" => <1KB
// "1KB" => >1KB
// "1KB~" => >1KB
// ">1KB" => >1KB
// "+1KB" => >1KB
func ParseSizeRange(expr string, opt *ParseSizeOpt) (min, max uint64, err error) {
opt = ensureOpt(opt)
expr = strings.TrimSpace(expr)
if expr == "" {
err = ErrInvalidSizeExpr
return
}
// parse size range. eg: "1KB~2MB"
if strings.IndexByte(expr, '~') > -1 {
s1, s2 := TrimCut(expr, "~")
if s1 != "" {
min, err = ToByteSize(s1)
if err != nil {
return
}
}
if s2 != "" {
max, err = ToByteSize(s2)
}
return
}
// parse single size. eg: "1KB"
if byteutil.IsNumChar(expr[0]) {
min, err = ToByteSize(expr)
if err != nil {
return
}
if opt.OneAsMax {
max = min
}
return
}
// parse with prefix. eg: "<1KB", ">= 1KB", "-1KB", "+1KB"
switch expr[0] {
case '<', '-':
max, err = ToByteSize(strings.Trim(expr[1:], " ="))
case '>', '+':
min, err = ToByteSize(strings.Trim(expr[1:], " ="))
default:
// parse keyword. eg: "small", "large"
if opt.KeywordFn != nil {
min, max, err = opt.KeywordFn(expr)
} else {
err = ErrInvalidSizeExpr
}
}
return
}
// SafeByteSize converts size string like 1GB/1g or 12mb/12M into an unsigned integer number of bytes
func SafeByteSize(sizeStr string) uint64 {
size, _ := ToByteSize(sizeStr)
return size
}
// ToByteSize converts size string like 1GB/1g or 12mb/12M into an unsigned integer number of bytes
func ToByteSize(sizeStr string) (uint64, error) {
sizeStr = strings.TrimSpace(sizeStr)
lastPos := len(sizeStr) - 1
if lastPos < 0 {
return 0, nil
}
if sizeStr[lastPos] == 'b' || sizeStr[lastPos] == 'B' {
// last second char is k,m,g,t
lastSec := sizeStr[lastPos-1]
if lastSec > 'A' {
lastPos--
}
} else if IsNumChar(sizeStr[lastPos]) { // not unit suffix. eg: 346
return strconv.ParseUint(sizeStr, 10, 32)
}
multiplier := float64(1)
switch unicode.ToLower(rune(sizeStr[lastPos])) {
case 'k':
multiplier = 1 << 10
case 'm':
multiplier = 1 << 20
case 'g':
multiplier = 1 << 30
case 't':
multiplier = 1 << 40
case 'p':
multiplier = 1 << 50
default: // b
multiplier = 1
}
sizeNum := strings.TrimSpace(sizeStr[:lastPos])
size, err := strconv.ParseFloat(sizeNum, 64)
if err != nil {
return 0, err
}
return uint64(size * multiplier), nil
}