t_strptime.c revision 1.8 1 /* $NetBSD: t_strptime.c,v 1.8 2015/10/30 01:51:15 ginsbach Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34 The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_strptime.c,v 1.8 2015/10/30 01:51:15 ginsbach Exp $");
36
37 #include <time.h>
38
39 #include <atf-c.h>
40
41 static void
42 h_pass(const char *buf, const char *fmt, int len,
43 int tm_sec, int tm_min, int tm_hour, int tm_mday,
44 int tm_mon, int tm_year, int tm_wday, int tm_yday)
45 {
46 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
47 const char *ret, *exp;
48
49 exp = buf + len;
50 ret = strptime(buf, fmt, &tm);
51
52 ATF_REQUIRE_MSG(ret == exp,
53 "strptime(\"%s\", \"%s\", tm): incorrect return code: "
54 "expected: %p, got: %p", buf, fmt, exp, ret);
55
56 #define H_REQUIRE_FIELD(field) \
57 ATF_REQUIRE_MSG(tm.field == field, \
58 "strptime(\"%s\", \"%s\", tm): incorrect %s: " \
59 "expected: %d, but got: %d", buf, fmt, \
60 ___STRING(field), field, tm.field)
61
62 H_REQUIRE_FIELD(tm_sec);
63 H_REQUIRE_FIELD(tm_min);
64 H_REQUIRE_FIELD(tm_hour);
65 H_REQUIRE_FIELD(tm_mday);
66 H_REQUIRE_FIELD(tm_mon);
67 H_REQUIRE_FIELD(tm_year);
68 H_REQUIRE_FIELD(tm_wday);
69 H_REQUIRE_FIELD(tm_yday);
70
71 #undef H_REQUIRE_FIELD
72 }
73
74 static void
75 h_fail(const char *buf, const char *fmt)
76 {
77 struct tm tm = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
78
79 ATF_REQUIRE_MSG(strptime(buf, fmt, &tm) == NULL, "strptime(\"%s\", "
80 "\"%s\", &tm) should fail, but it didn't", buf, fmt);
81 }
82
83 ATF_TC(common);
84
85 ATF_TC_HEAD(common, tc)
86 {
87
88 atf_tc_set_md_var(tc, "descr", "Checks strptime(3): various checks");
89 }
90
91 ATF_TC_BODY(common, tc)
92 {
93
94 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %T %Y",
95 24, 46, 27, 23, 20, 0, 98, 2, 19);
96 h_pass("Tue Jan 20 23:27:46 1998", "%a %b %d %H:%M:%S %Y",
97 24, 46, 27, 23, 20, 0, 98, 2, 19);
98 h_pass("Tue Jan 20 23:27:46 1998", "%c",
99 24, 46, 27, 23, 20, 0, 98, 2, 19);
100 h_pass("Fri Mar 4 20:05:34 2005", "%a %b %e %H:%M:%S %Y",
101 24, 34, 5, 20, 4, 2, 105, 5, 62);
102 h_pass("5\t3 4 8pm:05:34 2005", "%w%n%m%t%d%n%k%p:%M:%S %Y",
103 21, 34, 5, 20, 4, 2, 105, 5, 62);
104 h_pass("Fri Mar 4 20:05:34 2005", "%c",
105 24, 34, 5, 20, 4, 2, 105, 5, 62);
106 }
107
108 ATF_TC(day);
109
110 ATF_TC_HEAD(day, tc)
111 {
112
113 atf_tc_set_md_var(tc, "descr",
114 "Checks strptime(3) day name conversions [aA]");
115 }
116
117 ATF_TC_BODY(day, tc)
118 {
119
120 h_pass("Sun", "%a", 3, -1, -1, -1, -1, -1, -1, 0, -1);
121 h_pass("Sunday", "%a", 6, -1, -1, -1, -1, -1, -1, 0, -1);
122 h_pass("Mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
123 h_pass("Monday", "%a", 6, -1, -1, -1, -1, -1, -1, 1, -1);
124 h_pass("Tue", "%a", 3, -1, -1, -1, -1, -1, -1, 2, -1);
125 h_pass("Tuesday", "%a", 7, -1, -1, -1, -1, -1, -1, 2, -1);
126 h_pass("Wed", "%a", 3, -1, -1, -1, -1, -1, -1, 3, -1);
127 h_pass("Wednesday", "%a", 9, -1, -1, -1, -1, -1, -1, 3, -1);
128 h_pass("Thu", "%a", 3, -1, -1, -1, -1, -1, -1, 4, -1);
129 h_pass("Thursday", "%a", 8, -1, -1, -1, -1, -1, -1, 4, -1);
130 h_pass("Fri", "%a", 3, -1, -1, -1, -1, -1, -1, 5, -1);
131 h_pass("Friday", "%a", 6, -1, -1, -1, -1, -1, -1, 5, -1);
132 h_pass("Sat", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
133 h_pass("Saturday", "%a", 8, -1, -1, -1, -1, -1, -1, 6, -1);
134 h_pass("Saturn", "%a", 3, -1, -1, -1, -1, -1, -1, 6, -1);
135 h_fail("Moon", "%a");
136 h_pass("Sun", "%A", 3, -1, -1, -1, -1, -1, -1, 0, -1);
137 h_pass("Sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
138 h_pass("Mon", "%A", 3, -1, -1, -1, -1, -1, -1, 1, -1);
139 h_pass("Monday", "%A", 6, -1, -1, -1, -1, -1, -1, 1, -1);
140 h_pass("Tue", "%A", 3, -1, -1, -1, -1, -1, -1, 2, -1);
141 h_pass("Tuesday", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
142 h_pass("Wed", "%A", 3, -1, -1, -1, -1, -1, -1, 3, -1);
143 h_pass("Wednesday", "%A", 9, -1, -1, -1, -1, -1, -1, 3, -1);
144 h_pass("Thu", "%A", 3, -1, -1, -1, -1, -1, -1, 4, -1);
145 h_pass("Thursday", "%A", 8, -1, -1, -1, -1, -1, -1, 4, -1);
146 h_pass("Fri", "%A", 3, -1, -1, -1, -1, -1, -1, 5, -1);
147 h_pass("Friday", "%A", 6, -1, -1, -1, -1, -1, -1, 5, -1);
148 h_pass("Sat", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
149 h_pass("Saturday", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
150 h_pass("Saturn", "%A", 3, -1, -1, -1, -1, -1, -1, 6, -1);
151 h_fail("Moon", "%A");
152
153 h_pass("mon", "%a", 3, -1, -1, -1, -1, -1, -1, 1, -1);
154 h_pass("tueSDay", "%A", 7, -1, -1, -1, -1, -1, -1, 2, -1);
155 h_pass("sunday", "%A", 6, -1, -1, -1, -1, -1, -1, 0, -1);
156 h_fail("sunday", "%EA");
157 h_pass("SaturDay", "%A", 8, -1, -1, -1, -1, -1, -1, 6, -1);
158 h_fail("SaturDay", "%OA");
159 }
160
161 ATF_TC(hour);
162
163 ATF_TC_HEAD(hour, tc)
164 {
165
166 atf_tc_set_md_var(tc, "descr",
167 "Checks strptime(3) hour conversions [IH]");
168 }
169
170 ATF_TC_BODY(hour, tc)
171 {
172
173 h_fail("00", "%I");
174 h_fail("13", "%I");
175
176 h_pass("00", "%H", 2, -1, -1, 0, -1, -1, -1, -1, -1);
177 h_pass("12", "%H", 2, -1, -1, 12, -1, -1, -1, -1, -1);
178 h_pass("23", "%H", 2, -1, -1, 23, -1, -1, -1, -1, -1);
179 h_fail("24", "%H");
180 }
181
182
183 ATF_TC(month);
184
185 ATF_TC_HEAD(month, tc)
186 {
187
188 atf_tc_set_md_var(tc, "descr",
189 "Checks strptime(3) month name conversions [bB]");
190 }
191
192 ATF_TC_BODY(month, tc)
193 {
194
195 h_pass("Jan", "%b", 3, -1, -1, -1, -1, 0, -1, -1, -1);
196 h_pass("January", "%b", 7, -1, -1, -1, -1, 0, -1, -1, -1);
197 h_pass("Feb", "%b", 3, -1, -1, -1, -1, 1, -1, -1, -1);
198 h_pass("February", "%b", 8, -1, -1, -1, -1, 1, -1, -1, -1);
199 h_pass("Mar", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
200 h_pass("March", "%b", 5, -1, -1, -1, -1, 2, -1, -1, -1);
201 h_pass("Apr", "%b", 3, -1, -1, -1, -1, 3, -1, -1, -1);
202 h_pass("April", "%b", 5, -1, -1, -1, -1, 3, -1, -1, -1);
203 h_pass("May", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
204 h_pass("Jun", "%b", 3, -1, -1, -1, -1, 5, -1, -1, -1);
205 h_pass("June", "%b", 4, -1, -1, -1, -1, 5, -1, -1, -1);
206 h_pass("Jul", "%b", 3, -1, -1, -1, -1, 6, -1, -1, -1);
207 h_pass("July", "%b", 4, -1, -1, -1, -1, 6, -1, -1, -1);
208 h_pass("Aug", "%b", 3, -1, -1, -1, -1, 7, -1, -1, -1);
209 h_pass("August", "%b", 6, -1, -1, -1, -1, 7, -1, -1, -1);
210 h_pass("Sep", "%b", 3, -1, -1, -1, -1, 8, -1, -1, -1);
211 h_pass("September", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
212 h_pass("Oct", "%b", 3, -1, -1, -1, -1, 9, -1, -1, -1);
213 h_pass("October", "%b", 7, -1, -1, -1, -1, 9, -1, -1, -1);
214 h_pass("Nov", "%b", 3, -1, -1, -1, -1, 10, -1, -1, -1);
215 h_pass("November", "%b", 8, -1, -1, -1, -1, 10, -1, -1, -1);
216 h_pass("Dec", "%b", 3, -1, -1, -1, -1, 11, -1, -1, -1);
217 h_pass("December", "%b", 8, -1, -1, -1, -1, 11, -1, -1, -1);
218 h_pass("Mayor", "%b", 3, -1, -1, -1, -1, 4, -1, -1, -1);
219 h_pass("Mars", "%b", 3, -1, -1, -1, -1, 2, -1, -1, -1);
220 h_fail("Rover", "%b");
221 h_pass("Jan", "%B", 3, -1, -1, -1, -1, 0, -1, -1, -1);
222 h_pass("January", "%B", 7, -1, -1, -1, -1, 0, -1, -1, -1);
223 h_pass("Feb", "%B", 3, -1, -1, -1, -1, 1, -1, -1, -1);
224 h_pass("February", "%B", 8, -1, -1, -1, -1, 1, -1, -1, -1);
225 h_pass("Mar", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
226 h_pass("March", "%B", 5, -1, -1, -1, -1, 2, -1, -1, -1);
227 h_pass("Apr", "%B", 3, -1, -1, -1, -1, 3, -1, -1, -1);
228 h_pass("April", "%B", 5, -1, -1, -1, -1, 3, -1, -1, -1);
229 h_pass("May", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
230 h_pass("Jun", "%B", 3, -1, -1, -1, -1, 5, -1, -1, -1);
231 h_pass("June", "%B", 4, -1, -1, -1, -1, 5, -1, -1, -1);
232 h_pass("Jul", "%B", 3, -1, -1, -1, -1, 6, -1, -1, -1);
233 h_pass("July", "%B", 4, -1, -1, -1, -1, 6, -1, -1, -1);
234 h_pass("Aug", "%B", 3, -1, -1, -1, -1, 7, -1, -1, -1);
235 h_pass("August", "%B", 6, -1, -1, -1, -1, 7, -1, -1, -1);
236 h_pass("Sep", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
237 h_pass("September", "%B", 9, -1, -1, -1, -1, 8, -1, -1, -1);
238 h_pass("Oct", "%B", 3, -1, -1, -1, -1, 9, -1, -1, -1);
239 h_pass("October", "%B", 7, -1, -1, -1, -1, 9, -1, -1, -1);
240 h_pass("Nov", "%B", 3, -1, -1, -1, -1, 10, -1, -1, -1);
241 h_pass("November", "%B", 8, -1, -1, -1, -1, 10, -1, -1, -1);
242 h_pass("Dec", "%B", 3, -1, -1, -1, -1, 11, -1, -1, -1);
243 h_pass("December", "%B", 8, -1, -1, -1, -1, 11, -1, -1, -1);
244 h_pass("Mayor", "%B", 3, -1, -1, -1, -1, 4, -1, -1, -1);
245 h_pass("Mars", "%B", 3, -1, -1, -1, -1, 2, -1, -1, -1);
246 h_fail("Rover", "%B");
247
248 h_pass("september", "%b", 9, -1, -1, -1, -1, 8, -1, -1, -1);
249 h_pass("septembe", "%B", 3, -1, -1, -1, -1, 8, -1, -1, -1);
250 }
251
252 ATF_TC(seconds);
253
254 ATF_TC_HEAD(seconds, tc)
255 {
256
257 atf_tc_set_md_var(tc, "descr",
258 "Checks strptime(3) seconds conversions [S]");
259 }
260
261 ATF_TC_BODY(seconds, tc)
262 {
263
264 h_pass("0", "%S", 1, 0, -1, -1, -1, -1, -1, -1, -1);
265 h_pass("59", "%S", 2, 59, -1, -1, -1, -1, -1, -1, -1);
266 h_pass("60", "%S", 2, 60, -1, -1, -1, -1, -1, -1, -1);
267 h_pass("61", "%S", 2, 61, -1, -1, -1, -1, -1, -1, -1);
268 h_fail("62", "%S");
269 }
270
271 ATF_TC(year);
272
273 ATF_TC_HEAD(year, tc)
274 {
275
276 atf_tc_set_md_var(tc, "descr",
277 "Checks strptime(3) century/year conversions [CyY]");
278 }
279
280 ATF_TC_BODY(year, tc)
281 {
282
283 h_pass("x20y", "x%Cy", 4, -1, -1, -1, -1, -1, 100, -1, -1);
284 h_pass("x84y", "x%yy", 4, -1, -1, -1, -1, -1, 84, -1, -1);
285 h_pass("x2084y", "x%C%yy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
286 h_pass("x8420y", "x%y%Cy", 6, -1, -1, -1, -1, -1, 184, -1, -1);
287 h_pass("%20845", "%%%C%y5", 6, -1, -1, -1, -1, -1, 184, -1, -1);
288 h_fail("%", "%E%");
289
290 h_pass("1980", "%Y", 4, -1, -1, -1, -1, -1, 80, -1, -1);
291 h_pass("1980", "%EY", 4, -1, -1, -1, -1, -1, 80, -1, -1);
292 }
293
294 ATF_TC(zone);
295
296 ATF_TC_HEAD(zone, tc)
297 {
298
299 atf_tc_set_md_var(tc, "descr",
300 "Checks strptime(3) timezone conversion [z]");
301 }
302
303 static struct {
304 const char *name;
305 long offs;
306 } zt[] = {
307 { "Z", 0 },
308 { "UT", 0 },
309 { "UTC", 0 },
310 { "GMT", 0 },
311 { "EST", -18000 },
312 { "EDT", -14400 },
313 { "CST", -21600 },
314 { "CDT", -18000 },
315 { "MST", -25200 },
316 { "MDT", -21600 },
317 { "PST", -28800 },
318 { "PDT", -25200 },
319
320 { "VST", -1 },
321 { "VDT", -1 },
322
323 { "+03", 10800 },
324 { "-03", -10800 },
325 { "+0403", 14580 },
326 { "-0403", -14580 },
327 { "+04:03", 14580 },
328 { "-04:03", -14580 },
329
330 { "1", -1 },
331 { "03", -1 },
332 { "0304", -1 },
333 { "+1", -1 },
334 { "-203", -1 },
335 { "+12345", -1 },
336 { "+12:345", -1 },
337 { "+123:45", -1 },
338 { "+1430", -1 },
339 { "-1430", -1 },
340 { "+1060", -1 },
341 { "-1060", -1 },
342
343 { "A", -3600 },
344 { "B", -7200 },
345 { "C", -10800 },
346 { "D", -14400 },
347 { "E", -18000 },
348 { "F", -21600 },
349 { "G", -25200 },
350 { "H", -28800 },
351 { "I", -32400 },
352 { "L", -39600 },
353 { "M", -43200 },
354 { "N", 3600 },
355 { "O", 7200 },
356 { "P", 10800 },
357 { "Q", 14400 },
358 { "R", 18000 },
359 { "T", 25200 },
360 { "U", 28800 },
361 { "V", 32400 },
362 { "W", 36000 },
363 { "X", 39600 },
364 { "Y", 43200 },
365
366 { "J", -1 },
367
368 { "America/Los_Angeles", -28800 },
369 { "America/New_York", -18000 },
370 { "EST4EDT", -14400 },
371
372 { "Bogus", -1 },
373 };
374
375 ATF_TC_BODY(zone, tc)
376 {
377 struct tm tm;
378
379 for (size_t i = 0; i < __arraycount(zt); i++) {
380 if (strptime(zt[i].name, "%z", &tm) == NULL)
381 tm.tm_gmtoff = -1;
382 ATF_REQUIRE_MSG(tm.tm_gmtoff == zt[i].offs,
383 "strptime(\"%s\", \"%%z\", &tm): "
384 "expected: tm.tm_gmtoff=%ld, got: tm.tm_gmtoff=%ld",
385 zt[i].name, zt[i].offs, tm.tm_gmtoff);
386 }
387 }
388
389 ATF_TP_ADD_TCS(tp)
390 {
391
392 ATF_TP_ADD_TC(tp, common);
393 ATF_TP_ADD_TC(tp, day);
394 ATF_TP_ADD_TC(tp, hour);
395 ATF_TP_ADD_TC(tp, month);
396 ATF_TP_ADD_TC(tp, seconds);
397 ATF_TP_ADD_TC(tp, year);
398 ATF_TP_ADD_TC(tp, zone);
399
400 return atf_no_error();
401 }
402