-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtime.go
559 lines (523 loc) · 13.3 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package devstatscode
import (
"database/sql"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
var (
gSeeded bool
)
// IntervalHours - return number of hour from for a given interval
func IntervalHours(period string) string {
ary := strings.Split(period, " ")
tokens := []string{}
for _, token := range ary {
if token != "" {
tokens = append(tokens, token)
}
}
if len(tokens) < 1 {
return "0"
}
n := 1.0
interval := tokens[0]
if len(tokens) > 1 {
var err error
n, err = strconv.ParseFloat(tokens[0], 64)
FatalOnError(err)
if n < 0.0 {
n = 0.0
}
interval = tokens[1]
}
mul := 1.0
switch strings.ToLower(interval) {
case "s", "sec", "second", "secs", "seconds":
mul = 1.0 / 3600.0
case "min", "minute", "mins", "minutes":
mul = 1.0 / 60.0
case "h", "hr", Hour, "hrs", "hours":
case "d", Day, "days":
mul = 24.0
case "w", Week, "weeks":
mul = 168.0
case Month, "months":
mul = 730.5
case "q", Quarter, "quarters":
mul = 2191.5
case "y", Year, "years":
mul = 8766.0
default:
Fatalf("unknown interval '%s'\n", interval)
}
return fmt.Sprintf("%f", n*mul)
}
// RangeHours - return number of hour from 'from' to 'to' as float64 converted to string
func RangeHours(from, to time.Time) string {
if !to.After(from) {
return "0"
}
return fmt.Sprintf("%f", to.Sub(from).Hours())
}
// GetDateAgo returns date: 'from' - 'n hours/days' etc.
func GetDateAgo(con *sql.DB, ctx *Ctx, from time.Time, ago string) (tm time.Time) {
rows := QuerySQLWithErr(
con,
ctx,
fmt.Sprintf(
"select %s::timestamp - %s::interval",
NValue(1),
NValue(2),
),
ToYMDHMSDate(from),
ago,
)
defer func() { FatalOnError(rows.Close()) }()
for rows.Next() {
FatalOnError(rows.Scan(&tm))
}
FatalOnError(rows.Err())
return
}
// 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
}
}
// Probab - return true with percent % probablity
func Probab(percent int) bool {
if !gSeeded {
rand.Seed(time.Now().UnixNano())
gSeeded = true
}
return rand.Intn(100) < percent
}
// ComputePeriodAtThisDate - for some longer periods, only recalculate them on specific dates/times
// see: time_test.go
func ComputePeriodAtThisDate(ctx *Ctx, period string, idt time.Time, hist bool) bool {
if ctx.ComputeAll {
return true
}
if ctx.ComputePeriods != nil {
data, ok := ctx.ComputePeriods[period]
if !ok {
return false
}
_, ok = data[hist]
return ok
}
dt := HourStart(idt)
// dtc: date with current hour start
// dtn: tomorrow with current hour start
// dth: current data with tz offset
dtc := dt
dtn := dt.AddDate(0, 0, 1)
dth := dt.Add(time.Hour * time.Duration(ctx.TmOffset))
// h: current hour with tz offset
// ch: current hour without tz offset
h := dth.Hour()
ch := dtc.Hour()
periodStart := period[0:1]
if periodStart == "h" {
return true
} else if periodStart == "d" {
if len(period) == 1 {
return true
}
if ctx.RandComputeAtThisDate {
return Probab(25)
}
return h == 1 || h == 6 || h == 9 || h == 13 || h == 18 || h == 21
} else if hist && periodStart == "a" {
periodLen := len(period)
periodEnd := period[periodLen-2:]
if periodEnd == "_n" {
if ctx.RandComputeAtThisDate {
return Probab(20)
}
return h == 1 || h == 8 || h == 15 || h == 13 || h == 20
}
if ctx.RandComputeAtThisDate {
return Probab(9)
}
return h == 2 || h == 3
} else if hist && periodStart == "c" {
if ctx.RandComputeAtThisDate {
return Probab(9)
}
return h == 3 || h == 4
}
if hist {
if periodStart == "w" {
if ctx.RandComputeAtThisDate {
return Probab(29)
}
return h%7 == 0
} else if periodStart == "m" || periodStart == "q" || periodStart == "y" {
if ctx.RandComputeAtThisDate {
return Probab(9)
}
return h == 23 || h == 18
}
} else {
if periodStart == "w" {
if ctx.RandComputeAtThisDate {
return Probab(60) && h >= 12 && int(dtc.Weekday()) == 0
}
return ch == 23 && int(dtc.Weekday()) == 0
} else if periodStart == "m" {
if ctx.RandComputeAtThisDate {
return Probab(80) && h < 12 && dtn.Day() == 1
}
return ch == 23 && dtn.Day() == 1
} else if periodStart == "q" {
if ctx.RandComputeAtThisDate {
return h > 16 && dtn.Day() == 1 && dtn.Month()%3 == 1
}
return ch == 23 && dtn.Day() == 1 && dtn.Month()%3 == 1
} else if periodStart == "y" {
if ctx.RandComputeAtThisDate {
return h < 8 && dtn.Day() == 1 && dtn.Month() == 1
}
return ch == 23 && dtn.Day() == 1 && dtn.Month() == 1
}
}
Fatalf("ComputePeriodAtThisDate: unknown period: '%s', hist: %v", period, hist)
return false
}
// 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)
}
// 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)
}
// 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
}
// 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()
}
// 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())
}
// 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())
}
// 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())
}
// ToYMDHDate - return time formatted as YYYY-MM-DD HH
func ToYMDHDate(dt time.Time) string {
return fmt.Sprintf("%04d-%02d-%02d %d", dt.Year(), dt.Month(), dt.Day(), dt.Hour())
}
// DescriblePeriodInHours - return string description of a time period given in hours
func DescriblePeriodInHours(hrs float64) (desc string) {
secs := int((hrs * 3600.0) + 0.5)
if secs < 0 {
return "- " + DescriblePeriodInHours(-hrs)
}
if secs == 0 {
return "zero"
}
weeks := secs / 604800
if weeks > 0 {
if weeks > 1 {
desc += strconv.Itoa(weeks) + " weeks "
} else {
desc += "1 week "
}
secs -= weeks * 604800
}
days := secs / 86400
if days > 0 {
if days > 1 {
desc += strconv.Itoa(days) + " days "
} else {
desc += "1 day "
}
secs -= days * 86400
}
hours := secs / 3600
if hours > 0 {
if hours > 1 {
desc += strconv.Itoa(hours) + " hours "
} else {
desc += "1 hour "
}
secs -= hours * 3600
}
minutes := secs / 60
if minutes > 0 {
if minutes > 1 {
desc += strconv.Itoa(minutes) + " minutes "
} else {
desc += "1 minute "
}
secs -= minutes * 60
}
if secs > 0 {
if secs > 1 {
desc += strconv.Itoa(secs) + " seconds "
} else {
desc += "1 second "
}
}
return strings.TrimSpace(desc)
}
// AddNIntervals adds (using nextIntervalStart) or subtracts (using prevIntervalStart) N itervals to the given date
// Functions Next/Prev can use Hour, Day, Week, Month, Quarter, Year functions (defined in this module) or other custom defined functions
// With `func(time.Time) time.Time` signature
func AddNIntervals(dt time.Time, n int, nextIntervalStart, prevIntervalStart func(time.Time) time.Time) time.Time {
if n == 0 {
return dt
}
times := n
fun := nextIntervalStart
if n < 0 {
times = -n
fun = prevIntervalStart
}
for i := 0; i < times; i++ {
dt = fun(dt)
}
return dt
}
// GetIntervalFunctions - return interval name, interval number, interval start, next, prev function from interval abbr: h|d2|w3|m4|q|y
// w3 = 3 weeks, q2 = 2 quarters, y = year (1), d7 = 7 days (not the same as w), m3 = 3 months (not the same as q)
func GetIntervalFunctions(intervalAbbr string, allowUnknown bool) (interval string, n int, intervalStart, nextIntervalStart, prevIntervalStart func(time.Time) time.Time) {
n = 1
switch strings.ToLower(intervalAbbr[0:1]) {
case "h":
interval = Hour
intervalStart = HourStart
nextIntervalStart = NextHourStart
prevIntervalStart = PrevHourStart
case "d":
interval = Day
intervalStart = DayStart
nextIntervalStart = NextDayStart
prevIntervalStart = PrevDayStart
case "w":
interval = Week
intervalStart = WeekStart
nextIntervalStart = NextWeekStart
prevIntervalStart = PrevWeekStart
case "m":
interval = Month
intervalStart = MonthStart
nextIntervalStart = NextMonthStart
prevIntervalStart = PrevMonthStart
case "q":
interval = Quarter
intervalStart = QuarterStart
nextIntervalStart = NextQuarterStart
prevIntervalStart = PrevQuarterStart
case "y":
interval = Year
intervalStart = YearStart
nextIntervalStart = NextYearStart
prevIntervalStart = PrevYearStart
default:
if !allowUnknown {
Printf("Error:\nUnknown interval '%v'\n", intervalAbbr)
fmt.Fprintf(os.Stdout, "Error:\nUnknown interval '%v'\n", intervalAbbr)
os.Exit(1)
} else {
return
}
}
lenIntervalAbbr := len(intervalAbbr)
if lenIntervalAbbr > 1 {
nStr := intervalAbbr[1:lenIntervalAbbr]
nI, err := strconv.Atoi(nStr)
FatalOnError(err)
if nI > 1 {
n = nI
}
}
return
}