-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimekit_test.go
193 lines (156 loc) · 4.57 KB
/
timekit_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package timkit
import (
"testing"
"time"
)
var (
tk *TimeKit
l *time.Location
err error
)
func Location(t *testing.B) {
l, err = time.LoadLocation("Local")
if err != nil {
t.Errorf("Test failed:%s .\n", err.Error())
}
}
func init() {
t := new(testing.B)
Location(t)
tk = NewTimeKit(time.Date(2021, time.Month(1), 2, 15, 4, 5, 0, l))
}
func BenchmarkTimeKit_AddCenturies(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
t := NewTimeKit(time.Now())
for i := 0; i < b.N; i++ {
t.AddCenturies(2)
}
}
func BenchmarkTimeKit_StartOfWeek(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
t := NewTimeKit(time.Now())
for i := 0; i < b.N; i++ {
t.StartOfWeek()
}
}
func TestTimeKit_AddCenturies(t *testing.T) {
expected := NewTimeKit(time.Date(2221, 1, 2, 15, 4, 5, 0, l))
tk.AddCenturies(2)
if tk.String() != expected.String() {
t.Errorf("AddCenturies = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddYears(t *testing.T) {
expected := NewTimeKit(time.Date(2023, 1, 2, 15, 4, 5, 0, l))
tk.AddYears(2)
if tk.String() != expected.String() {
t.Errorf("AddYears = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddQuarter(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 4, 2, 15, 4, 5, 0, l))
tk.AddQuarters(1)
if tk.String() != expected.String() {
t.Errorf("AddQuarter = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddMonths(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 8, 2, 15, 4, 5, 0, l))
tk.AddMonths(7)
if tk.String() != expected.String() {
t.Errorf("AddMonths = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddWeek(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 1, 16, 15, 4, 5, 0, l))
tk.AddWeeks(2)
if tk.String() != expected.String() {
t.Errorf("AddWeek = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddDays(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 1, 11, 15, 4, 5, 0, l))
tk.AddDays(3)
if tk.String() != expected.String() {
t.Errorf("AddDays = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddHours(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 1, 9, 6, 4, 5, 0, l))
tk.AddHours(16)
if tk.String() != expected.String() {
t.Errorf("AddHours = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddMinutes(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 1, 8, 15, 20, 5, 0, l))
tk.AddMinutes(16)
if tk.String() != expected.String() {
t.Errorf("AddMinutes = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddSeconds(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 1, 8, 15, 4, 28, 0, l))
tk.AddSeconds(23)
if tk.String() != expected.String() {
t.Errorf("AddSeconds = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_AddWeekdays(t *testing.T) {
expected := NewTimeKit(time.Date(2021, 1, 8, 15, 4, 5, 0, l))
tk.AddWeekdays(5)
if tk.String() != expected.String() {
t.Errorf("AddWeekdays = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_StartOfCentury(t *testing.T) {
expected := NewTimeKit(time.Date(2000, time.Month(1), 1, 0, 0, 0, 0, l))
tk.StartOfCentury()
if tk.String() != expected.String() {
t.Errorf("StartOfCentury = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_StartOfYear(t *testing.T) {
expected := NewTimeKit(time.Date(2021, time.Month(1), 1, 0, 0, 0, 0, l))
tk.StartOfYear()
if tk.String() != expected.String() {
t.Errorf("StartOfYear = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_StartOfQuarter(t *testing.T) {
expected := NewTimeKit(time.Date(2021, time.Month(1), 1, 0, 0, 0, 0, l))
tk.StartOfQuarter()
if tk.String() != expected.String() {
t.Errorf("StartOfQuarter = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_StartOfMonth(t *testing.T) {
expected := NewTimeKit(time.Date(2021, time.Month(1), 1, 0, 0, 0, 0, l))
tk.StartOfMonth()
if tk.String() != expected.String() {
t.Errorf("StartOfMonth = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_StartOfWeek(t *testing.T) {
expected := NewTimeKit(time.Date(2020, time.Month(12), 28, 0, 0, 0, 0, l))
tk.StartOfWeek()
if tk.String() != expected.String() {
t.Errorf("StartOfWeek = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_StartOfDay(t *testing.T) {
expected := NewTimeKit(time.Date(2021, time.Month(1), 2, 0, 0, 0, 0, l))
tk.StartOfDay()
if tk.String() != expected.String() {
t.Errorf("StartOfDay = %+v ,expected %+v", tk, expected)
}
}
func TestTimeKit_DiffInMonths(t *testing.T) {
ntk := NewTimeKit(time.Date(2020, 9, 18, 10, 30, 40, 0, l))
expected := tk.DiffInMonths(ntk, true)
if expected != 3 {
t.Errorf("DiffInMonths = %+v ,expected %+v", expected, 3)
}
}