mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
exchanges: Refactor time handling and other minor improvements (#1948)
* exchanges: Refactor time handling and other minor improvements - Updated Kraken wrapper to utilise new time handling methods. - Simplified Kucoin types by removing unnecessary structures and using direct JSON unmarshalling. - Improved websocket handling in Kucoin to directly parse candlestick data. - Modified Lbank types to use the new time representation. - Adjusted Poloniex wrapper and types to utilise the new time handling. - Updated Yobit types and wrapper to reflect changes in time representation. - Introduced DateTime type for better handling of specific time formats. - Added tests for DateTime unmarshalling to ensure correctness. - Rid UTC().Unix and UTC().UnixMilli as it's not needed - Correct Huobi timestamp usage for some endpoints. - Rid RFC3339 time parsing since Go does that automatically. * exchanges: Refactor JSON unmarshalling for various types and improve test coverage * linter: Update error message in TestGetKlines * refactor: Simplify JSON unmarshalling in MovementHistory and improve test assertions in GetKlines * refactor: Improve JSON unmarshalling for channel name and clarify comment in wsProcessOpenOrders * refactor: Update time handling in Huobi types to use types.Time for createdAt fields and relax GetLiquidationOrders test * refactor: Move wsTicker, wsSpread, wsTrades, and wsCandle types to kraken_types.go for better organistion * refactor: Add validation for underlying parameter in GetExpirationTime and update tests
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
@@ -49,21 +48,6 @@ func Int64FromString(raw any) (int64, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// TimeFromUnixTimestampFloat format
|
||||
func TimeFromUnixTimestampFloat(raw any) (time.Time, error) {
|
||||
ts, ok := raw.(float64)
|
||||
if !ok {
|
||||
return time.Time{}, fmt.Errorf("unable to parse, value not float64: %T", raw)
|
||||
}
|
||||
return time.UnixMilli(int64(ts)), nil
|
||||
}
|
||||
|
||||
// TimeFromUnixTimestampDecimal converts a unix timestamp in decimal form to a time.Time in UTC
|
||||
func TimeFromUnixTimestampDecimal(input float64) time.Time {
|
||||
i, f := math.Modf(input)
|
||||
return time.Unix(int64(i), int64(f*(1e9))).UTC()
|
||||
}
|
||||
|
||||
// BoolPtr takes in boolean condition and returns pointer version of it
|
||||
func BoolPtr(condition bool) *bool {
|
||||
b := condition
|
||||
|
||||
@@ -2,7 +2,6 @@ package convert
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -78,39 +77,6 @@ func TestInt64FromString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeFromUnixTimestampFloat(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTimestamp := float64(1414456320000)
|
||||
expectedOutput := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
|
||||
|
||||
actualOutput, err := TimeFromUnixTimestampFloat(testTimestamp)
|
||||
if actualOutput.UTC().String() != expectedOutput.UTC().String() || err != nil {
|
||||
t.Errorf("Common TimeFromUnixTimestampFloat. Expected '%v'. Actual '%v'. Error: %s",
|
||||
expectedOutput, actualOutput, err)
|
||||
}
|
||||
|
||||
testString := "Time"
|
||||
_, err = TimeFromUnixTimestampFloat(testString)
|
||||
if err == nil {
|
||||
t.Error("Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeFromUnixTimestampDecimal(t *testing.T) {
|
||||
for in, exp := range map[float64]time.Time{
|
||||
1590633982.5714: time.Date(2020, 5, 28, 2, 46, 22, 571400000, time.UTC),
|
||||
1560516023.070651: time.Date(2019, 6, 14, 12, 40, 23, 70651000, time.UTC),
|
||||
// Examples from Kraken
|
||||
1373750306.9819: time.Date(2013, 7, 13, 21, 18, 26, 981900000, time.UTC),
|
||||
1534614098.345543: time.Date(2018, 8, 18, 17, 41, 38, 345543000, time.UTC),
|
||||
} {
|
||||
got := TimeFromUnixTimestampDecimal(in)
|
||||
z, _ := got.Zone()
|
||||
assert.Equal(t, "UTC", z, "TimeFromUnixTimestampDecimal should return a UTC time")
|
||||
assert.WithinRangef(t, got, exp.Add(-time.Microsecond), exp.Add(time.Microsecond), "TimeFromUnixTimestampDecimal(%f) should parse a unix timestamp correctly", in)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolPtr(t *testing.T) {
|
||||
y := BoolPtr(true)
|
||||
if !*y {
|
||||
|
||||
Reference in New Issue
Block a user