-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtest_date.sh
More file actions
executable file
·301 lines (263 loc) · 8.35 KB
/
Copy pathtest_date.sh
File metadata and controls
executable file
·301 lines (263 loc) · 8.35 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/bash
if [ -z "$SKDB_BIN" ]; then
if [ -z "$SKARGO_PROFILE" ]; then
SKARGO_PROFILE=dev
fi
SKDB_BIN="skargo run -q --profile $SKARGO_PROFILE -- "
fi
pass() { printf "%-44s OK\n" "$1:"; }
fail() { printf "%-44s FAILED\n" "$1:"; }
SKDB=$SKDB_BIN
LC_TIME=C
export SKDB LC_TIME
mkdir -p "test/dates"
rm -f "test/dates/*.diff"
if [ ! -f "test/dates/valid_date_list3.txt" ]; then
for y in {1900..2100}; do
for m in {1..12}; do
if [[ "$m" -lt "10" ]];
then
mm="0$m"
else
mm="$m"
fi
for d in {1..31}; do
if [[ "$d" -lt "10" ]];
then
dd="0$d"
else
dd="$d"
fi
date -d "$y-$mm-$dd" +"%Y-%m-%d" &> /dev/null
if [ $? -eq 0 ]
then
echo "$y-$mm-$dd"
fi
done
done
done > "test/dates/valid_date_list3.txt"
fi
if [ ! -f "test/dates/valid_date_list.txt" ]; then
while IFS="-" read -r y mm dd
do
if ((y >= 1990 && y <= 2020)); then
echo "$y-$mm-$dd"
fi
done < "test/dates/valid_date_list3.txt" > "test/dates/valid_date_list.txt"
fi
if [ ! -f "test/dates/valid_date_list2.txt" ]; then
while IFS="-" read -r y mm dd
do
if ((y >= 1995 && y <= 2005)); then
echo "$y-$mm-$dd"
fi
done < "test/dates/valid_date_list.txt" > "test/dates/valid_date_list2.txt"
fi
if [ ! -f "test/dates/days.sql" ]; then
while IFS="" read -r d || [ -n "$d" ]
do
echo "select strftime('%Y-%m-%d %H:%M:%S', '$d');"
done < "test/dates/valid_date_list3.txt" > "test/dates/days.sql"
fi
if [ ! -f "test/dates/weeknumbers.sql" ]; then
while IFS="" read -r d || [ -n "$d" ]
do
echo "select strftime('%Y-%m-%d: %W', '$d');"
done < "test/dates/valid_date_list.txt" > "test/dates/weeknumbers.sql"
fi
if [ ! -f "test/dates/plus_one_day.sql" ]; then
while IFS="" read -r d || [ -n "$d" ]
do
echo "select strftime('%Y-%m-%d %H:%M:%S', '$d', '+1 day');"
done < "test/dates/valid_date_list.txt" > "test/dates/plus_one_day.sql"
fi
if [ ! -f "test/dates/minus_one_day.sql" ]; then
while IFS="" read -r d || [ -n "$d" ]
do
echo "select strftime('%Y-%m-%d %H:%M:%S', '$d', '-1 day');"
done < "test/dates/valid_date_list.txt" > "test/dates/minus_one_day.sql"
fi
run_test () {
cat "$1" | $SKDB --always-allow-joins | sort > "$1.out";
cat "$1" | sqlite3 | sort > "$1.res"
diff=$(diff -u "$1.out" "$1.res")
if [ "$diff" == "" ]; then
rm -f "$1.out"
rm -f "$1.res"
pass "$2"
else
echo "$diff" > "$1.diff"
fail "$2"
exit 2
fi
}
run_l_date () {
echo "select strftime('$2', '$3', 'islocal');" | $SKDB >> "$1.out";
date -d "$3" +"$2" >> "$1.res";
}
run_date () {
echo "select strftime('$2', '$3');" | $SKDB >> "$1.out";
date -u -d "$3" +"$2" >> "$1.res";
}
run_date_test () {
rm -f "$1.out"
rm -f "$1.res"
run_date "$1" "A: %A" "$3"
run_date "$1" "a: %a" "$3"
run_date "$1" "B: %B" "$3"
run_date "$1" "b: %b" "$3"
run_date "$1" "C: %C" "$3"
run_date "$1" "D: %D" "$3"
run_date "$1" "d: %d" "$3"
run_date "$1" "e: %e" "$3"
run_date "$1" "F: %F" "$3"
run_date "$1" "H: %H" "$3"
run_date "$1" "h: %h" "$3"
run_date "$1" "I: %I" "$3"
run_date "$1" "j: %j" "$3"
run_date "$1" "k: %k" "$3"
run_date "$1" "L: %L" "$3"
run_date "$1" "l: %l" "$3"
run_date "$1" "M: %M" "$3"
run_date "$1" "m: %m" "$3"
run_date "$1" "n: line1%nn: line2" "$3"
run_date "$1" "P: %P" "$3"
run_date "$1" "p: %p" "$3"
run_date "$1" "Q: %Q" "$3"
run_date "$1" "R: %R" "$3"
run_date "$1" "S: %S" "$3"
run_date "$1" "T: %T" "$3"
run_date "$1" "t: a%tb" "$3"
run_date "$1" "U: %U" "$3"
run_date "$1" "u: %u" "$3"
run_date "$1" "V: %V" "$3"
run_date "$1" "W: %W" "$3"
run_date "$1" "w: %w" "$3"
run_date "$1" "Y: %Y" "$3"
run_date "$1" "y: %y" "$3"
run_l_date "$1" "z: %z" "$3"
run_l_date "$1" "Z: %Z" "$3"
diff=$(diff -u "$1.out" "$1.res")
if [ "$diff" == "" ]; then
rm -f "$1.out"
rm -f "$1.res"
pass "$2"
else
echo "$diff" > "$1.diff"
fail "$2"
exit 2
fi
}
##############################################################################
# testing weeknumbers #
##############################################################################
##############################################################################
# testing +1 day #
##############################################################################
##############################################################################
# testing modifiers with hours #
##############################################################################
run_hour_test () {
for i in {"23:00","23:30","00:00","00:30","01:00"}; do
tmp=${1/+/plus}
suffix=${tmp// /_}_${i//:/_}
if [ ! -f "test/dates/hour_$suffix.sql" ]; then
while IFS="" read -r d || [ -n "$d" ]
do
echo "select strftime('%Y-%m-%d %H:%M:%S', '$d $i', '$1');"
done < "test/dates/valid_date_list2.txt" > "test/dates/hour_$suffix.sql"
fi
run_test "test/dates/hour_$suffix.sql" "$1 (at hour $i)"
done
}
run_hour_test2 () {
for i in {"23:00","23:30","00:00","00:30","01:00"}; do
tmp=${1/-/minus}
suffix=${tmp// /_}_${i//:/_}
if [ ! -f "test/dates/hour_$suffix.sql" ]; then
while IFS="" read -r d || [ -n "$d" ]
do
echo "select strftime('%Y-%m-%d %H:%M:%S', '$d $i', '$1');"
done < "test/dates/valid_date_list2.txt" > "test/dates/hour_$suffix.sql"
fi
run_test "test/dates/hour_$suffix.sql" "$1 (at hour $i)"
done
}
##############################################################################
# testing days #
##############################################################################
##############################################################################
# modify time #
##############################################################################
run_modify_time () {
date=$(date +"%Y-%m-%d %H:%M:%S")
if [ ! -f "test/dates/$1.sql" ]; then
for i in {1..20500}; do
echo "select strftime('%Y-%m-%d %H:%M:%S', '$date', '$2$i $3');"
done > "test/dates/$1.sql"
fi
run_test "test/dates/$1.sql" "$1"
}
##############################################################################
# testing epoch #
##############################################################################
run_epoch_utc_test () {
rm -f "$1.out"
rm -f "$1.res"
while IFS="" read -r d || [ -n "$d" ]
do
run_date "$1" "%Y-%m-%d %H:%M:%S => %s" "$d"
done < "test/dates/valid_date_list2.txt"
diff=$(diff -u "$1.out" "$1.res")
if [ "$diff" == "" ]; then
rm -f "$1.out"
rm -f "$1.res"
pass "$2"
else
echo "$diff" > "$1.diff";
fail "$2"
exit 2
fi
}
run_epoch_locale_test () {
rm -f "$1.out"
rm -f "$1.res"
while IFS="" read -r d || [ -n "$d" ]
do
run_l_date "$1" "%Y-%m-%d %H:%M:%S => %s" "$d"
done < "test/dates/valid_date_list2.txt"
diff=$(diff -u "$1.out" "$1.res")
if [ "$diff" == "" ]; then
rm -f "$1.out"
rm -f "$1.res"
pass "$2"
else
echo "$diff" > "$1.diff";
fail "$2"
exit 2
fi
}
# Make functions defined in this file visible to parallel's sub-shells
export -f pass fail run_date run_l_date run_hour_test run_hour_test2 run_date_test run_test run_modify_time
run_hour_test2 "-365 day"
run_hour_test2 "-366 day"
seq 0 6 | parallel 'run_hour_test "weekday {}"'
seq 0 13 | parallel 'run_hour_test "+{} month" ; run_hour_test2 "-{} month"'
seq 0 32 | parallel 'run_hour_test "+{} day" ; run_hour_test2 "-{} day"'
parallel -n3 run_date_test ::: \
test/dates/formats_1.sql "formats 1" "2023-09-18 13:26:54" \
test/dates/formats_2.sql "formats 2" "2021-01-27 08:52:03" \
test/dates/formats_3.sql "formats 3" "1985-01-01 00:00:00" \
test/dates/formats_4.sql "formats 4" "1963-12-31 00:00:00"
parallel -n2 run_test ::: \
test/dates/weeknumbers.sql "weeknumbers" \
test/dates/plus_one_day.sql "+1 day (at hour 00:00)" \
test/dates/minus_one_day.sql "-1 day (at hour 00:00)" \
test/dates/days.sql "days"
parallel -n3 run_modify_time ::: \
"add_hours" "+" "hour" \
"minus_hours" "-" "hour" \
"add_minutes" "+" "minute" \
"minus_minute" "-" "minute"
run_epoch_utc_test "test/dates/epoch_utc.sql" "epoch_utc"
run_epoch_locale_test "test/dates/epoch_locale.sql" "epoch_locale"