Files
gocryptotrader/exchanges/gateio/gateio_convert.go
Ryan O'Hara-Reid 43a8044b44 gateio: websocket updates (#1282)
* gateio: websocket updates

* Update exchanges/gateio/gateio_websocket.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* glorious: nits

* revert that trick

* glorious:nits

* Update exchanges/gateio/gateio_ws_futures.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_ws_futures.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_websocket.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_websocket.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_ws_option.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_ws_option.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_websocket.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_websocket.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* thrasher-nits

* Update exchanges/gateio/gateio_ws_futures.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/gateio/gateio_ws_futures.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* thrasher: nits rides again chapter 2 volume 1

* rm unmarshaljson method for orderbook

* use gateio time where it can and update tests

* math.trunc and lower time frame on big books

* :eagle

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
2023-08-04 17:39:38 +10:00

84 lines
1.8 KiB
Go

package gateio
import (
"encoding/json"
"fmt"
"math"
"strconv"
"time"
)
type gateioTime time.Time
// UnmarshalJSON deserializes json, and timestamp information.
func (a *gateioTime) UnmarshalJSON(data []byte) error {
var value interface{}
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
var standard int64
switch val := value.(type) {
case float64:
if math.Trunc(val) != val {
standard = int64(val * 1e3) // Account for 1684981731.098
} else {
standard = int64(val)
}
case string:
if val == "" {
return nil
}
parsedValue, err := strconv.ParseFloat(val, 64)
if err != nil {
return err
}
if math.Trunc(parsedValue) != parsedValue {
*a = gateioTime(time.UnixMicro(int64(parsedValue * 1e3))) // Account for "1691122380942.173000" microseconds
return nil
}
standard = int64(parsedValue)
default:
return fmt.Errorf("cannot unmarshal %T into gateioTime", val)
}
if standard > 9999999999 {
*a = gateioTime(time.UnixMilli(standard))
} else {
*a = gateioTime(time.Unix(standard, 0))
}
return nil
}
// Time represents a time instance.
func (a gateioTime) Time() time.Time { return time.Time(a) }
type gateioNumericalValue float64
// UnmarshalJSON is custom type json unmarshaller for gateioNumericalValue
func (a *gateioNumericalValue) UnmarshalJSON(data []byte) error {
var num interface{}
err := json.Unmarshal(data, &num)
if err != nil {
return err
}
switch d := num.(type) {
case float64:
*a = gateioNumericalValue(d)
case string:
if d == "" {
*a = gateioNumericalValue(0)
return nil
}
convNum, err := strconv.ParseFloat(d, 64)
if err != nil {
return err
}
*a = gateioNumericalValue(convNum)
}
return nil
}
// Float64 returns float64 value from gateioNumericalValue instance.
func (a gateioNumericalValue) Float64() float64 { return float64(a) }