-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_macros.go
70 lines (55 loc) · 2.05 KB
/
time_macros.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
package macros
import (
"fmt"
"strings"
"time"
"github.com/famarks/grafarg-plugin-sdk-go/backend"
)
func FromMacro(inputString string, timeRange backend.TimeRange) (string, error) {
res, err := applyMacro("$$from", inputString, func(query string, args []string) (string, error) {
return expandTimeMacro(timeRange.From, args)
})
return res, err
}
func ToMacro(inputString string, timeRange backend.TimeRange) (string, error) {
res, err := applyMacro("$$to", inputString, func(query string, args []string) (string, error) {
return expandTimeMacro(timeRange.To, args)
})
return res, err
}
func expandTimeMacro(t time.Time, args []string) (string, error) {
if len(args) < 1 || args[0] == "" {
return fmt.Sprintf("%d", t.UnixMilli()), nil
}
if args[0] == "date" {
if len(args) < 2 || args[1] == ":iso" {
return t.Format("2006-01-02T15:04:05.999Z"), nil
}
}
format := strings.TrimPrefix(strings.Join(args, ","), "date:")
if format == "iso" {
return t.Format("2006-01-02T15:04:05.999Z"), nil
}
format = strings.ReplaceAll(format, "YYYY", "2006")
format = strings.ReplaceAll(format, "YY", "06")
format = strings.ReplaceAll(format, "MMMM", "January")
format = strings.ReplaceAll(format, "MMM", "Jan")
format = strings.ReplaceAll(format, "MM", "01")
format = strings.ReplaceAll(format, "M", "1")
format = strings.ReplaceAll(format, "DD", "02")
format = strings.ReplaceAll(format, "D", "2")
format = strings.ReplaceAll(format, "hh", "03")
format = strings.ReplaceAll(format, "h", "3")
format = strings.ReplaceAll(format, "HH", "15")
format = strings.ReplaceAll(format, "mm", "04")
format = strings.ReplaceAll(format, "m", "4")
format = strings.ReplaceAll(format, "ss", "05")
format = strings.ReplaceAll(format, "s", "5")
format = strings.ReplaceAll(format, "S", "0")
format = strings.ReplaceAll(format, "A", "PM")
format = strings.ReplaceAll(format, "zz", "MST")
format = strings.ReplaceAll(format, "z", "MST")
format = strings.ReplaceAll(format, "dddd", "Monday")
format = strings.ReplaceAll(format, "ddd", "Mon")
return t.Format(format), nil
}