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"
40 # define timezone _timezone
41 # define tzname _tzname
44 static const char *abb_weekdays
[] = {
54 static const char *full_weekdays
[] = {
64 static const char *abb_month
[] = {
79 static const char *full_month
[] = {
94 static const char *ampm
[] = {
100 * Convert hour in [0, 24] to [12 1 - 11 12 1 - 11 12]
104 hour_24to12 (int hour
)
114 * Return AM or PM for `hour'
118 hour_to_ampm (int hour
)
120 return ampm
[hour
/ 12];
124 * Return the week number of `tm' (Sunday being the first day of the week)
129 week_number_sun (const struct tm
*tm
)
131 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - tm
->tm_wday
+ 7) % 7) / 7;
135 * Return the week number of `tm' (Monday being the first day of the week)
140 week_number_mon (const struct tm
*tm
)
142 int wday
= (tm
->tm_wday
+ 6) % 7;
144 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - wday
+ 7) % 7) / 7;
148 * Return the week number of `tm' (Monday being the first day of the
149 * week) as [01, 53]. Week number one is the one that has four or more
154 week_number_mon4 (const struct tm
*tm
)
156 int wday
= (tm
->tm_wday
+ 6) % 7;
157 int w1day
= (wday
- tm
->tm_yday
% 7 + 7) % 7;
160 ret
= (tm
->tm_yday
+ w1day
) / 7;
174 ROKEN_LIB_FUNCTION
size_t ROKEN_LIB_CALL
175 strftime (char *buf
, size_t maxsize
, const char *format
,
181 while (*format
!= '\0' && n
< maxsize
) {
182 if (*format
== '%') {
184 if(*format
== 'E' || *format
== 'O')
188 ret
= snprintf (buf
, maxsize
- n
,
189 "%s", abb_weekdays
[tm
->tm_wday
]);
192 ret
= snprintf (buf
, maxsize
- n
,
193 "%s", full_weekdays
[tm
->tm_wday
]);
197 ret
= snprintf (buf
, maxsize
- n
,
198 "%s", abb_month
[tm
->tm_mon
]);
201 ret
= snprintf (buf
, maxsize
- n
,
202 "%s", full_month
[tm
->tm_mon
]);
205 ret
= snprintf (buf
, maxsize
- n
,
206 "%d:%02d:%02d %02d:%02d:%02d",
215 ret
= snprintf (buf
, maxsize
- n
,
216 "%02d", (tm
->tm_year
+ 1900) / 100);
219 ret
= snprintf (buf
, maxsize
- n
,
220 "%02d", tm
->tm_mday
);
223 ret
= snprintf (buf
, maxsize
- n
,
227 (tm
->tm_year
+ 1900) % 100);
230 ret
= snprintf (buf
, maxsize
- n
,
234 ret
= snprintf (buf
, maxsize
- n
,
235 "%04d-%02d-%02d", tm
->tm_year
+ 1900,
236 tm
->tm_mon
+ 1, tm
->tm_mday
);
239 /* last two digits of week-based year */
242 /* week-based year */
245 ret
= snprintf (buf
, maxsize
- n
,
246 "%02d", tm
->tm_hour
);
249 ret
= snprintf (buf
, maxsize
- n
,
251 hour_24to12 (tm
->tm_hour
));
254 ret
= snprintf (buf
, maxsize
- n
,
255 "%03d", tm
->tm_yday
+ 1);
258 ret
= snprintf (buf
, maxsize
- n
,
262 ret
= snprintf (buf
, maxsize
- n
,
264 hour_24to12 (tm
->tm_hour
));
267 ret
= snprintf (buf
, maxsize
- n
,
268 "%02d", tm
->tm_mon
+ 1);
271 ret
= snprintf (buf
, maxsize
- n
,
275 ret
= snprintf (buf
, maxsize
- n
, "\n");
278 ret
= snprintf (buf
, maxsize
- n
, "%s",
279 hour_to_ampm (tm
->tm_hour
));
282 ret
= snprintf (buf
, maxsize
- n
,
284 hour_24to12 (tm
->tm_hour
),
287 hour_to_ampm (tm
->tm_hour
));
290 ret
= snprintf (buf
, maxsize
- n
,
296 ret
= snprintf (buf
, maxsize
- n
,
297 "%d", (int)mktime(rk_UNCONST(tm
)));
300 ret
= snprintf (buf
, maxsize
- n
,
304 ret
= snprintf (buf
, maxsize
- n
, "\t");
308 ret
= snprintf (buf
, maxsize
- n
,
315 ret
= snprintf (buf
, maxsize
- n
,
316 "%d", (tm
->tm_wday
== 0) ? 7 : tm
->tm_wday
);
319 ret
= snprintf (buf
, maxsize
- n
,
320 "%02d", week_number_sun (tm
));
323 ret
= snprintf (buf
, maxsize
- n
,
324 "%02d", week_number_mon4 (tm
));
327 ret
= snprintf (buf
, maxsize
- n
,
331 ret
= snprintf (buf
, maxsize
- n
,
332 "%02d", week_number_mon (tm
));
335 ret
= snprintf (buf
, maxsize
- n
,
342 ret
= snprintf (buf
, maxsize
- n
,
343 "%02d", (tm
->tm_year
+ 1900) % 100);
346 ret
= snprintf (buf
, maxsize
- n
,
347 "%d", tm
->tm_year
+ 1900);
350 ret
= snprintf (buf
, maxsize
- n
,
352 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
354 #elif defined(HAVE_TIMEZONE)
361 #error Where in timezone chaos are you?
366 ret
= snprintf (buf
, maxsize
- n
,
369 #if defined(HAVE_STRUCT_TM_TM_ZONE)
371 #elif defined(HAVE_TIMEZONE)
383 ret
= snprintf (buf
, maxsize
- n
,
387 ret
= snprintf (buf
, maxsize
- n
,
391 if (ret
< 0 || ret
>= (int)(maxsize
- n
))