forked from gofinance/ib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wire.go
242 lines (207 loc) · 6.28 KB
/
wire.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package ib
import (
"bufio"
"bytes"
"fmt"
"math"
"strconv"
"strings"
"time"
)
type timeFmt int
const (
timeWriteUTC timeFmt = iota // write datetime as UTC converted time string with explicit UTC TZ designator
timeWriteLocalTime // write datetime as local time without an explicit TZ designator
timeReadAutoDetect // read datetime (or date) by auto-detecting likely format (first checks epoch)
timeReadEpoch // read datetime as epoch in seconds since 1/1/70 UTC
timeReadLocalDateTime // read datetime which is in local time (any TZ designator is ignored)
timeReadLocalDate // read date which is in local time and does not have any timezone designator
timeReadLocalTime // read time which is in local time and does not have any timezone designator
)
type readable interface {
read(b *bufio.Reader) error
}
type writable interface {
write(buf *bytes.Buffer) error
}
func readString(b *bufio.Reader) (s string, err error) {
if s, err = b.ReadString(0); err != nil {
return
}
return string(s[:len(s)-1]), nil
}
// readStringList reads an IB delimited separated string of strings into a Go slice.
func readStringList(b *bufio.Reader, sep string) (r []string, err error) {
s, err := readString(b)
if err != nil {
return
}
return strings.Split(s, sep), nil
}
func readInt(b *bufio.Reader) (int64, error) {
str, err := readString(b)
if err != nil {
return -1, err
}
if str == "" {
return math.MaxInt64, nil
}
i, err := strconv.ParseInt(str, 10, 64)
return i, err
}
// readIntList reads an IB pipe-separated string of integers into a Go slice.
func readIntList(b *bufio.Reader) ([]int, error) {
s, err := readString(b)
if err != nil {
return nil, err
}
split := strings.Split(s, "|")
r := make([]int, len(split))
for i, val := range split {
r[i], err = strconv.Atoi(val)
if err != nil {
return nil, err
}
}
return r, nil
}
func readFloat(b *bufio.Reader) (float64, error) {
str, err := readString(b)
if err != nil {
return -1., err
}
if str == "" {
return math.MaxFloat64, nil
}
f, err := strconv.ParseFloat(str, 64)
return f, err
}
// readBool is equivalent of IB API EReader.readBoolFromInt.
func readBool(b *bufio.Reader) (bool, error) {
i, err := readInt(b)
if err != nil {
return false, err
}
return (i > 0), nil
}
// readTime reads a string and then parses it according to the given time format.
// Returned times are always in the local timezone (convert with time.UTC()).
func readTime(b *bufio.Reader, f timeFmt) (t time.Time, err error) {
var timeString string
if timeString, err = readString(b); err != nil {
return
}
if f == timeReadAutoDetect {
f, err = detectTime(timeString)
if err != nil {
return
}
}
if f == timeReadEpoch {
var epochSecs int64
if epochSecs, err = strconv.ParseInt(timeString, 10, 64); err != nil {
return
}
return time.Unix(epochSecs, 0), nil
}
if f == timeReadLocalDateTime {
format := "20060102 15:04:05"
if len(timeString) < len(format) {
return time.Now(), fmt.Errorf("ibgo: '%s' too short to be datetime format '%s'", timeString, format)
}
// Truncate any portion this parse does not require (ie timezones)
fields := strings.Fields(timeString)
if len(fields) < 2 {
return time.Now(), fmt.Errorf("ibgo: '%s' does not contain expected whitespace for datetime format '%s'", timeString, format)
}
timeString = fields[0] + " " + fields[1]
return time.ParseInLocation(format, timeString, time.Local)
}
if f == timeReadLocalDate {
format := "20060102"
if len(timeString) != len(format) {
return time.Now(), fmt.Errorf("ibgo: '%s' wrong length to be datetime format '%s'", timeString, format)
}
return time.ParseInLocation(format, timeString, time.Local)
}
if f == timeReadLocalTime {
formatShort := "15:04"
formatLong := "15:04:05"
switch len(timeString) {
case len(formatShort):
return time.ParseInLocation(formatShort, timeString, time.Local)
case len(formatLong):
return time.ParseInLocation(formatLong, timeString, time.Local)
default:
return time.Now(), fmt.Errorf("ibgo: '%s' wrong length to be time format '%s' or '%s'", timeString, formatShort, formatLong)
}
}
return time.Now(), fmt.Errorf("ibgo: unsupported read time format '%v'", f)
}
func writeString(b *bytes.Buffer, s string) error {
_, err := b.WriteString(s + "\000")
return err
}
func writeInt(b *bytes.Buffer, i int64) error {
return writeString(b, strconv.FormatInt(i, 10))
}
func writeFloat(b *bytes.Buffer, f float64) error {
return writeString(b, strconv.FormatFloat(f, 'g', 10, 64))
}
// TODO: this never errors. Is it expected?
func writeMaxFloat(b *bytes.Buffer, f float64) error {
if f >= math.MaxFloat64 {
b.WriteByte('\000')
} else {
writeFloat(b, f)
}
return nil
}
// TODO: this never errors. Is it expected?
func writeMaxInt(b *bytes.Buffer, i int64) error {
if i == math.MaxInt64 {
b.WriteByte('\000')
} else {
writeInt(b, i)
}
return nil
}
func writeBool(b *bytes.Buffer, bo bool) error {
s := "0"
if bo {
s = "1"
}
return writeString(b, s)
}
func writeTime(b *bytes.Buffer, t time.Time, f timeFmt) error {
switch f {
case timeWriteUTC:
return writeString(b, t.UTC().Format("20060102 15:04:05")+" UTC")
case timeWriteLocalTime:
return writeString(b, t.Format("20060102 15:04:05"))
}
return fmt.Errorf("goib: cannot write time format '%v'", f)
}
func detectTime(timeString string) (timeFmt, error) {
if len(timeString) == len("14:52") && strings.Contains(timeString, ":") {
return timeReadLocalTime, nil
}
if len(timeString) == len("14:52:02") && strings.Contains(timeString, ":") {
return timeReadLocalTime, nil
}
if len(timeString) == len("20060102 15:04:05") && strings.Contains(timeString, ":") {
return timeReadLocalDateTime, nil
}
if len(timeString) >= len("20060102 15:04:05 ") && strings.Contains(timeString, ":") {
return timeReadLocalDateTime, nil
}
// 8 character time strings are ambiguous as they can be a yyyymmdd date
// or an epoch. So try yyyymmdd first, as 8 char epochs are less likely
if len(timeString) == len("20060102") {
return timeReadLocalDate, nil
}
if _, err := strconv.ParseInt(timeString, 10, 64); err == nil {
return timeReadEpoch, nil
}
return -1, fmt.Errorf("ibgo: '%s' has unknown time format", timeString)
}