-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.go
61 lines (57 loc) · 1.33 KB
/
util.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
package clistats
import (
"fmt"
"strconv"
"time"
)
// String returns the string representation of a few different types that are simple
// enough to be represented as a static metric for stats.
//
// For everything else, it uses fmt.Sprint but it is very recommended to use
// only small and easy types.
func String(from interface{}) string {
// Special case for nil values
if from == nil {
return "n/a"
}
switch T := from.(type) {
case string:
return T
case fmt.Stringer:
return T.String()
case bool:
return strconv.FormatBool(T)
case int:
return strconv.FormatInt(int64(T), 10)
case int32:
return strconv.FormatInt(int64(T), 10)
case int64:
return strconv.FormatInt(T, 10)
case uint32:
return strconv.FormatUint(uint64(T), 10)
case uint64:
return strconv.FormatUint(T, 10)
case float32:
return strconv.FormatFloat(float64(T), 'E', -1, 32)
case float64:
return strconv.FormatFloat(T, 'E', -1, 64)
case []byte:
return string(T)
case *[]byte:
return string(*T)
case *string:
return *T
default:
return fmt.Sprintf("%v", from)
}
}
// FmtDuration formats the duration for the time elapsed
func FmtDuration(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
return fmt.Sprintf("%d:%02d:%02d", h, m, s)
}