mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-05 23:16:53 +00:00
types: Add Time type from Gateio and share across codebase (#1648)
* consolidate type to types package and share across code base * rm convert type and convert codebase * rm irrelavant test cases * Fix tests * glorious nits * Update exchanges/gateio/gateio_types.go Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> * thrasher: nits --------- Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
package gateio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
zero = []byte(`0`)
|
||||
emptyStr = []byte(`""`)
|
||||
zeroStr = []byte(`"0"`)
|
||||
)
|
||||
|
||||
// Time represents a time.Time object that can be unmarshalled from a float64 or string.
|
||||
type Time time.Time
|
||||
|
||||
// UnmarshalJSON deserializes json, and timestamp information.
|
||||
func (a *Time) UnmarshalJSON(data []byte) error {
|
||||
if bytes.Equal(data, zero) || bytes.Equal(data, emptyStr) || bytes.Equal(data, zeroStr) {
|
||||
*a = Time(time.Time{})
|
||||
return nil
|
||||
}
|
||||
|
||||
s := string(data)
|
||||
if s[0] == '"' {
|
||||
s = s[1 : len(s)-1]
|
||||
}
|
||||
|
||||
target := strings.Index(s, ".")
|
||||
if target != -1 {
|
||||
s = s[:target] + s[target+1:]
|
||||
}
|
||||
|
||||
standard, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch len(s) {
|
||||
case 10:
|
||||
// Seconds
|
||||
*a = Time(time.Unix(standard, 0))
|
||||
case 11, 12:
|
||||
// Milliseconds: 1726104395.5 && 1726104395.56
|
||||
*a = Time(time.UnixMilli(standard * int64(math.Pow10(13-len(s)))))
|
||||
case 13:
|
||||
// Milliseconds
|
||||
*a = Time(time.UnixMilli(standard))
|
||||
case 14:
|
||||
// MicroSeconds: 1726106210903.0
|
||||
*a = Time(time.UnixMicro(standard * 100))
|
||||
case 16:
|
||||
// MicroSeconds
|
||||
*a = Time(time.UnixMicro(standard))
|
||||
case 17:
|
||||
// NanoSeconds: 1606292218213.4578
|
||||
*a = Time(time.Unix(0, standard*100))
|
||||
case 19:
|
||||
// NanoSeconds
|
||||
*a = Time(time.Unix(0, standard))
|
||||
default:
|
||||
return fmt.Errorf("cannot unmarshal %s into Time", string(data))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Time represents a time instance.
|
||||
func (a Time) Time() time.Time { return time.Time(a) }
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
||||
testsubs "github.com/thrasher-corp/gocryptotrader/internal/testing/subscriptions"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
"github.com/thrasher-corp/gocryptotrader/types"
|
||||
)
|
||||
|
||||
// Please supply your own APIKEYS here for due diligence testing
|
||||
@@ -3192,7 +3193,7 @@ func TestParseGateioMilliSecTimeUnmarshal(t *testing.T) {
|
||||
float64JSON := `{"number": 1684981731.098}`
|
||||
|
||||
time := time.UnixMilli(timeWhenTesting)
|
||||
var in Time
|
||||
var in types.Time
|
||||
err := json.Unmarshal([]byte(timeWhenTestingString), &in)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -3201,7 +3202,7 @@ func TestParseGateioMilliSecTimeUnmarshal(t *testing.T) {
|
||||
t.Fatalf("found %v, but expected %v", in.Time(), time)
|
||||
}
|
||||
inInteger := struct {
|
||||
Number Time `json:"number"`
|
||||
Number types.Time `json:"number"`
|
||||
}{}
|
||||
err = json.Unmarshal([]byte(integerJSON), &inInteger)
|
||||
if err != nil {
|
||||
@@ -3212,7 +3213,7 @@ func TestParseGateioMilliSecTimeUnmarshal(t *testing.T) {
|
||||
}
|
||||
|
||||
inFloat64 := struct {
|
||||
Number Time `json:"number"`
|
||||
Number types.Time `json:"number"`
|
||||
}{}
|
||||
err = json.Unmarshal([]byte(float64JSON), &inFloat64)
|
||||
if err != nil {
|
||||
@@ -3232,7 +3233,7 @@ func TestParseTimeUnmarshal(t *testing.T) {
|
||||
timeWhenTestingStringMicroSecond := `"1691122380942.173000"`
|
||||
|
||||
whenTime := time.Unix(timeWhenTesting, 0)
|
||||
var in Time
|
||||
var in types.Time
|
||||
err := json.Unmarshal([]byte(timeWhenTestingString), &in)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -3241,7 +3242,7 @@ func TestParseTimeUnmarshal(t *testing.T) {
|
||||
t.Fatalf("found %v, but expected %v", in.Time(), whenTime)
|
||||
}
|
||||
inInteger := struct {
|
||||
Number Time `json:"number"`
|
||||
Number types.Time `json:"number"`
|
||||
}{}
|
||||
err = json.Unmarshal([]byte(integerJSON), &inInteger)
|
||||
if err != nil {
|
||||
@@ -3252,7 +3253,7 @@ func TestParseTimeUnmarshal(t *testing.T) {
|
||||
}
|
||||
|
||||
inFloat64 := struct {
|
||||
Number Time `json:"number"`
|
||||
Number types.Time `json:"number"`
|
||||
}{}
|
||||
err = json.Unmarshal([]byte(float64JSON), &inFloat64)
|
||||
if err != nil {
|
||||
@@ -3263,7 +3264,7 @@ func TestParseTimeUnmarshal(t *testing.T) {
|
||||
t.Fatalf("found %v, but expected %v", inFloat64.Number.Time(), msTime)
|
||||
}
|
||||
|
||||
var microSeconds Time
|
||||
var microSeconds types.Time
|
||||
err = json.Unmarshal([]byte(timeWhenTestingStringMicroSecond), µSeconds)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -3656,57 +3657,3 @@ func TestGenerateWebsocketMessageID(t *testing.T) {
|
||||
t.Parallel()
|
||||
require.NotEmpty(t, g.GenerateWebsocketMessageID(false))
|
||||
}
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
var testTime Time
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`0`), &testTime))
|
||||
assert.Equal(t, time.Time{}, testTime.Time())
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`""`), &testTime))
|
||||
assert.Equal(t, time.Time{}, testTime.Time())
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`"0"`), &testTime))
|
||||
assert.Equal(t, time.Time{}, testTime.Time())
|
||||
|
||||
// seconds
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1628736847"`), &testTime))
|
||||
assert.Equal(t, time.Unix(1628736847, 0), testTime.Time())
|
||||
|
||||
// milliseconds
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1726104395.5"`), &testTime))
|
||||
assert.Equal(t, time.UnixMilli(1726104395500), testTime.Time())
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1726104395.56"`), &testTime))
|
||||
assert.Equal(t, time.UnixMilli(1726104395560), testTime.Time())
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1628736847325"`), &testTime))
|
||||
assert.Equal(t, time.UnixMilli(1628736847325), testTime.Time())
|
||||
|
||||
// microseconds
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1628736847325123"`), &testTime))
|
||||
assert.Equal(t, time.UnixMicro(1628736847325123), testTime.Time())
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1726106210903.0"`), &testTime))
|
||||
assert.Equal(t, time.UnixMicro(1726106210903000), testTime.Time())
|
||||
|
||||
// nanoseconds
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1606292218213.4578"`), &testTime))
|
||||
assert.Equal(t, time.Unix(0, 1606292218213457800), testTime.Time())
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`"1606292218213457800"`), &testTime))
|
||||
assert.Equal(t, time.Unix(0, 1606292218213457800), testTime.Time())
|
||||
}
|
||||
|
||||
// 5046307 216.0 ns/op 168 B/op 2 allocs/op (current)
|
||||
// 2716176 441.9 ns/op 352 B/op 6 allocs/op (previous)
|
||||
func BenchmarkTime(b *testing.B) {
|
||||
var testTime Time
|
||||
for i := 0; i < b.N; i++ {
|
||||
err := json.Unmarshal([]byte(`"1691122380942.173000"`), &testTime)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user