-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_formats_test.go
90 lines (66 loc) · 2.59 KB
/
time_formats_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
package opensearchutil
import (
"encoding/json"
"testing"
"time"
. "github.com/onsi/gomega"
)
func TestTimeBasicDateTime_MarshalText(t *testing.T) {
g := NewGomegaWithT(t)
customTime := TimeBasicDateTime(time.Date(2019, 3, 23, 21, 34, 46, 567000000, time.UTC))
res, err := customTime.MarshalText()
g.Expect(err).To(BeNil())
g.Expect(string(res)).To(Equal("20190323T213446.567+00:00"))
}
func TestTimeBasicDateTime_UnmarshalText(t *testing.T) {
g := NewGomegaWithT(t)
var obj TimeBasicDateTime
g.Expect(obj.UnmarshalText([]byte("20190323T213446.567+00:00"))).To(Succeed())
g.Expect(time.Time(obj).Equal(time.Date(2019, 3, 23, 21, 34, 46, 567000000, time.UTC))).To(BeTrue())
}
func TestTimeBasicDateTimeNoMillis_MarshalText(t *testing.T) {
g := NewGomegaWithT(t)
customTime := TimeBasicDateTimeNoMillis(time.Date(2019, 3, 23, 21, 34, 46, 0, time.UTC))
res, err := customTime.MarshalText()
g.Expect(err).To(BeNil())
g.Expect(string(res)).To(Equal("20190323T213446+00:00"))
}
func TestTimeBasicDateTimeNoMillis_UnmarshalText(t *testing.T) {
g := NewGomegaWithT(t)
var obj TimeBasicDateTimeNoMillis
g.Expect(obj.UnmarshalText([]byte("20190323T213446+00:00"))).To(Succeed())
g.Expect(time.Time(obj).Equal(time.Date(2019, 3, 23, 21, 34, 46, 0, time.UTC))).To(BeTrue())
}
func TestTimeBasicDate_MarshalText(t *testing.T) {
g := NewGomegaWithT(t)
customTime := TimeBasicDate(time.Date(2019, 3, 23, 21, 34, 46, 0, time.UTC))
res, err := customTime.MarshalText()
g.Expect(err).To(BeNil())
g.Expect(string(res)).To(Equal("20190323"))
}
func TestTimeBasicDate_UnmarshalText(t *testing.T) {
g := NewGomegaWithT(t)
var obj TimeBasicDate
g.Expect(obj.UnmarshalText([]byte("20190323"))).To(Succeed())
g.Expect(time.Time(obj).Equal(time.Date(2019, 3, 23, 0, 0, 0, 0, time.UTC))).To(BeTrue())
}
func TestTimeFormat_marshallingIntoJson(t *testing.T) {
g := NewGomegaWithT(t)
type foo struct {
A TimeBasicDateTime
B TimeBasicDateTimeNoMillis
C TimeBasicDate
}
someTime := time.Date(2019, 3, 23, 21, 34, 46, 567000000, time.UTC)
jsonBytes, err := json.Marshal(foo{
A: TimeBasicDateTime(someTime),
B: TimeBasicDateTimeNoMillis(someTime),
C: TimeBasicDate(someTime),
})
var fooUnmarshalled foo
err = json.Unmarshal(jsonBytes, &fooUnmarshalled)
g.Expect(err).To(BeNil())
g.Expect(time.Time(fooUnmarshalled.A).Equal(time.Date(2019, 3, 23, 21, 34, 46, 567000000, time.UTC))).To(BeTrue())
g.Expect(time.Time(fooUnmarshalled.B).Equal(time.Date(2019, 3, 23, 21, 34, 46, 0, time.UTC))).To(BeTrue())
g.Expect(time.Time(fooUnmarshalled.C).Equal(time.Date(2019, 3, 23, 0, 0, 0, 0, time.UTC))).To(BeTrue())
}