-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdates_test.go
213 lines (178 loc) · 3.22 KB
/
dates_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
package main
import (
"fmt"
"strings"
"testing"
"time"
)
var epoch time.Time
var testsFoo = []struct {
// an Australian animal name, to help navigate test output
animal string
// the "question" - the time string to be parsed. Times without a
// timezone are Hong Kong times
question string
// your "answer" - the layout string used by time.Parse().
// If you have a parseFuncion "answer" will instead be passed
// into that function
answer string
// the unixTime value of the "question", used by the tests to check
// "question" and "answer" equality
unixTime int64
// some questions can't be parsed only using a layout string; for
// these write a parsing function
parseFunction func(answer string, question string) (time.Time, error)
// some questions can't be parsed exactly; for these write a function
// to test equality
equalityFunction func(unixTime, answerTime time.Time) bool
}{
{
"bandicoot",
"Tuesday, 21 November 2017 7:28:27 PM GMT+08:00",
// YOU need to write the following
"",
1511263707,
// YOU _might_ need to write the following
nil,
// YOU _might_ need to write the following
nil,
},
{
"quokka",
"Tue Jan 27 07:30:41 +0800 1970",
"",
2244641,
nil,
nil,
},
{
"koala",
"Tuesday, 21 November 2017 7:28:27 PM HKT",
"",
1511263707,
nil,
nil,
},
{
"wallaby",
"1991-11-13T00:08:18+08:00",
"",
689962098,
nil,
nil,
},
{
"dingo",
"Fri Oct 26 11:41:59 HKT 1979",
"",
309757319,
nil,
nil,
},
{
"echidna",
"Monday, 21-Apr-75 11:02:18 HKST",
"",
167277738,
nil,
nil,
},
{
"kookaburra",
"1973-11-10T23:42:42+08:00",
"",
121794162,
nil,
nil,
},
{
"emu",
"Thu, 21 Sep 2000 05:45:12 HKT",
"",
969486312,
nil,
nil,
},
{
"quoll",
"Sun, 14 Sep 1997 20:18:04 +0800",
"",
874239484,
nil,
nil,
},
{
"platypus",
"Fri May 7 01:04:53 1982",
"",
389552693,
nil,
nil,
},
{
"bilby",
"21 Apr 87 20:11 HKT",
"",
546005494,
nil,
nil,
},
{
"cassowary",
"08 Jan 70 14:59 +0800",
"",
629954,
nil,
nil,
},
{
"numbat",
"2:54PM",
"",
28104869,
nil,
nil,
},
// if you find the following difficult (I did) see:
// https://stackoverflow.com/questions/47471071/parse-dates-with-ordinal-date-fields/47475260#47475260
{
"wombat",
"Sunday 23rd January 2033 04:38:25 AM",
"",
1990039105,
nil,
nil,
},
{
"kangaroo",
"Tuesday 7th November 2017 03:18:25 PM",
"",
1510039105,
nil,
nil,
},
}
func TestExercises(t *testing.T) {
for _, row := range testsFoo {
var parsedTime time.Time
var err error
if row.parseFunction == nil {
parsedTime, err = time.Parse(row.answer, row.question)
} else {
parsedTime, err = row.parseFunction(row.answer, row.question)
}
if err != nil {
t.Errorf("\n%s:\n question: %s\n answer: %s\nerror:\n %v\n\n", row.animal, row.question, row.answer, err)
}
var equal bool
unixTime := time.Unix(row.unixTime, 0)
if row.equalityFunction == nil {
equal = unixTime.Equal(parsedTime)
} else {
equal = row.equalityFunction(unixTime, parsedTime)
}
if !equal {
t.Errorf("\n%s: unix timestamps don't match:\n you : %+v\n golang: %+v\n\n", row.animal, parsedTime, unixTime)
}
}
}