forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
73 lines (64 loc) · 1.8 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package common
import (
"encoding/json"
"fmt"
"os"
"protocol"
"strconv"
"strings"
"time"
"unicode"
)
// Returns the parsed duration in nanoseconds, support 'u', 's', 'm',
// 'h', 'd' and 'w' suffixes.
func ParseTimeDuration(value string) (int64, error) {
// shortcut for nanoseconds
if idx := strings.IndexFunc(value, func(r rune) bool { return !unicode.IsNumber(r) }); idx == -1 {
return strconv.ParseInt(value, 10, 64)
}
parsedFloat, err := strconv.ParseFloat(value[:len(value)-1], 64)
if err != nil {
return 0, err
}
switch value[len(value)-1] {
case 'u':
return int64(parsedFloat * float64(time.Microsecond)), nil
case 's':
return int64(parsedFloat * float64(time.Second)), nil
case 'm':
return int64(parsedFloat * float64(time.Minute)), nil
case 'h':
return int64(parsedFloat * float64(time.Hour)), nil
case 'd':
return int64(parsedFloat * 24 * float64(time.Hour)), nil
case 'w':
return int64(parsedFloat * 7 * 24 * float64(time.Hour)), nil
case 'y':
return int64(parsedFloat * 365 * 24 * float64(time.Hour)), nil
}
lastChar := value[len(value)-1]
if !unicode.IsDigit(rune(lastChar)) && lastChar != '.' {
return 0, fmt.Errorf("Invalid character '%c'", lastChar)
}
if value[len(value)-2] != '.' {
extraDigit := float64(lastChar - '0')
parsedFloat = parsedFloat*10 + extraDigit
}
return int64(parsedFloat), nil
}
func GetFileSize(path string) (int64, error) {
info, err := os.Stat(path)
if err != nil {
return 0, err
}
return info.Size(), nil
}
func StringToSeriesArray(seriesString string, args ...interface{}) ([]*protocol.Series, error) {
seriesString = fmt.Sprintf(seriesString, args...)
series := []*protocol.Series{}
err := json.Unmarshal([]byte(seriesString), &series)
return series, err
}
func CurrentTime() int64 {
return time.Now().UnixNano() / int64(1000)
}