/
templatefuncs.go
101 lines (88 loc) · 2.24 KB
/
templatefuncs.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
package main
import (
"math"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/bytefmt"
)
func DigestShortener(s string) string {
shortener := strings.SplitAfter(s, "sha256:")
return string(shortener[1][0:8])
}
func ByteDiffFmt(val1 int64, val2 int64) string {
if val1 > val2 {
return ByteFmt(val2 - val1)
}
return ByteFmt(val2 - val1)
}
func ByteFmt(v interface{}) string {
switch u := v.(type) {
case string:
val, _ := strconv.Atoi(u)
return bytefmt.ByteSize(uint64(val))
case int:
return bytefmt.ByteSize(uint64(u))
case int8:
return bytefmt.ByteSize(uint64(u))
case int16:
return bytefmt.ByteSize(uint64(u))
case int32:
return bytefmt.ByteSize(uint64(u))
case int64:
return bytefmt.ByteSize(uint64(u))
case uint:
return bytefmt.ByteSize(uint64(u))
case uint8:
return bytefmt.ByteSize(uint64(u))
case uint16:
return bytefmt.ByteSize(uint64(u))
case uint32:
return bytefmt.ByteSize(uint64(u))
case uint64:
return bytefmt.ByteSize(u)
default:
return ""
}
}
// TimeAgo returns the rounded form of amount of time elapsed between now and the passed time
func TimeAgo(passedTime time.Time) string {
// Get the elapsed number of hours
floatHoursAgo := time.Since(passedTime).Seconds()
// Take abs value since we handle the "ago" part below
floatHoursAgo = math.Abs(floatHoursAgo)
// Round the float
secondsAgo := Round(floatHoursAgo)
switch {
case secondsAgo < 60:
return strconv.Itoa(secondsAgo) + " seconds ago"
case secondsAgo >= 60 && secondsAgo < 3600:
return strconv.Itoa(secondsAgo/60) + " minutes ago"
case secondsAgo >= 3600 && secondsAgo < 86400:
if secondsAgo/3600 == 1 {
return "1 hour ago"
}
return strconv.Itoa(secondsAgo/3600) + " hours ago"
case secondsAgo >= 86400:
if secondsAgo/86400 == 1 {
return "1 day ago"
}
return strconv.Itoa(secondsAgo/86400) + " days ago"
}
return ""
}
// Round rounds the float to the nearest int
func Round(f float64) int {
if math.Abs(f) < 0.5 {
return 0
}
return int(f + math.Copysign(0.5, f))
}
// StatToSeconds takes in a beego stat param (e.g 20.40us or 15.20ms) and returns the time in seconds
func StatToSeconds(stat string) (float64, error) {
t, err := time.ParseDuration(stat)
if err != nil {
return 0, err
}
return t.Seconds(), nil
}