Files
gocryptotrader/exchanges/gateio/gateio_convert.go
Gareth Kirwan 2b3c63c5b3 Exchanges: Convert bespoke Number types to types.Number (#1429)
* Kucoin: Rename WsSpotTicker => WsSnapshot

* Kucoin: Replace kucoinNumber => types.Number

* Okcoin: Convert to types.Number

* Gateio: Convert to types.Number

* Okx: Convert to types.Number
2023-12-28 14:55:23 +11:00

54 lines
1.1 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) }