mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 15:10:49 +00:00
Split common package more and QA
This commit is contained in:
@@ -53,3 +53,27 @@ func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
|
||||
}
|
||||
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
|
||||
}
|
||||
|
||||
// UnixTimestampToTime returns time.time
|
||||
func UnixTimestampToTime(timeint64 int64) time.Time {
|
||||
return time.Unix(timeint64, 0)
|
||||
}
|
||||
|
||||
// UnixTimestampStrToTime returns a time.time and an error
|
||||
func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
|
||||
i, err := strconv.ParseInt(timeStr, 10, 64)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
return time.Unix(i, 0), nil
|
||||
}
|
||||
|
||||
// UnixMillis converts a UnixNano timestamp to milliseconds
|
||||
func UnixMillis(t time.Time) int64 {
|
||||
return t.UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
// RecvWindow converts a supplied time.Duration to milliseconds
|
||||
func RecvWindow(d time.Duration) int64 {
|
||||
return int64(d) / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
@@ -94,3 +94,58 @@ func TestTimeFromUnixTimestampFloat(t *testing.T) {
|
||||
t.Error("Test failed. Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixTimestampToTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := int64(1489439831)
|
||||
tm := time.Unix(testTime, 0)
|
||||
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
|
||||
actualResult := UnixTimestampToTime(testTime)
|
||||
if tm.String() != actualResult.String() {
|
||||
t.Errorf(
|
||||
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixTimestampStrToTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := "1489439831"
|
||||
incorrectTime := "DINGDONG"
|
||||
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
|
||||
actualResult, err := UnixTimestampStrToTime(testTime)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if actualResult.UTC().String() != expectedOutput {
|
||||
t.Errorf(
|
||||
"Test failed. Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
|
||||
}
|
||||
actualResult, err = UnixTimestampStrToTime(incorrectTime)
|
||||
if err == nil {
|
||||
t.Error("Test failed. Common UnixTimestampStrToTime error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnixMillis(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
|
||||
expectedOutput := int64(1414456320000)
|
||||
|
||||
actualOutput := UnixMillis(testTime)
|
||||
if actualOutput != expectedOutput {
|
||||
t.Errorf("Test failed. Common UnixMillis. Expected '%d'. Actual '%d'.",
|
||||
expectedOutput, actualOutput)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecvWindow(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTime := time.Duration(24760000)
|
||||
expectedOutput := int64(24)
|
||||
|
||||
actualOutput := RecvWindow(testTime)
|
||||
if actualOutput != expectedOutput {
|
||||
t.Errorf("Test failed. Common RecvWindow. Expected '%d'. Actual '%d'",
|
||||
expectedOutput, actualOutput)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user