-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstrptime_test.go
242 lines (212 loc) · 5.8 KB
/
strptime_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package strptime
import (
"testing"
"time"
)
func TestDatePaths(t *testing.T) {
var err error
var tm time.Time
datepathFormat1 := "/path/with/dates/%Y/%m/%d"
datepathFormat2 := "/blog/%Y/%m/%d/%-"
datepathFormat3 := "/blog/%Y/%m/%d/%-/page2"
datepathFormat4 := "/random/02/03/-0700/numbers/%Y/%m/%d"
datepathFormat5 := "/random/%-/numbers/%Y/%m/%d"
tm, err = Strptime("/path/with/dates/2012/04/28", datepathFormat1)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 28, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("/unmatched/path/with/dates/2012/04/28", datepathFormat1)
if err == nil {
t.Error("Should fail with invalid format")
}
tm, err = Strptime("/blog/2012/04/22/a-post-slug", datepathFormat2)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 22, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("/badblog/2012/04/28/a-post-slug", datepathFormat2)
if err == nil {
t.Error("Should fail with invalid format")
}
tm, err = Strptime("/blog/2012/04/28/a-post-slug/page2", datepathFormat2)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 28, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("/blog/2012/04/22/a-post-slug/page2", datepathFormat3)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 22, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("/blog/2012/04/28/a-post-slug", datepathFormat3)
if err == nil {
t.Error("Should fail with invalid format")
}
tm, err = Strptime("/badblog/2012/04/28/a-post-slug/page2", datepathFormat3)
if err == nil {
t.Error("Should fail with invalid format")
}
tm, err = Strptime("/random/02/03/-0700/numbers/2012/04/28", datepathFormat4)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 28, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("/random/04/28/-0500/numbers/2012/04/28", datepathFormat4)
if err == nil {
t.Error("Should fail with invalid format")
}
tm, err = Strptime("/random/02/03/-0700/numbers/2012/04/22", datepathFormat5)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 22, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("/random/04/28/-0500/numbers/2012/04/28", datepathFormat5)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 28, tm) {
t.Error("Invalid date parsed")
}
}
func TestCrazyDates(t *testing.T) {
var err error
var tm time.Time
dateFormat1 := "%Y%m%d"
dateFormat2 := "%b%d%Y"
dateFormat3 := "%b%d"
tm, err = Strptime("20120428", dateFormat1)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 28, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("20121111", dateFormat1)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.November, 11, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("2012111", dateFormat1)
if err == nil {
t.Error("Should fail with invalid format")
}
tm, err = Strptime("20120440", dateFormat1)
if err == nil {
t.Error("Should fail with invalid date", tm)
}
///
tm, err = Strptime("Apr152012", dateFormat2)
if err != nil {
t.Error(err)
} else if !checkDate(2012, time.April, 15, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("Apr201211", dateFormat2)
if err != nil {
t.Error(err)
} else if !checkDate(1211, time.April, 20, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("2012111", dateFormat2)
if err == nil {
t.Error("Should fail with invalid format")
}
tm, err = Strptime("04042012", dateFormat2)
if err == nil {
t.Error("Should fail with invalid format")
}
///
tm, err = Strptime("Apr15", dateFormat3)
if err != nil {
t.Error(err)
} else if !checkDate(0, time.April, 15, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("Apr20", dateFormat3)
if err != nil {
t.Error(err)
} else if !checkDate(0, time.April, 20, tm) {
t.Error("Invalid date parsed")
}
tm, err = Strptime("Apr2012", dateFormat3)
if err == nil {
t.Error("Should fail with invalid date", tm)
}
tm, err = Strptime("Apr00", dateFormat3)
if err == nil {
t.Log("Really should fail with invalid date", tm)
}
}
func TestTimes(t *testing.T) {
var err error
var tm time.Time
timeFormat1 := "%H:%M:%S.%f"
timeFormat2 := "%I:%M%p"
tm, err = Strptime("20:42:15.98", timeFormat1)
if err != nil {
t.Error(err)
} else if !checkTime(20, 42, 15, tm) {
t.Error("Invalid time parsed")
}
tm, err = Strptime("02:42:15.4", timeFormat1)
if err != nil {
t.Error(err)
} else if !checkTime(2, 42, 15, tm) {
t.Error("Invalid time parsed")
}
tm, err = Strptime("32:42:15.4", timeFormat1)
if err == nil {
t.Error("Should fail with invalid time", tm)
}
tm, err = Strptime("11:42am", timeFormat2)
if err != nil {
t.Error(err)
} else if !checkTime(11, 42, 0, tm) {
t.Error("Invalid time parsed")
}
tm, err = Strptime("11:42pm", timeFormat2)
if err != nil {
t.Error(err)
} else if !checkTime(23, 42, 0, tm) {
t.Error("Invalid time parsed")
}
tm, err = Strptime("12:02am", timeFormat2)
if err != nil {
t.Error(err)
} else if !checkTime(0, 2, 0, tm) {
t.Error("Invalid time parsed")
}
tm, err = Strptime("12:02pm", timeFormat2)
if err != nil {
t.Error(err)
} else if !checkTime(12, 2, 0, tm) {
t.Error("Invalid time parsed")
}
tm, err = Strptime("00:02am", timeFormat2)
if err == nil {
t.Log("Really should fail with invalid time", tm)
}
tm, err = Strptime("00:02pm", timeFormat2)
if err == nil {
t.Log("Really should fail with invalid time", tm)
}
}
func checkDate(year int, month time.Month, day int, tm time.Time) bool {
y, m, d := tm.Date()
if y != year || m != month || d != day {
return false
}
return true
}
func checkTime(hour, minute, sec int, tm time.Time) bool {
if tm.Hour() != hour || tm.Minute() != minute || tm.Second() != sec {
return false
}
return true
}