forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
64 lines (54 loc) · 1.11 KB
/
strings.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
package util
import (
"fmt"
"math"
"regexp"
"time"
)
func StringsFallback2(val1 string, val2 string) string {
return stringsFallback(val1, val2)
}
func StringsFallback3(val1 string, val2 string, val3 string) string {
return stringsFallback(val1, val2, val3)
}
func stringsFallback(vals ...string) string {
for _, v := range vals {
if v != "" {
return v
}
}
return ""
}
func SplitString(str string) []string {
if len(str) == 0 {
return []string{}
}
return regexp.MustCompile("[, ]+").Split(str, -1)
}
func GetAgeString(t time.Time) string {
if t.IsZero() {
return "?"
}
sinceNow := time.Since(t)
minutes := sinceNow.Minutes()
years := int(math.Floor(minutes / 525600))
months := int(math.Floor(minutes / 43800))
days := int(math.Floor(minutes / 1440))
hours := int(math.Floor(minutes / 60))
if years > 0 {
return fmt.Sprintf("%dy", years)
}
if months > 0 {
return fmt.Sprintf("%dM", months)
}
if days > 0 {
return fmt.Sprintf("%dd", days)
}
if hours > 0 {
return fmt.Sprintf("%dh", hours)
}
if int(minutes) > 0 {
return fmt.Sprintf("%dm", int(minutes))
}
return "< 1m"
}