-
Notifications
You must be signed in to change notification settings - Fork 181
/
util.go
296 lines (257 loc) · 7.21 KB
/
util.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package timex
import (
"fmt"
"strings"
"time"
"github.com/gookit/goutil/basefn"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/strutil"
)
// NowUnix is short of time.Now().Unix()
func NowUnix() int64 {
return time.Now().Unix()
}
// Format convert time to string use default layout
func Format(t time.Time) string { return t.Format(DefaultLayout) }
// FormatBy given default layout
func FormatBy(t time.Time, layout string) string { return t.Format(layout) }
// Date format time by given date template. see ToLayout() for template parse.
func Date(t time.Time, template ...string) string { return Datetime(t, template...) }
// Datetime convert time to string use template. see ToLayout() for template parse.
func Datetime(t time.Time, template ...string) string {
return FormatByTpl(t, basefn.FirstOr(template, DefaultTemplate))
}
// DateFormat format time by given date template. see ToLayout()
func DateFormat(t time.Time, template string) string {
return FormatByTpl(t, template)
}
// FormatByTpl format time by given date template. see ToLayout()
func FormatByTpl(t time.Time, template string) string {
return t.Format(ToLayout(template))
}
// FormatUnix time seconds use default layout
func FormatUnix(sec int64, layout ...string) string {
return time.Unix(sec, 0).Format(basefn.FirstOr(layout, DefaultLayout))
}
// FormatUnixBy format time seconds use given layout
func FormatUnixBy(sec int64, layout string) string {
return time.Unix(sec, 0).Format(layout)
}
// FormatUnixByTpl format time seconds use given date template.
// see ToLayout()
func FormatUnixByTpl(sec int64, template ...string) string {
layout := ToLayout(basefn.FirstOr(template, DefaultTemplate))
return time.Unix(sec, 0).Format(layout)
}
// HowLongAgo format given timestamp to string.
func HowLongAgo(sec int64) string {
return basefn.HowLongAgo(sec)
}
// ToTime parse a datetime string. alias of strutil.ToTime()
func ToTime(s string, layouts ...string) (time.Time, error) {
return strutil.ToTime(s, layouts...)
}
// ToDur parse a duration string. alias of ToDuration()
func ToDur(s string) (time.Duration, error) { return ToDuration(s) }
// ToDuration parses a duration string. such as "300ms", "-1.5h" or "2h45m".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func ToDuration(s string) (time.Duration, error) {
return comfunc.ToDuration(s)
}
// IsDuration check the string is a valid duration string. alias of basefn.IsDuration()
func IsDuration(s string) bool { return comfunc.IsDuration(s) }
// TryToTime parse a date string or duration string to time.Time.
//
// if s is empty, return zero time.
func TryToTime(s string, bt time.Time) (time.Time, error) {
if s == "" {
return ZeroTime, nil
}
if s == "now" {
return time.Now(), nil
}
// if s is a duration string, add it to bt(base time)
if IsDuration(s) {
dur, err := ToDuration(s)
if err != nil {
return ZeroTime, err
}
return bt.Add(dur), nil
}
// as a date string, parse it to time.Time
return ToTime(s)
}
// InRange check the dst time is in the range of start and end.
//
// if start is zero, only check dst < end,
// if end is zero, only check dst > start.
func InRange(dst, start, end time.Time) bool {
if start.IsZero() && end.IsZero() {
return false
}
if start.IsZero() {
return dst.Before(end)
}
if end.IsZero() {
return dst.After(start)
}
return dst.After(start) && dst.Before(end)
}
// ParseRangeOpt is the option for ParseRange
type ParseRangeOpt struct {
// BaseTime is the base time for relative time string.
// if is zero, use time.Now() as base time.
BaseTime time.Time
// OneAsEnd is the option for one time range.
// - False: "-1h" => "-1h,0"; "1h" => "+1h, feature"
// - True: "-1h" => "zero,-1h"; "1h" => "zero,1h"
OneAsEnd bool
// AutoSort is the option for sort the time range.
AutoSort bool
// SepChar is the separator char for time range string. default is '~'
SepChar byte
// BeforeFn hook for before parse time string.
BeforeFn func(string) string
// KeywordFn is the function for parse keyword time string.
KeywordFn func(string) (time.Time, time.Time, error)
}
func ensureOpt(opt *ParseRangeOpt) *ParseRangeOpt {
if opt == nil {
opt = &ParseRangeOpt{BaseTime: time.Now(), SepChar: '~'}
} else {
if opt.BaseTime.IsZero() {
opt.BaseTime = time.Now()
}
if opt.SepChar == 0 {
opt.SepChar = '~'
}
}
return opt
}
// ParseRange parse time range expression string to time.Time range.
// - "0" is alias of "now"
//
// Expression format:
//
// "-5h~-1h" => 5 hours ago to 1 hour ago
// "1h~5h" => 1 hour after to 5 hours after
// "-1h~1h" => 1 hour ago to 1 hour after
// "-1h" => 1 hour ago to feature. eq "-1h,"
// "-1h~0" => 1 hour ago to now.
// "< -1h" OR "~-1h" => 1 hour ago. eq ",-1h"
// "> 1h" OR "1h" => 1 hour after to feature
// // keyword: now, today, yesterday, tomorrow
// "today" => today start to today end
// "yesterday" => yesterday start to yesterday end
// "tomorrow" => tomorrow start to tomorrow end
//
// Usage:
//
// start, end, err := ParseRange("-1h~1h", nil)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(start, end)
func ParseRange(expr string, opt *ParseRangeOpt) (start, end time.Time, err error) {
opt = ensureOpt(opt)
expr = strings.TrimSpace(expr)
if expr == "" {
err = fmt.Errorf("invalid time range expr %q", expr)
return
}
// parse time range. eg: "5h~1h"
if strings.IndexByte(expr, opt.SepChar) > -1 {
s1, s2 := strutil.TrimCut(expr, string(opt.SepChar))
if s1 == "" && s2 == "" {
err = fmt.Errorf("invalid time range expr: %s", expr)
return
}
if s1 != "" {
start, err = TryToTime(s1, opt.BaseTime)
if err != nil {
return
}
}
if s2 != "" {
end, err = TryToTime(s2, opt.BaseTime)
// auto sort range time
if opt.AutoSort && err == nil {
if !start.IsZero() && start.After(end) {
start, end = end, start
}
}
}
return
}
// single time. eg: "5h", "1h", "-1h"
if IsDuration(expr) {
tt, err1 := TryToTime(expr, opt.BaseTime)
if err1 != nil {
err = err1
return
}
if opt.OneAsEnd {
end = tt
} else {
start = tt
}
return
}
// with compare operator. eg: "<1h", ">1h"
if expr[0] == '<' || expr[0] == '>' {
tt, err1 := TryToTime(strings.Trim(expr[1:], " ="), opt.BaseTime)
if err1 != nil {
err = err1
return
}
if expr[0] == '<' {
end = tt
} else {
start = tt
}
return
}
// parse keyword time string
switch expr {
case "0":
if opt.OneAsEnd {
end = opt.BaseTime
} else {
start = opt.BaseTime
}
case "now":
if opt.OneAsEnd {
end = time.Now()
} else {
start = time.Now()
}
case "today":
start = DayStart(opt.BaseTime)
end = DayEnd(opt.BaseTime)
case "yesterday":
yd := opt.BaseTime.AddDate(0, 0, -1)
start = DayStart(yd)
end = DayEnd(yd)
case "tomorrow":
td := opt.BaseTime.AddDate(0, 0, 1)
start = DayStart(td)
end = DayEnd(td)
default:
// single datetime. eg: "2019-01-01"
tt, err1 := TryToTime(expr, opt.BaseTime)
if err1 != nil {
if opt.KeywordFn == nil {
err = fmt.Errorf("invalid keyword time string: %s", expr)
return
}
start, end, err = opt.KeywordFn(expr)
return
}
if opt.OneAsEnd {
end = tt
} else {
start = tt
}
}
return
}