t_parsedate.c revision 1.14 1 1.14 christos /* $NetBSD: t_parsedate.c,v 1.14 2015/12/07 20:52:46 christos Exp $ */
2 1.1 njoly /*-
3 1.14 christos * Copyright (c) 2010, 2015 The NetBSD Foundation, Inc.
4 1.1 njoly * All rights reserved.
5 1.1 njoly *
6 1.1 njoly * Redistribution and use in source and binary forms, with or without
7 1.1 njoly * modification, are permitted provided that the following conditions
8 1.1 njoly * are met:
9 1.1 njoly *
10 1.1 njoly * 1. Redistributions of source code must retain the above copyright
11 1.1 njoly * notice, this list of conditions and the following disclaimer.
12 1.1 njoly * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 njoly * notice, this list of conditions and the following disclaimer in
14 1.1 njoly * the documentation and/or other materials provided with the
15 1.1 njoly * distribution.
16 1.1 njoly *
17 1.1 njoly * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 1.1 njoly * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 1.1 njoly * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 1.1 njoly * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 1.1 njoly * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 njoly * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 njoly * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 njoly * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 njoly * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 njoly * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 1.1 njoly * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 njoly * SUCH DAMAGE.
29 1.1 njoly */
30 1.1 njoly
31 1.1 njoly #include <sys/cdefs.h>
32 1.14 christos __RCSID("$NetBSD: t_parsedate.c,v 1.14 2015/12/07 20:52:46 christos Exp $");
33 1.1 njoly
34 1.1 njoly #include <atf-c.h>
35 1.6 apb #include <errno.h>
36 1.12 apb #include <stdio.h>
37 1.6 apb #include <stdlib.h>
38 1.6 apb #include <time.h>
39 1.1 njoly #include <util.h>
40 1.1 njoly
41 1.8 apb /*
42 1.8 apb * ANY is used as a placeholder for values that do not need to be
43 1.8 apb * checked. The actual value is arbitrary. We don't use -1
44 1.8 apb * because some tests might want to use -1 as a literal value.
45 1.8 apb */
46 1.8 apb #define ANY -30215
47 1.8 apb
48 1.8 apb /* parsecheck --
49 1.8 apb * call parsedate(), then call time_to_tm() on the result,
50 1.8 apb * and check that year/month/day/hour/minute/second are as expected.
51 1.8 apb *
52 1.8 apb * time_to_tm should usually be localtime_r or gmtime_r.
53 1.8 apb *
54 1.8 apb * Don't check values specified as ANY.
55 1.8 apb */
56 1.8 apb static void
57 1.8 apb parsecheck(const char *datestr, const time_t *reftime, const int *zoff,
58 1.8 apb struct tm * time_to_tm(const time_t *, struct tm *),
59 1.8 apb int year, int month, int day, int hour, int minute, int second)
60 1.8 apb {
61 1.8 apb time_t t;
62 1.8 apb struct tm tm;
63 1.12 apb char argstr[128];
64 1.12 apb
65 1.12 apb /*
66 1.12 apb * printable version of the args.
67 1.12 apb *
68 1.12 apb * Note that printf("%.*d", 0, 0)) prints nothing at all,
69 1.12 apb * while printf("%.*d", 1, val) prints the value as usual.
70 1.12 apb */
71 1.12 apb snprintf(argstr, sizeof(argstr), "%s%s%s, %s%.*jd, %s%.*d",
72 1.12 apb /* NULL or \"<datestr>\" */
73 1.12 apb (datestr ? "\"" : ""),
74 1.12 apb (datestr ? datestr : "NULL"),
75 1.12 apb (datestr ? "\"" : ""),
76 1.12 apb /* NULL or *reftime */
77 1.12 apb (reftime ? "" : "NULL"),
78 1.12 apb (reftime ? 1 : 0),
79 1.12 apb (reftime ? (intmax_t)*reftime : (intmax_t)0),
80 1.12 apb /* NULL or *zoff */
81 1.12 apb (zoff ? "" : "NULL"),
82 1.12 apb (zoff ? 1 : 0),
83 1.12 apb (zoff ? *zoff : 0));
84 1.8 apb
85 1.8 apb ATF_CHECK_MSG((t = parsedate(datestr, reftime, zoff)) != -1,
86 1.12 apb "parsedate(%s) returned -1\n", argstr);
87 1.8 apb ATF_CHECK(time_to_tm(&t, &tm) != NULL);
88 1.8 apb if (year != ANY)
89 1.8 apb ATF_CHECK_MSG(tm.tm_year + 1900 == year,
90 1.12 apb "parsedate(%s) expected year %d got %d (+1900)\n",
91 1.12 apb argstr, year, (int)tm.tm_year);
92 1.8 apb if (month != ANY)
93 1.8 apb ATF_CHECK_MSG(tm.tm_mon + 1 == month,
94 1.12 apb "parsedate(%s) expected month %d got %d (+1)\n",
95 1.12 apb argstr, month, (int)tm.tm_mon);
96 1.8 apb if (day != ANY)
97 1.8 apb ATF_CHECK_MSG(tm.tm_mday == day,
98 1.12 apb "parsedate(%s) expected day %d got %d\n",
99 1.12 apb argstr, day, (int)tm.tm_mday);
100 1.8 apb if (hour != ANY)
101 1.8 apb ATF_CHECK_MSG(tm.tm_hour == hour,
102 1.12 apb "parsedate(%s) expected hour %d got %d\n",
103 1.12 apb argstr, hour, (int)tm.tm_hour);
104 1.8 apb if (minute != ANY)
105 1.8 apb ATF_CHECK_MSG(tm.tm_min == minute,
106 1.12 apb "parsedate(%s) expected minute %d got %d\n",
107 1.12 apb argstr, minute, (int)tm.tm_min);
108 1.8 apb if (second != ANY)
109 1.8 apb ATF_CHECK_MSG(tm.tm_sec == second,
110 1.12 apb "parsedate(%s) expected second %d got %d\n",
111 1.12 apb argstr, second, (int)tm.tm_sec);
112 1.8 apb }
113 1.8 apb
114 1.1 njoly ATF_TC(dates);
115 1.1 njoly
116 1.1 njoly ATF_TC_HEAD(dates, tc)
117 1.1 njoly {
118 1.4 christos atf_tc_set_md_var(tc, "descr", "Test unambiguous dates"
119 1.5 jruoho " (PR lib/44255)");
120 1.1 njoly }
121 1.1 njoly
122 1.1 njoly ATF_TC_BODY(dates, tc)
123 1.1 njoly {
124 1.1 njoly
125 1.10 apb parsecheck("9/10/69", NULL, NULL, localtime_r,
126 1.10 apb 2069, 9, 10, 0, 0, 0); /* year < 70: add 2000 */
127 1.10 apb parsecheck("9/10/70", NULL, NULL, localtime_r,
128 1.10 apb 1970, 9, 10, 0, 0, 0); /* 70 <= year < 100: add 1900 */
129 1.8 apb parsecheck("69-09-10", NULL, NULL, localtime_r,
130 1.10 apb 69, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
131 1.9 apb parsecheck("70-09-10", NULL, NULL, localtime_r,
132 1.10 apb 70, 9, 10, 0, 0, 0); /* ISO8601 year remains unchanged */
133 1.8 apb parsecheck("2006-11-17", NULL, NULL, localtime_r,
134 1.8 apb 2006, 11, 17, 0, 0, 0);
135 1.8 apb parsecheck("10/1/2000", NULL, NULL, localtime_r,
136 1.9 apb 2000, 10, 1, 0, 0, 0); /* month/day/year */
137 1.8 apb parsecheck("20 Jun 1994", NULL, NULL, localtime_r,
138 1.8 apb 1994, 6, 20, 0, 0, 0);
139 1.14 christos parsecheck("97 September 2", NULL, NULL, localtime_r,
140 1.14 christos 1997, 9, 2, 0, 0, 0);
141 1.8 apb parsecheck("23jun2001", NULL, NULL, localtime_r,
142 1.8 apb 2001, 6, 23, 0, 0, 0);
143 1.8 apb parsecheck("1-sep-06", NULL, NULL, localtime_r,
144 1.8 apb 2006, 9, 1, 0, 0, 0);
145 1.8 apb parsecheck("1/11", NULL, NULL, localtime_r,
146 1.9 apb ANY, 1, 11, 0, 0, 0); /* month/day */
147 1.8 apb parsecheck("1500-01-02", NULL, NULL, localtime_r,
148 1.8 apb 1500, 1, 2, 0, 0, 0);
149 1.8 apb parsecheck("9999-12-21", NULL, NULL, localtime_r,
150 1.8 apb 9999, 12, 21, 0, 0, 0);
151 1.14 christos parsecheck("2015.12.07.08.07.35", NULL, NULL, localtime_r,
152 1.14 christos 2015, 12, 7, 8, 7, 35);
153 1.1 njoly }
154 1.1 njoly
155 1.1 njoly ATF_TC(times);
156 1.1 njoly
157 1.1 njoly ATF_TC_HEAD(times, tc)
158 1.1 njoly {
159 1.4 christos atf_tc_set_md_var(tc, "descr", "Test times"
160 1.5 jruoho " (PR lib/44255)");
161 1.1 njoly }
162 1.1 njoly
163 1.1 njoly ATF_TC_BODY(times, tc)
164 1.1 njoly {
165 1.1 njoly
166 1.8 apb parsecheck("10:01", NULL, NULL, localtime_r,
167 1.8 apb ANY, ANY, ANY, 10, 1, 0);
168 1.8 apb parsecheck("10:12pm", NULL, NULL, localtime_r,
169 1.8 apb ANY, ANY, ANY, 22, 12, 0);
170 1.8 apb parsecheck("12:11:01.000012", NULL, NULL, localtime_r,
171 1.8 apb ANY, ANY, ANY, 12, 11, 1);
172 1.8 apb parsecheck("12:21-0500", NULL, NULL, gmtime_r,
173 1.8 apb ANY, ANY, ANY, 12+5, 21, 0);
174 1.14 christos /* numeric zones not permitted with am/pm ... */
175 1.14 christos parsecheck("7 a.m. ICT", NULL, NULL, gmtime_r,
176 1.14 christos ANY, ANY, ANY, 7-7, 0, 0);
177 1.14 christos parsecheck("midnight", NULL, NULL, localtime_r,
178 1.14 christos ANY, ANY, ANY, 0, 0, 0);
179 1.14 christos parsecheck("mn", NULL, NULL, localtime_r,
180 1.14 christos ANY, ANY, ANY, 0, 0, 0);
181 1.14 christos parsecheck("noon", NULL, NULL, localtime_r,
182 1.14 christos ANY, ANY, ANY, 12, 0, 0);
183 1.14 christos parsecheck("dawn", NULL, NULL, localtime_r,
184 1.14 christos ANY, ANY, ANY, 6, 0, 0);
185 1.14 christos parsecheck("sunset", NULL, NULL, localtime_r,
186 1.14 christos ANY, ANY, ANY, 18, 0, 0);
187 1.1 njoly }
188 1.1 njoly
189 1.11 apb ATF_TC(dsttimes);
190 1.11 apb
191 1.11 apb ATF_TC_HEAD(dsttimes, tc)
192 1.11 apb {
193 1.11 apb atf_tc_set_md_var(tc, "descr", "Test DST transition times"
194 1.11 apb " (PR lib/47916)");
195 1.11 apb }
196 1.11 apb
197 1.11 apb ATF_TC_BODY(dsttimes, tc)
198 1.11 apb {
199 1.11 apb struct tm tm;
200 1.11 apb time_t t;
201 1.11 apb int tzoff;
202 1.11 apb
203 1.11 apb putenv(__UNCONST("TZ=EST"));
204 1.11 apb tzset();
205 1.11 apb parsecheck("12:0", NULL, NULL, localtime_r,
206 1.11 apb ANY, ANY, ANY, 12, 0, 0);
207 1.11 apb
208 1.14 christos putenv(__UNCONST("TZ=Asia/Tokyo"));
209 1.11 apb tzset();
210 1.11 apb parsecheck("12:0", NULL, NULL, localtime_r,
211 1.11 apb ANY, ANY, ANY, 12, 0, 0);
212 1.11 apb
213 1.11 apb /*
214 1.11 apb * When the effective local time is Tue Jul 9 13:21:53 BST 2013,
215 1.11 apb * check mktime("14:00")
216 1.11 apb */
217 1.11 apb putenv(__UNCONST("TZ=Europe/London"));
218 1.11 apb tzset();
219 1.11 apb tm = (struct tm){
220 1.11 apb .tm_year = 2013-1900, .tm_mon = 7-1, .tm_mday = 9,
221 1.11 apb .tm_hour = 13, .tm_min = 21, .tm_sec = 53,
222 1.11 apb .tm_isdst = 0 };
223 1.11 apb t = mktime(&tm);
224 1.11 apb ATF_CHECK(t != (time_t)-1);
225 1.11 apb parsecheck("14:00", &t, NULL, localtime_r,
226 1.11 apb 2013, 7, 9, 14, 0, 0);
227 1.13 apb tzoff = -60; /* British Summer Time */
228 1.11 apb parsecheck("14:00", &t, &tzoff, localtime_r,
229 1.11 apb 2013, 7, 9, 14, 0, 0);
230 1.11 apb }
231 1.11 apb
232 1.1 njoly ATF_TC(relative);
233 1.1 njoly
234 1.1 njoly ATF_TC_HEAD(relative, tc)
235 1.1 njoly {
236 1.4 christos atf_tc_set_md_var(tc, "descr", "Test relative items"
237 1.5 jruoho " (PR lib/44255)");
238 1.1 njoly }
239 1.1 njoly
240 1.1 njoly ATF_TC_BODY(relative, tc)
241 1.1 njoly {
242 1.14 christos struct tm tm;
243 1.14 christos time_t now;
244 1.14 christos
245 1.14 christos #define REL_CHECK(s, now, tm) do { \
246 1.14 christos time_t p, q; \
247 1.14 christos char pb[30], qb[30]; \
248 1.14 christos ATF_CHECK_EQ_MSG((p = parsedate(s, &now, NULL)), \
249 1.14 christos (q = mktime(&tm)), \
250 1.14 christos "From %s Expected %jd: %24.24s Obtained %jd: %24.24s", s, \
251 1.14 christos (uintmax_t)p, ctime_r(&p, pb), (uintmax_t)q, \
252 1.14 christos ctime_r(&q, qb)); \
253 1.14 christos } while (/*CONSTCOND*/0)
254 1.1 njoly
255 1.1 njoly ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1);
256 1.1 njoly ATF_CHECK(parsedate("last friday", NULL, NULL) != -1);
257 1.1 njoly ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1);
258 1.1 njoly ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1);
259 1.1 njoly ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1);
260 1.1 njoly ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1);
261 1.14 christos
262 1.14 christos (void)time(&now);
263 1.14 christos
264 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
265 1.14 christos tm.tm_mday--;
266 1.14 christos /* "yesterday" leaves time untouched */
267 1.14 christos tm.tm_isdst = -1;
268 1.14 christos REL_CHECK("yesterday", now, tm);
269 1.14 christos
270 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
271 1.14 christos tm.tm_mday++;
272 1.14 christos /* as does "tomorrow" */
273 1.14 christos tm.tm_isdst = -1;
274 1.14 christos REL_CHECK("tomorrow", now, tm);
275 1.14 christos
276 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
277 1.14 christos if (tm.tm_wday > 4)
278 1.14 christos tm.tm_mday += 7;
279 1.14 christos tm.tm_mday += 4 - tm.tm_wday;
280 1.14 christos /* if a day name is mentioned, it means midnight (by default) */
281 1.14 christos tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
282 1.14 christos tm.tm_isdst = -1;
283 1.14 christos REL_CHECK("this thursday", now, tm);
284 1.14 christos
285 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
286 1.14 christos tm.tm_mday += 14 - tm.tm_wday;
287 1.14 christos tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
288 1.14 christos tm.tm_isdst = -1;
289 1.14 christos REL_CHECK("next sunday", now, tm);
290 1.14 christos
291 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
292 1.14 christos if (tm.tm_wday <= 5)
293 1.14 christos tm.tm_mday -= 7;
294 1.14 christos tm.tm_mday += 5 - tm.tm_wday;
295 1.14 christos tm.tm_sec = tm.tm_min = 0;
296 1.14 christos tm.tm_hour = 16;
297 1.14 christos tm.tm_isdst = -1;
298 1.14 christos REL_CHECK("last friday 4 p.m.", now, tm);
299 1.14 christos
300 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
301 1.14 christos tm.tm_mday += 14;
302 1.14 christos if (tm.tm_wday > 3)
303 1.14 christos tm.tm_mday += 7;
304 1.14 christos tm.tm_mday += 3 - tm.tm_wday;
305 1.14 christos tm.tm_sec = tm.tm_min = 0;
306 1.14 christos tm.tm_hour = 3;
307 1.14 christos tm.tm_isdst = -1;
308 1.14 christos REL_CHECK("we fortnight 3 a.m.", now, tm);
309 1.14 christos
310 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
311 1.14 christos tm.tm_min -= 5;
312 1.14 christos tm.tm_isdst = -1;
313 1.14 christos REL_CHECK("5 minutes ago", now, tm);
314 1.14 christos
315 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
316 1.14 christos tm.tm_hour++;
317 1.14 christos tm.tm_min += 37;
318 1.14 christos tm.tm_isdst = -1;
319 1.14 christos REL_CHECK("97 minutes", now, tm);
320 1.14 christos
321 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
322 1.14 christos tm.tm_mon++;
323 1.14 christos tm.tm_isdst = -1;
324 1.14 christos REL_CHECK("month", now, tm);
325 1.14 christos
326 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
327 1.14 christos tm.tm_mon += 2; /* "next" means add 2 ... */
328 1.14 christos tm.tm_isdst = -1;
329 1.14 christos REL_CHECK("next month", now, tm);
330 1.14 christos
331 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
332 1.14 christos tm.tm_mon--;
333 1.14 christos tm.tm_isdst = -1;
334 1.14 christos REL_CHECK("last month", now, tm);
335 1.14 christos
336 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
337 1.14 christos tm.tm_mon += 6;
338 1.14 christos tm.tm_mday += 2;
339 1.14 christos tm.tm_isdst = -1;
340 1.14 christos REL_CHECK("+6 months 2 days", now, tm);
341 1.14 christos
342 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
343 1.14 christos tm.tm_mon -= 9;
344 1.14 christos tm.tm_isdst = -1;
345 1.14 christos REL_CHECK("9 months ago", now, tm);
346 1.14 christos
347 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
348 1.14 christos if (tm.tm_wday <= 2)
349 1.14 christos tm.tm_mday -= 7;
350 1.14 christos tm.tm_mday += 2 - tm.tm_wday;
351 1.14 christos tm.tm_isdst = -1;
352 1.14 christos tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
353 1.14 christos REL_CHECK("1 week ago Tu", now, tm);
354 1.14 christos #if 0
355 1.14 christos REL_DEBUG("1 week ago Tu");
356 1.14 christos #endif
357 1.14 christos
358 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
359 1.14 christos tm.tm_isdst = -1;
360 1.14 christos tm.tm_mday++;
361 1.14 christos tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
362 1.14 christos REL_CHECK("midnight tomorrow", now, tm);
363 1.14 christos
364 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
365 1.14 christos tm.tm_isdst = -1;
366 1.14 christos tm.tm_mday++;
367 1.14 christos tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
368 1.14 christos REL_CHECK("tomorrow midnight", now, tm);
369 1.14 christos
370 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
371 1.14 christos tm.tm_isdst = -1;
372 1.14 christos tm.tm_mday++;
373 1.14 christos tm.tm_hour = 12;
374 1.14 christos tm.tm_min = tm.tm_sec = 0;
375 1.14 christos REL_CHECK("noon tomorrow", now, tm);
376 1.14 christos
377 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
378 1.14 christos if (tm.tm_wday > 2)
379 1.14 christos tm.tm_mday += 7;
380 1.14 christos tm.tm_mday += 2 - tm.tm_wday;
381 1.14 christos tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
382 1.14 christos tm.tm_isdst = -1;
383 1.14 christos REL_CHECK("midnight Tuesday", now, tm);
384 1.14 christos
385 1.14 christos ATF_CHECK(localtime_r(&now, &tm) != NULL);
386 1.14 christos if (tm.tm_wday > 2)
387 1.14 christos tm.tm_mday += 7;
388 1.14 christos tm.tm_mday += 2 - tm.tm_wday;
389 1.14 christos tm.tm_mday++; /* xxx midnight --> the next day */
390 1.14 christos tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
391 1.14 christos tm.tm_isdst = -1;
392 1.14 christos REL_CHECK("Tuesday midnight", now, tm);
393 1.14 christos
394 1.14 christos #undef REL_DEBUG
395 1.1 njoly }
396 1.1 njoly
397 1.6 apb ATF_TC(atsecs);
398 1.6 apb
399 1.6 apb ATF_TC_HEAD(atsecs, tc)
400 1.6 apb {
401 1.6 apb atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch");
402 1.6 apb }
403 1.6 apb
404 1.6 apb ATF_TC_BODY(atsecs, tc)
405 1.6 apb {
406 1.6 apb int tzoff;
407 1.6 apb
408 1.6 apb /* "@0" -> (time_t)0, regardless of timezone */
409 1.6 apb ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
410 1.6 apb putenv(__UNCONST("TZ=Europe/Berlin"));
411 1.6 apb tzset();
412 1.6 apb ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
413 1.6 apb putenv(__UNCONST("TZ=America/New_York"));
414 1.6 apb tzset();
415 1.6 apb ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0);
416 1.6 apb tzoff = 0;
417 1.6 apb ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
418 1.6 apb tzoff = 3600;
419 1.6 apb ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
420 1.6 apb tzoff = -3600;
421 1.6 apb ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0);
422 1.6 apb
423 1.7 apb /* -1 or other negative numbers are not errors */
424 1.6 apb errno = 0;
425 1.6 apb ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0);
426 1.7 apb ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0);
427 1.7 apb
428 1.7 apb /* junk is an error */
429 1.7 apb errno = 0;
430 1.7 apb ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0);
431 1.6 apb }
432 1.6 apb
433 1.14 christos ATF_TC(zones);
434 1.14 christos
435 1.14 christos ATF_TC_HEAD(zones, tc)
436 1.14 christos {
437 1.14 christos atf_tc_set_md_var(tc, "descr", "Test parsing dates with zones");
438 1.14 christos }
439 1.14 christos
440 1.14 christos ATF_TC_BODY(zones, tc)
441 1.14 christos {
442 1.14 christos parsecheck("2015-12-06 16:11:48 UTC", NULL, NULL, gmtime_r,
443 1.14 christos 2015, 12, 6, 16, 11, 48);
444 1.14 christos parsecheck("2015-12-06 16:11:48 UT", NULL, NULL, gmtime_r,
445 1.14 christos 2015, 12, 6, 16, 11, 48);
446 1.14 christos parsecheck("2015-12-06 16:11:48 GMT", NULL, NULL, gmtime_r,
447 1.14 christos 2015, 12, 6, 16, 11, 48);
448 1.14 christos parsecheck("2015-12-06 16:11:48 +0000", NULL, NULL, gmtime_r,
449 1.14 christos 2015, 12, 6, 16, 11, 48);
450 1.14 christos
451 1.14 christos parsecheck("2015-12-06 16:11:48 -0500", NULL, NULL, gmtime_r,
452 1.14 christos 2015, 12, 6, 21, 11, 48);
453 1.14 christos parsecheck("2015-12-06 16:11:48 EST", NULL, NULL, gmtime_r,
454 1.14 christos 2015, 12, 6, 21, 11, 48);
455 1.14 christos parsecheck("2015-12-06 16:11:48 EDT", NULL, NULL, gmtime_r,
456 1.14 christos 2015, 12, 6, 20, 11, 48);
457 1.14 christos parsecheck("2015-12-06 16:11:48 +0500", NULL, NULL, gmtime_r,
458 1.14 christos 2015, 12, 6, 11, 11, 48);
459 1.14 christos
460 1.14 christos parsecheck("2015-12-06 16:11:48 +1000", NULL, NULL, gmtime_r,
461 1.14 christos 2015, 12, 6, 6, 11, 48);
462 1.14 christos parsecheck("2015-12-06 16:11:48 AEST", NULL, NULL, gmtime_r,
463 1.14 christos 2015, 12, 6, 6, 11, 48);
464 1.14 christos parsecheck("2015-12-06 16:11:48 -1000", NULL, NULL, gmtime_r,
465 1.14 christos 2015, 12, 7, 2, 11, 48);
466 1.14 christos parsecheck("2015-12-06 16:11:48 HST", NULL, NULL, gmtime_r,
467 1.14 christos 2015, 12, 7, 2, 11, 48);
468 1.14 christos
469 1.14 christos parsecheck("2015-12-06 16:11:48 AWST", NULL, NULL, gmtime_r,
470 1.14 christos 2015, 12, 6, 8, 11, 48);
471 1.14 christos parsecheck("2015-12-06 16:11:48 NZDT", NULL, NULL, gmtime_r,
472 1.14 christos 2015, 12, 6, 3, 11, 48);
473 1.14 christos
474 1.14 christos parsecheck("Sun, 6 Dec 2015 09:43:16 -0500", NULL, NULL, gmtime_r,
475 1.14 christos 2015, 12, 6, 14, 43, 16);
476 1.14 christos parsecheck("Mon Dec 7 03:13:31 ICT 2015", NULL, NULL, gmtime_r,
477 1.14 christos 2015, 12, 6, 20, 13, 31);
478 1.14 christos /* the day name is ignored when a day of month (etc) is given... */
479 1.14 christos parsecheck("Sat Dec 7 03:13:31 ICT 2015", NULL, NULL, gmtime_r,
480 1.14 christos 2015, 12, 6, 20, 13, 31);
481 1.14 christos
482 1.14 christos
483 1.14 christos parsecheck("2015-12-06 12:00:00 IDLW", NULL, NULL, gmtime_r,
484 1.14 christos 2015, 12, 7, 0, 0, 0);
485 1.14 christos parsecheck("2015-12-06 12:00:00 IDLE", NULL, NULL, gmtime_r,
486 1.14 christos 2015, 12, 6, 0, 0, 0);
487 1.14 christos
488 1.14 christos parsecheck("2015-12-06 21:17:33 NFT", NULL, NULL, gmtime_r,
489 1.14 christos 2015, 12, 7, 0, 47, 33);
490 1.14 christos parsecheck("2015-12-06 21:17:33 ACST", NULL, NULL, gmtime_r,
491 1.14 christos 2015, 12, 6, 11, 47, 33);
492 1.14 christos parsecheck("2015-12-06 21:17:33 +0717", NULL, NULL, gmtime_r,
493 1.14 christos 2015, 12, 6, 14, 0, 33);
494 1.14 christos
495 1.14 christos parsecheck("2015-12-06 21:21:21 Z", NULL, NULL, gmtime_r,
496 1.14 christos 2015, 12, 6, 21, 21, 21);
497 1.14 christos parsecheck("2015-12-06 21:21:21 A", NULL, NULL, gmtime_r,
498 1.14 christos 2015, 12, 6, 22, 21, 21);
499 1.14 christos parsecheck("2015-12-06 21:21:21 G", NULL, NULL, gmtime_r,
500 1.14 christos 2015, 12, 7, 4, 21, 21);
501 1.14 christos parsecheck("2015-12-06 21:21:21 M", NULL, NULL, gmtime_r,
502 1.14 christos 2015, 12, 7, 9, 21, 21);
503 1.14 christos parsecheck("2015-12-06 21:21:21 N", NULL, NULL, gmtime_r,
504 1.14 christos 2015, 12, 6, 20, 21, 21);
505 1.14 christos parsecheck("2015-12-06 21:21:21 T", NULL, NULL, gmtime_r,
506 1.14 christos 2015, 12, 6, 14, 21, 21);
507 1.14 christos parsecheck("2015-12-06 21:21:21 Y", NULL, NULL, gmtime_r,
508 1.14 christos 2015, 12, 6, 9, 21, 21);
509 1.14 christos
510 1.14 christos }
511 1.14 christos
512 1.14 christos ATF_TC(gibberish);
513 1.14 christos
514 1.14 christos ATF_TC_HEAD(gibberish, tc)
515 1.14 christos {
516 1.14 christos atf_tc_set_md_var(tc, "descr", "Test (not) parsing nonsense");
517 1.14 christos }
518 1.14 christos
519 1.14 christos ATF_TC_BODY(gibberish, tc)
520 1.14 christos {
521 1.14 christos errno = 0;
522 1.14 christos ATF_CHECK(parsedate("invalid nonsense", NULL, NULL) == (time_t)-1
523 1.14 christos && errno != 0);
524 1.14 christos errno = 0;
525 1.14 christos ATF_CHECK(parsedate("12th day of Christmas", NULL, NULL) == (time_t)-1
526 1.14 christos && errno != 0);
527 1.14 christos errno = 0;
528 1.14 christos ATF_CHECK(parsedate("2015-31-07 15:00", NULL, NULL) == (time_t)-1
529 1.14 christos && errno != 0);
530 1.14 christos errno = 0;
531 1.14 christos ATF_CHECK(parsedate("2015-02-29 10:01", NULL, NULL) == (time_t)-1
532 1.14 christos && errno != 0);
533 1.14 christos errno = 0;
534 1.14 christos ATF_CHECK(parsedate("2015-12-06 24:01", NULL, NULL) == (time_t)-1
535 1.14 christos && errno != 0);
536 1.14 christos errno = 0;
537 1.14 christos ATF_CHECK(parsedate("2015-12-06 14:61", NULL, NULL) == (time_t)-1
538 1.14 christos && errno != 0);
539 1.14 christos }
540 1.14 christos
541 1.1 njoly ATF_TP_ADD_TCS(tp)
542 1.1 njoly {
543 1.1 njoly ATF_TP_ADD_TC(tp, dates);
544 1.1 njoly ATF_TP_ADD_TC(tp, times);
545 1.11 apb ATF_TP_ADD_TC(tp, dsttimes);
546 1.1 njoly ATF_TP_ADD_TC(tp, relative);
547 1.6 apb ATF_TP_ADD_TC(tp, atsecs);
548 1.14 christos ATF_TP_ADD_TC(tp, zones);
549 1.14 christos ATF_TP_ADD_TC(tp, gibberish);
550 1.1 njoly
551 1.1 njoly return atf_no_error();
552 1.1 njoly }
553 1.14 christos
554