-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.go
74 lines (65 loc) · 1.85 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
62
63
64
65
66
67
68
69
70
71
72
73
74
package binance
import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
)
func floatFromString(raw interface{}) (float64, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf(fmt.Sprintf("unable to parse, value not string: %T", raw))
}
flt, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, warpError(err, fmt.Sprintf("unable to parse as float: %s", str))
}
return flt, nil
}
func intFromString(raw interface{}) (int, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf(fmt.Sprintf("unable to parse, value not string: %T", raw))
}
n, err := strconv.Atoi(str)
if err != nil {
return 0, warpError(err, fmt.Sprintf("unable to parse as int: %s", str))
}
return n, nil
}
func timeFromUnixTimestampString(raw interface{}) (time.Time, error) {
str, ok := raw.(string)
if !ok {
return time.Time{}, fmt.Errorf(fmt.Sprintf("unable to parse, value not string"))
}
ts, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return time.Time{}, warpError(err, fmt.Sprintf("unable to parse as int: %s", str))
}
return time.Unix(0, ts*int64(time.Millisecond)), nil
}
func timeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
ts, ok := raw.(float64)
if !ok {
return time.Time{}, fmt.Errorf(fmt.Sprintf("unable to parse, value not int64: %T", raw))
}
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
}
func unixMillis(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}
func recvWindow(d time.Duration) int64 {
return int64(d) / int64(time.Millisecond)
}
func (as *apiService) handleError(textRes []byte) error {
err := &Error{}
log.Println("errorResponse:", string(textRes))
if err := json.Unmarshal(textRes, err); err != nil {
return warpError(err, "error unmarshal failed")
}
return err
}
func warpError(err error, msg string) error {
return fmt.Errorf(err.Error() + msg)
}