mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-17 07:26:48 +00:00
* types/time, exchanges/kraken: Refactor spot/futures time types
- Updated WSFuturesTickerData, WsFuturesTradeData, and other related structs to replace int64 timestamps with the new types.Time.
- Adjusted related test cases to accommodate changes in timestamp handling.
- Modified functions in kraken_wrapper and kraken_websocket to utilise the new Time type for better time management.
- Enhanced JSON unmarshalling in the Time type to handle various timestamp formats, including "0" and "0.0".
* refactor: Update JSON field name/types and improve perf
* types/time: revert to more precise check, just check for 'n'
* refactor: Ryan the F1 driver is so back 🏎️
* refactor: Enhance UnmarshalJSON error handling and simplify test cases
* ocd: Fix trigger
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package types
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"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
|
|
|
|
var errInvalidTimestampFormat = errors.New("invalid timestamp format")
|
|
|
|
// UnmarshalJSON deserializes json, and timestamp information.
|
|
func (t *Time) UnmarshalJSON(data []byte) error {
|
|
s := string(data)
|
|
|
|
if s[0] == '"' {
|
|
s = s[1 : len(s)-1]
|
|
}
|
|
|
|
if s == "" || s[0] == 'n' || s == "0" {
|
|
return nil
|
|
}
|
|
|
|
if target := strings.Index(s, "."); target != -1 {
|
|
s = s[:target] + s[target+1:]
|
|
|
|
if strings.Trim(s, "0") == "" {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// Expects a string of length 10 (seconds), 13 (milliseconds), 16 (microseconds), or 19 (nanoseconds) representing a Unix timestamp
|
|
switch len(s) {
|
|
case 12, 15, 18:
|
|
s += "0"
|
|
case 11, 14, 17:
|
|
s += "00"
|
|
}
|
|
|
|
unixTS, err := strconv.ParseInt(s, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("error parsing unix timestamp: %w", err)
|
|
}
|
|
|
|
switch len(s) {
|
|
case 10:
|
|
*t = Time(time.Unix(unixTS, 0))
|
|
case 13:
|
|
*t = Time(time.UnixMilli(unixTS))
|
|
case 16:
|
|
*t = Time(time.UnixMicro(unixTS))
|
|
case 19:
|
|
*t = Time(time.Unix(0, unixTS))
|
|
default:
|
|
return fmt.Errorf("%w: %q", errInvalidTimestampFormat, 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()
|
|
}
|