-
Notifications
You must be signed in to change notification settings - Fork 1
/
time.go
333 lines (300 loc) · 7.53 KB
/
time.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package dadsgha
import (
"fmt"
"os"
"strings"
"time"
)
// ProgressInfo display info about progress: i/n if current time >= last + period
// If displayed info, update last
func ProgressInfo(i, n int, start time.Time, last *time.Time, period time.Duration, msg string) {
now := time.Now()
if last.Add(period).Before(now) {
perc := 0.0
if n > 0 {
perc = (float64(i) * 100.0) / float64(n)
}
eta := start
if i > 0 && n > 0 {
etaNs := float64(now.Sub(start).Nanoseconds()) * (float64(n) / float64(i))
etaDuration := time.Duration(etaNs) * time.Nanosecond
eta = start.Add(etaDuration)
if msg != "" {
Printf("%d/%d (%.3f%%), ETA: %v: %s\n", i, n, perc, eta, msg)
} else {
Printf("%d/%d (%.3f%%), ETA: %v\n", i, n, perc, eta)
}
} else {
Printf("%s\n", msg)
}
*last = now
}
}
// HourStart - return time rounded to current hour start
func HourStart(dt time.Time) time.Time {
return time.Date(
dt.Year(),
dt.Month(),
dt.Day(),
dt.Hour(),
0,
0,
0,
time.UTC,
)
}
// NextHourStart - return time rounded to next hour start
func NextHourStart(dt time.Time) time.Time {
return HourStart(dt).Add(time.Hour)
}
// PrevHourStart - return time rounded to prev hour start
func PrevHourStart(dt time.Time) time.Time {
return HourStart(dt).Add(-time.Hour)
}
// DayStart - return time rounded to current day start
func DayStart(dt time.Time) time.Time {
return time.Date(
dt.Year(),
dt.Month(),
dt.Day(),
0,
0,
0,
0,
time.UTC,
)
}
// NextDayStart - return time rounded to next day start
func NextDayStart(dt time.Time) time.Time {
return DayStart(dt).AddDate(0, 0, 1)
}
// PrevDayStart - return time rounded to prev day start
func PrevDayStart(dt time.Time) time.Time {
return DayStart(dt).AddDate(0, 0, -1)
}
// WeekStart - return time rounded to current week start
// Assumes first week day is Sunday
func WeekStart(dt time.Time) time.Time {
wDay := int(dt.Weekday())
// Go returns negative numbers for `modulo` operation when argument is negative
// So instead of wDay-1 I'm using wDay+6
subDays := (wDay + 6) % 7
return DayStart(dt).AddDate(0, 0, -subDays)
}
// NextWeekStart - return time rounded to next week start
func NextWeekStart(dt time.Time) time.Time {
return WeekStart(dt).AddDate(0, 0, 7)
}
// PrevWeekStart - return time rounded to prev week start
func PrevWeekStart(dt time.Time) time.Time {
return WeekStart(dt).AddDate(0, 0, -7)
}
// NDaysStart - return time rounded to current n-days start
func NDaysStart(dt time.Time, nDays int) time.Time {
d := ((dt.YearDay() - 1) / nDays) * nDays
return time.Date(
dt.Year(),
1,
1,
0,
0,
0,
0,
time.UTC,
).AddDate(0, 0, d)
}
// NextNDaysStart - return time rounded to next n-days start
func NextNDaysStart(dt time.Time, nDays int) time.Time {
ndt := dt.AddDate(0, 0, nDays)
if ndt.Year() > dt.Year() {
return time.Date(
ndt.Year(),
1,
1,
0,
0,
0,
0,
time.UTC,
)
}
return NDaysStart(ndt, nDays)
}
// PrevNDaysStart - return time rounded to prev n-days start
func PrevNDaysStart(dt time.Time, nDays int) time.Time {
pdt := dt.AddDate(0, 0, -nDays)
if pdt.Year() < dt.Year() {
pdt = time.Date(
pdt.Year(),
12,
31,
0,
0,
0,
0,
time.UTC,
)
}
return NDaysStart(pdt, nDays)
}
// MonthStart - return time rounded to current month start
func MonthStart(dt time.Time) time.Time {
return time.Date(
dt.Year(),
dt.Month(),
1,
0,
0,
0,
0,
time.UTC,
)
}
// NextMonthStart - return time rounded to next month start
func NextMonthStart(dt time.Time) time.Time {
return MonthStart(dt).AddDate(0, 1, 0)
}
// PrevMonthStart - return time rounded to prev month start
func PrevMonthStart(dt time.Time) time.Time {
return MonthStart(dt).AddDate(0, -1, 0)
}
// QuarterStart - return time rounded to current month start
func QuarterStart(dt time.Time) time.Time {
month := ((dt.Month()-1)/3)*3 + 1
return time.Date(
dt.Year(),
month,
1,
0,
0,
0,
0,
time.UTC,
)
}
// NextQuarterStart - return time rounded to next quarter start
func NextQuarterStart(dt time.Time) time.Time {
return QuarterStart(dt).AddDate(0, 3, 0)
}
// PrevQuarterStart - return time rounded to prev quarter start
func PrevQuarterStart(dt time.Time) time.Time {
return QuarterStart(dt).AddDate(0, -3, 0)
}
// YearStart - return time rounded to current month start
func YearStart(dt time.Time) time.Time {
return time.Date(
dt.Year(),
1,
1,
0,
0,
0,
0,
time.UTC,
)
}
// NextYearStart - return time rounded to next year start
func NextYearStart(dt time.Time) time.Time {
return YearStart(dt).AddDate(1, 0, 0)
}
// PrevYearStart - return time rounded to prev year start
func PrevYearStart(dt time.Time) time.Time {
return YearStart(dt).AddDate(-1, 0, 0)
}
// TimeParseAny - attempts to parse time from string YYYY-MM-DD HH:MI:SS
// Skipping parts from right until only YYYY id left
func TimeParseAny(dtStr string) time.Time {
formats := []string{
"2006-01-02T15:04:05Z",
"2006-01-02 15:04:05",
"2006-01-02 15:04",
"2006-01-02 15",
"2006-01-02",
"2006-01",
"2006",
}
for _, format := range formats {
t, e := time.Parse(format, dtStr)
if e == nil {
return t
}
}
Printf("Error:\nCannot parse date: '%v'\n", dtStr)
fmt.Fprintf(os.Stdout, "Error:\nCannot parse date: '%v'\n", dtStr)
os.Exit(1)
return time.Now()
}
// ParseGHAString - Parse GHA date in 2020121514 format
func ParseGHAString(dtStr string) time.Time {
t, e := time.Parse("2006-01-02-15", dtStr)
if e == nil {
return t
}
Printf("Error:\nCannot parse GHA date: '%v'\n", dtStr)
fmt.Fprintf(os.Stdout, "Error:\nCannot parse GHA date: '%v'\n", dtStr)
os.Exit(1)
return time.Now()
}
// TimeParseES - parse ES time format
func TimeParseES(dtStr string) (time.Time, error) {
dtStr = strings.TrimSpace(strings.Replace(dtStr, "Z", "", -1))
ary := strings.Split(dtStr, "+")
ary2 := strings.Split(ary[0], ".")
var s string
if len(ary2) == 1 {
s = ary2[0] + ".000"
} else {
if len(ary2[1]) > 3 {
ary2[1] = ary2[1][:3]
}
s = strings.Join(ary2, ".")
}
return time.Parse("2006-01-02T15:04:05.000", s)
}
// ToYMDDate - return time formatted as YYYY-MM-DD
func ToYMDDate(dt time.Time) string {
return fmt.Sprintf("%04d-%02d-%02d", dt.Year(), dt.Month(), dt.Day())
}
// ToPeriodDate - return time formatted as N-YYYYMMDD
func ToPeriodDate(dt time.Time, nDays int) string {
return fmt.Sprintf("%d-%04d%02d%02d", nDays, dt.Year(), dt.Month(), dt.Day())
}
// ToYMDate - return time formatted as YYYYMM
func ToYMDate(dt time.Time) string {
return fmt.Sprintf("%04d%02d", dt.Year(), dt.Month())
}
// ToYMDHMSDate - return time formatted as YYYY-MM-DD HH:MI:SS
func ToYMDHMSDate(dt time.Time) string {
return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second())
}
// ToGHADate - return time formatted as YYYY-MM-DD-H
func ToGHADate(dt time.Time) string {
return fmt.Sprintf("%04d-%02d-%02d-%d", dt.Year(), dt.Month(), dt.Day(), dt.Hour())
}
// ToGHADate2 - return time formatted as YYYY-MM-DD-HH
func ToGHADate2(dt time.Time) string {
return fmt.Sprintf("%04d-%02d-%02d-%02d", dt.Year(), dt.Month(), dt.Day(), dt.Hour())
}
// PeriodParse - tries to parse period
func PeriodParse(perStr string) (dur time.Duration, ok bool) {
idx := strings.Index(perStr, "[rate reset in ")
if idx == -1 {
return
}
rateStr := ""
_, err := fmt.Sscanf(perStr[idx:], "[rate reset in %s", &rateStr)
if err != nil || len(rateStr) < 2 {
return
}
rateStr = rateStr[0 : len(rateStr)-1]
if rateStr == "" {
return
}
d, err := time.ParseDuration(rateStr)
if err != nil {
return
}
dur = d
ok = true
return
}