2 * Copyright (c) 1999 - 2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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. */
36 #include "strpftime-test.h"
39 static const char *abb_weekdays[] = {
49 static const char *full_weekdays[] = {
59 static const char *abb_month[] = {
74 static const char *full_month[] = {
89 static const char *ampm[] = {
95 * Convert hour in [0, 24] to [12 1 - 11 12 1 - 11 12]
99 hour_24to12 (int hour)
109 * Return AM or PM for `hour'
113 hour_to_ampm (int hour)
115 return ampm[hour / 12];
119 * Return the week number of `tm' (Sunday being the first day of the week)
124 week_number_sun (const struct tm *tm)
126 return (tm->tm_yday + 7 - (tm->tm_yday % 7 - tm->tm_wday + 7) % 7) / 7;
130 * Return the week number of `tm' (Monday being the first day of the week)
135 week_number_mon (const struct tm *tm)
137 int wday = (tm->tm_wday + 6) % 7;
139 return (tm->tm_yday + 7 - (tm->tm_yday % 7 - wday + 7) % 7) / 7;
143 * Return the week number of `tm' (Monday being the first day of the
144 * week) as [01, 53]. Week number one is the one that has four or more
149 week_number_mon4 (const struct tm *tm)
151 int wday = (tm->tm_wday + 6) % 7;
152 int w1day = (wday - tm->tm_yday % 7 + 7) % 7;
155 ret = (tm->tm_yday + w1day) / 7;
169 ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL
170 strftime (char *buf, size_t maxsize, const char *format,
176 while (*format != '\0' && n < maxsize) {
177 if (*format == '%') {
179 if(*format == 'E' || *format == 'O')
183 ret = snprintf (buf, maxsize - n,
184 "%s", abb_weekdays[tm->tm_wday]);
187 ret = snprintf (buf, maxsize - n,
188 "%s", full_weekdays[tm->tm_wday]);
192 ret = snprintf (buf, maxsize - n,
193 "%s", abb_month[tm->tm_mon]);
196 ret = snprintf (buf, maxsize - n,
197 "%s", full_month[tm->tm_mon]);
200 ret = snprintf (buf, maxsize - n,
201 "%d:%02d:%02d %02d:%02d:%02d",
210 ret = snprintf (buf, maxsize - n,
211 "%02d", (tm->tm_year + 1900) / 100);
214 ret = snprintf (buf, maxsize - n,
215 "%02d", tm->tm_mday);
218 ret = snprintf (buf, maxsize - n,
222 (tm->tm_year + 1900) % 100);
225 ret = snprintf (buf, maxsize - n,
229 ret = snprintf (buf, maxsize - n,
230 "%04d-%02d-%02d", tm->tm_year + 1900,
231 tm->tm_mon + 1, tm->tm_mday);
234 /* last two digits of week-based year */
237 /* week-based year */
240 ret = snprintf (buf, maxsize - n,
241 "%02d", tm->tm_hour);
244 ret = snprintf (buf, maxsize - n,
246 hour_24to12 (tm->tm_hour));
249 ret = snprintf (buf, maxsize - n,
250 "%03d", tm->tm_yday + 1);
253 ret = snprintf (buf, maxsize - n,
257 ret = snprintf (buf, maxsize - n,
259 hour_24to12 (tm->tm_hour));
262 ret = snprintf (buf, maxsize - n,
263 "%02d", tm->tm_mon + 1);
266 ret = snprintf (buf, maxsize - n,
270 ret = snprintf (buf, maxsize - n, "\n");
273 ret = snprintf (buf, maxsize - n, "%s",
274 hour_to_ampm (tm->tm_hour));
277 ret = snprintf (buf, maxsize - n,
279 hour_24to12 (tm->tm_hour),
282 hour_to_ampm (tm->tm_hour));
285 ret = snprintf (buf, maxsize - n,
291 ret = snprintf (buf, maxsize - n,
292 "%d", (int)mktime(rk_UNCONST(tm)));
295 ret = snprintf (buf, maxsize - n,
299 ret = snprintf (buf, maxsize - n, "\t");
303 ret = snprintf (buf, maxsize - n,
310 ret = snprintf (buf, maxsize - n,
311 "%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
314 ret = snprintf (buf, maxsize - n,
315 "%02d", week_number_sun (tm));
318 ret = snprintf (buf, maxsize - n,
319 "%02d", week_number_mon4 (tm));
322 ret = snprintf (buf, maxsize - n,
326 ret = snprintf (buf, maxsize - n,
327 "%02d", week_number_mon (tm));
330 ret = snprintf (buf, maxsize - n,
337 ret = snprintf (buf, maxsize - n,
338 "%02d", (tm->tm_year + 1900) % 100);
341 ret = snprintf (buf, maxsize - n,
342 "%d", tm->tm_year + 1900);
345 ret = snprintf (buf, maxsize - n,
347 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
349 #elif defined(HAVE_TIMEZONE)
356 #error Where in timezone chaos are you?
361 ret = snprintf (buf, maxsize - n,
364 #if defined(HAVE_STRUCT_TM_TM_ZONE)
366 #elif defined(HAVE_TIMEZONE)
377 ret = snprintf (buf, maxsize - n,
381 ret = snprintf (buf, maxsize - n,
385 if (ret < 0 || ret >= (int)(maxsize - n))