mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-04 15:10:54 +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:
79
types/time.go
Normal file
79
types/time.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Time represents a time.Time object that can be unmarshalled from a float64 or string.
|
||||
// MarshalJSON serializes the time to JSON using RFC 3339 format.
|
||||
// Note: Not all exchanges may support RFC 3339 for outbound requests, so ensure compatibility with each exchange's time
|
||||
// format requirements.
|
||||
type Time time.Time
|
||||
|
||||
// UnmarshalJSON deserializes json, and timestamp information.
|
||||
func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
s := string(data)
|
||||
|
||||
switch s {
|
||||
case "null", "0", `""`, `"0"`:
|
||||
*t = Time(time.Time{})
|
||||
return nil
|
||||
}
|
||||
|
||||
if s[0] == '"' {
|
||||
s = s[1 : len(s)-1]
|
||||
}
|
||||
|
||||
if target := strings.Index(s, "."); 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
|
||||
*t = Time(time.Unix(standard, 0))
|
||||
case 11, 12:
|
||||
// Milliseconds: 1726104395.5 && 1726104395.56
|
||||
*t = Time(time.UnixMilli(standard * int64(math.Pow10(13-len(s)))))
|
||||
case 13:
|
||||
// Milliseconds
|
||||
*t = Time(time.UnixMilli(standard))
|
||||
case 14:
|
||||
// MicroSeconds: 1726106210903.0
|
||||
*t = Time(time.UnixMicro(standard * 100))
|
||||
case 16:
|
||||
// MicroSeconds
|
||||
*t = Time(time.UnixMicro(standard))
|
||||
case 17:
|
||||
// NanoSeconds: 1606292218213.4578
|
||||
*t = Time(time.Unix(0, standard*100))
|
||||
case 19:
|
||||
// NanoSeconds
|
||||
*t = Time(time.Unix(0, standard))
|
||||
default:
|
||||
return fmt.Errorf("cannot unmarshal %s into Time", string(data))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Time represents a time instance.
|
||||
func (t Time) Time() time.Time { return time.Time(t) }
|
||||
|
||||
// String returns a string representation of the time.
|
||||
func (t Time) String() string {
|
||||
return t.Time().String()
|
||||
}
|
||||
|
||||
// MarshalJSON serializes the time to json.
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
return t.Time().MarshalJSON()
|
||||
}
|
||||
86
types/time_test.go
Normal file
86
types/time_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUnmarshalJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
var testTime Time
|
||||
|
||||
require.NoError(t, json.Unmarshal([]byte(`null`), &testTime))
|
||||
assert.Equal(t, time.Time{}, 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())
|
||||
|
||||
require.ErrorIs(t, json.Unmarshal([]byte(`"blurp"`), &testTime), strconv.ErrSyntax)
|
||||
require.Error(t, json.Unmarshal([]byte(`"123456"`), &testTime))
|
||||
}
|
||||
|
||||
// 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 BenchmarkUnmarshalJSON(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := Time(time.Time{})
|
||||
assert.Equal(t, time.Time{}, testTime.Time())
|
||||
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", testTime.String())
|
||||
}
|
||||
|
||||
func TestTime_MarshalJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := Time(time.Time{})
|
||||
data, err := testTime.MarshalJSON()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `"0001-01-01T00:00:00Z"`, string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user