1/*
2 * Copyright (c) 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 *    used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32
33#include <config.h>
34#include <roken.h>
35#ifdef TEST_STRPFTIME
36#include "strpftime-test.h"
37#endif
38
39enum { MAXSIZE = 26 };
40
41static struct testcase {
42    time_t t;
43    struct {
44	const char *format;
45	const char *result;
46    } vals[MAXSIZE];
47} tests[] = {
48    {0,
49     {
50	 {"%A", "Thursday"},
51	 {"%a", "Thu"},
52	 {"%B", "January"},
53	 {"%b", "Jan"},
54	 {"%C", "19"},
55	 {"%d", "01"},
56	 {"%e", " 1"},
57	 {"%H", "00"},
58	 {"%I", "12"},
59	 {"%j", "001"},
60	 {"%k", " 0"},
61	 {"%l", "12"},
62	 {"%M", "00"},
63	 {"%m", "01"},
64	 {"%n", "\n"},
65	 {"%p", "AM"},
66	 {"%S", "00"},
67	 {"%t", "\t"},
68	 {"%w", "4"},
69	 {"%Y", "1970"},
70	 {"%y", "70"},
71	 {"%U", "00"},
72	 {"%W", "00"},
73	 {"%V", "01"},
74	 {"%%", "%"},
75	 {NULL, NULL}}
76    },
77    {90000,
78     {
79	 {"%A", "Friday"},
80	 {"%a", "Fri"},
81	 {"%B", "January"},
82	 {"%b", "Jan"},
83	 {"%C", "19"},
84	 {"%d", "02"},
85	 {"%e", " 2"},
86	 {"%H", "01"},
87	 {"%I", "01"},
88	 {"%j", "002"},
89	 {"%k", " 1"},
90	 {"%l", " 1"},
91	 {"%M", "00"},
92	 {"%m", "01"},
93	 {"%n", "\n"},
94	 {"%p", "AM"},
95	 {"%S", "00"},
96	 {"%t", "\t"},
97	 {"%w", "5"},
98	 {"%Y", "1970"},
99	 {"%y", "70"},
100	 {"%U", "00"},
101	 {"%W", "00"},
102	 {"%V", "01"},
103	 {"%%", "%"},
104	 {NULL, NULL}
105     }
106    },
107    {216306,
108     {
109	 {"%A", "Saturday"},
110	 {"%a", "Sat"},
111	 {"%B", "January"},
112	 {"%b", "Jan"},
113	 {"%C", "19"},
114	 {"%d", "03"},
115	 {"%e", " 3"},
116	 {"%H", "12"},
117	 {"%I", "12"},
118	 {"%j", "003"},
119	 {"%k", "12"},
120	 {"%l", "12"},
121	 {"%M", "05"},
122	 {"%m", "01"},
123	 {"%n", "\n"},
124	 {"%p", "PM"},
125	 {"%S", "06"},
126	 {"%t", "\t"},
127	 {"%w", "6"},
128	 {"%Y", "1970"},
129	 {"%y", "70"},
130	 {"%U", "00"},
131	 {"%W", "00"},
132	 {"%V", "01"},
133	 {"%%", "%"},
134	 {NULL, NULL}
135     }
136    },
137    {259200,
138     {
139	 {"%A", "Sunday"},
140	 {"%a", "Sun"},
141	 {"%B", "January"},
142	 {"%b", "Jan"},
143	 {"%C", "19"},
144	 {"%d", "04"},
145	 {"%e", " 4"},
146	 {"%H", "00"},
147	 {"%I", "12"},
148	 {"%j", "004"},
149	 {"%k", " 0"},
150	 {"%l", "12"},
151	 {"%M", "00"},
152	 {"%m", "01"},
153	 {"%n", "\n"},
154	 {"%p", "AM"},
155	 {"%S", "00"},
156	 {"%t", "\t"},
157	 {"%w", "0"},
158	 {"%Y", "1970"},
159	 {"%y", "70"},
160	 {"%U", "01"},
161	 {"%W", "00"},
162	 {"%V", "01"},
163	 {"%%", "%"},
164	 {NULL, NULL}
165     }
166    },
167    {915148800,
168     {
169	 {"%A", "Friday"},
170	 {"%a", "Fri"},
171	 {"%B", "January"},
172	 {"%b", "Jan"},
173	 {"%C", "19"},
174	 {"%d", "01"},
175	 {"%e", " 1"},
176	 {"%H", "00"},
177	 {"%I", "12"},
178	 {"%j", "001"},
179	 {"%k", " 0"},
180	 {"%l", "12"},
181	 {"%M", "00"},
182	 {"%m", "01"},
183	 {"%n", "\n"},
184	 {"%p", "AM"},
185	 {"%S", "00"},
186	 {"%t", "\t"},
187	 {"%w", "5"},
188	 {"%Y", "1999"},
189	 {"%y", "99"},
190	 {"%U", "00"},
191	 {"%W", "00"},
192	 {"%V", "53"},
193	 {"%%", "%"},
194	 {NULL, NULL}}
195    },
196    {942161105,
197     {
198
199	 {"%A", "Tuesday"},
200	 {"%a", "Tue"},
201	 {"%B", "November"},
202	 {"%b", "Nov"},
203	 {"%C", "19"},
204	 {"%d", "09"},
205	 {"%e", " 9"},
206	 {"%H", "15"},
207	 {"%I", "03"},
208	 {"%j", "313"},
209	 {"%k", "15"},
210	 {"%l", " 3"},
211	 {"%M", "25"},
212	 {"%m", "11"},
213	 {"%n", "\n"},
214	 {"%p", "PM"},
215	 {"%S", "05"},
216	 {"%t", "\t"},
217	 {"%w", "2"},
218	 {"%Y", "1999"},
219	 {"%y", "99"},
220	 {"%U", "45"},
221	 {"%W", "45"},
222	 {"%V", "45"},
223	 {"%%", "%"},
224	 {NULL, NULL}
225     }
226    }
227};
228
229int
230main(int argc, char **argv)
231{
232    int i, j;
233    int ret = 0;
234
235    for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
236	struct tm *tm;
237
238	tm = gmtime (&tests[i].t);
239
240	for (j = 0; tests[i].vals[j].format != NULL; ++j) {
241	    char buf[128];
242	    size_t len;
243	    struct tm tm2;
244	    char *ptr;
245
246	    len = strftime (buf, sizeof(buf), tests[i].vals[j].format, tm);
247	    if (len != strlen (buf)) {
248		printf ("length of strftime(\"%s\") = %lu (\"%s\")\n",
249			tests[i].vals[j].format, (unsigned long)len,
250			buf);
251		++ret;
252		continue;
253	    }
254	    if (strcmp (buf, tests[i].vals[j].result) != 0) {
255		printf ("result of strftime(\"%s\") = \"%s\" != \"%s\"\n",
256			tests[i].vals[j].format, buf,
257			tests[i].vals[j].result);
258		++ret;
259		continue;
260	    }
261	    memset (&tm2, 0, sizeof(tm2));
262	    ptr = strptime (tests[i].vals[j].result,
263			    tests[i].vals[j].format,
264			    &tm2);
265	    if (ptr == NULL || *ptr != '\0') {
266		printf ("bad return value from strptime("
267			"\"%s\", \"%s\")\n",
268			tests[i].vals[j].result,
269			tests[i].vals[j].format);
270		++ret;
271	    }
272	    strftime (buf, sizeof(buf), tests[i].vals[j].format, &tm2);
273	    if (strcmp (buf, tests[i].vals[j].result) != 0) {
274		printf ("reverse of \"%s\" failed: \"%s\" vs \"%s\"\n",
275			tests[i].vals[j].format,
276			buf, tests[i].vals[j].result);
277		++ret;
278	    }
279	}
280    }
281    {
282	struct tm tm;
283	memset(&tm, 0, sizeof(tm));
284	strptime ("200505", "%Y%m", &tm);
285	if (tm.tm_year != 105)
286	    ++ret;
287	if (tm.tm_mon != 4)
288	    ++ret;
289    }
290    if (ret) {
291	printf ("%d errors\n", ret);
292	return 1;
293    } else
294	return 0;
295}
296