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:
Adrian Gallagher
2025-07-01 09:11:55 +10:00
committed by GitHub
parent 48a66c9faa
commit 3cc9a2b9e0
92 changed files with 2488 additions and 3276 deletions

View File

@@ -6,6 +6,8 @@ import (
"strconv"
"strings"
"time"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
// Time represents a time.Time object that can be unmarshalled from a float64 or string.
@@ -76,3 +78,27 @@ func (t Time) String() string {
func (t Time) MarshalJSON() ([]byte, error) {
return t.Time().MarshalJSON()
}
// DateTime represents a time.Time object that can be unmarshalled from a string in the format "2006-01-02 15:04:05".
type DateTime time.Time
// UnmarshalJSON unmarshals json data into a DateTime type.
func (d *DateTime) UnmarshalJSON(data []byte) error {
var ts string
if err := json.Unmarshal(data, &ts); err != nil {
return fmt.Errorf("error unmarshalling %q into string: %w", data, err)
}
tm, err := time.Parse(time.DateTime, ts)
if err != nil {
return fmt.Errorf("error parsing %q into DateTime: %w", ts, err)
}
*d = DateTime(tm)
return nil
}
// Time converts DateTime to time.Time
func (d DateTime) Time() time.Time {
return time.Time(d)
}

View File

@@ -76,3 +76,16 @@ func TestTime_MarshalJSON(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, `"0001-01-01T00:00:00Z"`, string(data))
}
func TestDateTimeUnmarshalJSON(t *testing.T) {
t.Parallel()
var (
testTime DateTime
jsonError *json.UnmarshalTypeError
parseError *time.ParseError
)
require.ErrorAs(t, json.Unmarshal([]byte(`69`), &testTime), &jsonError)
require.ErrorAs(t, json.Unmarshal([]byte(`"2025"`), &testTime), &parseError)
require.NoError(t, json.Unmarshal([]byte(`"2018-08-20 19:20:46"`), &testTime))
assert.Equal(t, time.Date(2018, 8, 20, 19, 20, 46, 0, time.UTC), testTime.Time())
}