mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 15:10:49 +00:00
Miscellaneous bug fixes (#513)
* Various bug fixes * Deadlink, cleanup plus bug fixes * Various Kraken fixes * Add convert func for decimal unix timestamps * Convert all test times to UTC * Kraken: Make assets a pointer to prevent excessive copying * Docker slash fix * Address nits plus bump ITBit last checked pairs timestamp * Set pairs to enabled pairs when getting active orders * Use asset translator for UpdateAccountInfo and more checks for the exchange template tool * Address MadCozBadd's nits * Make exchange var 2 chars * Make program more user friendly * Project wide comment checks and exchName check * Fix Huobi indexing bug and use correct pair formatting * Address nits + readme change
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -57,6 +55,13 @@ func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
|
||||
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
|
||||
}
|
||||
|
||||
// TimeFromUnixTimestampDecimal converts a unix timestamp in decimal form to
|
||||
// a time.Time
|
||||
func TimeFromUnixTimestampDecimal(input float64) time.Time {
|
||||
i, f := math.Modf(input)
|
||||
return time.Unix(int64(i), int64(f*(1e9)))
|
||||
}
|
||||
|
||||
// UnixTimestampToTime returns time.time
|
||||
func UnixTimestampToTime(timeint64 int64) time.Time {
|
||||
return time.Unix(timeint64, 0)
|
||||
@@ -81,33 +86,6 @@ func RecvWindow(d time.Duration) int64 {
|
||||
return int64(d) / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
// SplitFloatDecimals takes in a float64 and splits
|
||||
// the decimals into their own integers
|
||||
// Warning. Passing in numbers with many decimals
|
||||
// can lead to a loss of accuracy
|
||||
func SplitFloatDecimals(input float64) (baseNum, decimalNum int64, err error) {
|
||||
if input > float64(math.MaxInt64) {
|
||||
return 0, 0, errors.New("number too large to split into integers")
|
||||
}
|
||||
if input == float64(int64(input)) {
|
||||
return int64(input), 0, nil
|
||||
}
|
||||
decStr := strconv.FormatFloat(input, 'f', -1, 64)
|
||||
splitNum := strings.Split(decStr, ".")
|
||||
baseNum, err = strconv.ParseInt(splitNum[0], 10, 64)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
decimalNum, err = strconv.ParseInt(splitNum[1], 10, 64)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if baseNum < 0 {
|
||||
decimalNum *= -1
|
||||
}
|
||||
return baseNum, decimalNum, nil
|
||||
}
|
||||
|
||||
// BoolPtr takes in boolen condition and returns pointer version of it
|
||||
func BoolPtr(condition bool) *bool {
|
||||
b := condition
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -96,6 +95,22 @@ func TestTimeFromUnixTimestampFloat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeFromUnixTimestampDecimal(t *testing.T) {
|
||||
r := TimeFromUnixTimestampDecimal(1590633982.5714)
|
||||
if r.Year() != 2020 ||
|
||||
r.Month().String() != "May" ||
|
||||
r.Day() != 28 {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
|
||||
r = TimeFromUnixTimestampDecimal(1560516023.070651)
|
||||
if r.Year() != 2019 ||
|
||||
r.Month().String() != "June" ||
|
||||
r.Day() != 14 {
|
||||
t.Error("unexpected result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixTimestampToTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := int64(1489439831)
|
||||
@@ -151,61 +166,6 @@ func TestRecvWindow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSplitFloatDecimals ensures SplitFloatDecimals
|
||||
// accurately splits decimals into integers
|
||||
func TestSplitFloatDecimals(t *testing.T) {
|
||||
x, y, err := SplitFloatDecimals(1.2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if x != 1 && y != 2 {
|
||||
t.Error("Conversion error")
|
||||
}
|
||||
x, y, err = SplitFloatDecimals(123456.654321)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if x != 123456 && y != 654321 {
|
||||
t.Error("Conversion error")
|
||||
}
|
||||
x, y, err = SplitFloatDecimals(123.111000)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if x != 123 && y != 111 {
|
||||
t.Error("Conversion error")
|
||||
}
|
||||
x, y, err = SplitFloatDecimals(0123.111001)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if x != 123 && y != 111001 {
|
||||
t.Error("Conversion error")
|
||||
}
|
||||
x, y, err = SplitFloatDecimals(1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if x != 1 && y != 0 {
|
||||
t.Error("Conversion error")
|
||||
}
|
||||
_, _, err = SplitFloatDecimals(float64(math.MaxInt64) + 1)
|
||||
if err == nil {
|
||||
t.Error("Expected conversion error")
|
||||
}
|
||||
_, _, err = SplitFloatDecimals(1797693134862315700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111)
|
||||
if err == nil {
|
||||
t.Error("Expected conversion error")
|
||||
}
|
||||
x, y, err = SplitFloatDecimals(-1.2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if x != -1 && y != -2 {
|
||||
t.Error("Conversion error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolPtr(t *testing.T) {
|
||||
y := BoolPtr(true)
|
||||
if !*y {
|
||||
|
||||
Reference in New Issue
Block a user