-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtime_test.go
109 lines (92 loc) · 2.55 KB
/
time_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tradier
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestDateTime_Set(t *testing.T) {
t.Run("Parse 2006-01-02T15:04:05", func(t *testing.T) {
input := "2006-01-02T15:04:05"
value := DateTime{}
err := value.Set(input)
assert.NoError(t, err)
assert.Equal(t, value.Unix(), int64(1136214245))
})
t.Run("Parse 2006-01-02", func(t *testing.T) {
input := "2006-01-02"
value := DateTime{}
err := value.Set(input)
assert.NoError(t, err)
assert.Equal(t, value.Unix(), int64(1136160000))
})
t.Run("Parse 15:04", func(t *testing.T) {
input := "15:04"
value := DateTime{}
err := value.Set(input)
assert.NoError(t, err)
assert.Equal(t, value.Unix(), int64(-62167164960))
})
t.Run("Parse seconds since epoc", func(t *testing.T) {
input := time.Now()
ms := fmt.Sprintf("%v%v", input.Unix(), input.Nanosecond()/1000000)
value := DateTime{}
err := value.Set(ms)
assert.NoError(t, err)
assert.Equal(t, value.Unix(), input.Unix())
})
t.Run("Invalid time", func(t *testing.T) {
input := "not a time"
value := DateTime{}
err := value.Set(input)
assert.Error(t, err)
})
}
func TestDateTime_UnmarshalJSON(t *testing.T) {
t.Run("Parse 2006-01-02T15:04:05", func(t *testing.T) {
input := "2006-01-02T15:04:05"
value := DateTime{}
err := value.UnmarshalJSON([]byte(input))
assert.NoError(t, err)
assert.Equal(t, value.Unix(), int64(1136214245))
})
t.Run("Parse 2006-01-02", func(t *testing.T) {
input := "2006-01-02"
value := DateTime{}
err := value.UnmarshalJSON([]byte(input))
assert.NoError(t, err)
assert.Equal(t, value.Unix(), int64(1136160000))
})
t.Run("Parse 15:04", func(t *testing.T) {
input := "15:04"
value := DateTime{}
err := value.UnmarshalJSON([]byte(input))
assert.NoError(t, err)
assert.Equal(t, value.Unix(), int64(-62167164960))
})
t.Run("Parse seconds since epoc", func(t *testing.T) {
input := time.Now()
ms := fmt.Sprintf("%v000", input.Unix())
value := DateTime{}
err := value.UnmarshalJSON([]byte(ms))
assert.NoError(t, err)
assert.Equal(t, value.Unix(), input.Unix())
})
t.Run("Invalid time", func(t *testing.T) {
input := "not a time"
value := DateTime{}
err := value.UnmarshalJSON([]byte(input))
assert.Error(t, err)
})
}
func TestParseTimeMs(t *testing.T) {
t.Run("Invalid ms", func(t *testing.T) {
_, err := ParseTimeMs("not a number")
assert.Error(t, err)
})
t.Run("Invalid ms", func(t *testing.T) {
output, err := ParseTimeMs("123456")
assert.NoError(t, err)
assert.Equal(t, output.Nanosecond(), 456000000)
})
}