forked from openshift/origin
/
helpers.go
50 lines (47 loc) · 902 Bytes
/
helpers.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
package metrics
import (
"fmt"
"math"
"strconv"
"time"
)
func ConvertToFloat64(v interface{}) (float64, error) {
switch i := v.(type) {
case float64:
return float64(i), nil
case float32:
return float64(i), nil
case int64:
return float64(i), nil
case int32:
return float64(i), nil
case int16:
return float64(i), nil
case int8:
return float64(i), nil
case uint64:
return float64(i), nil
case uint32:
return float64(i), nil
case uint16:
return float64(i), nil
case uint8:
return float64(i), nil
case int:
return float64(i), nil
case uint:
return float64(i), nil
case string:
f, err := strconv.ParseFloat(i, 64)
if err != nil {
return math.NaN(), err
}
return f, err
default:
return math.NaN(), fmt.Errorf("Cannot convert %s to float64", i)
}
}
// Returns milliseconds since epoch
func UnixMilli(t time.Time) int64 {
return t.UnixNano() / 1e6
}