1 /* $NetBSD: strftime.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
4 * Copyright (c) 1999 - 2002 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * 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.
19 * 3. Neither the name of KTH nor the names of its contributors may be
20 * used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 #include <krb5/roken.h>
38 #include "strpftime-test.h"
41 static const char *abb_weekdays
[] = {
51 static const char *full_weekdays
[] = {
61 static const char *abb_month
[] = {
76 static const char *full_month
[] = {
91 static const char *ampm
[] = {
97 * Convert hour in [0, 24] to [12 1 - 11 12 1 - 11 12]
101 hour_24to12 (int hour
)
111 * Return AM or PM for `hour'
115 hour_to_ampm (int hour
)
117 return ampm
[hour
/ 12];
121 * Return the week number of `tm' (Sunday being the first day of the week)
126 week_number_sun (const struct tm
*tm
)
128 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - tm
->tm_wday
+ 7) % 7) / 7;
132 * Return the week number of `tm' (Monday being the first day of the week)
137 week_number_mon (const struct tm
*tm
)
139 int wday
= (tm
->tm_wday
+ 6) % 7;
141 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - wday
+ 7) % 7) / 7;
145 * Return the week number of `tm' (Monday being the first day of the
146 * week) as [01, 53]. Week number one is the one that has four or more
151 week_number_mon4 (const struct tm
*tm
)
153 int wday
= (tm
->tm_wday
+ 6) % 7;
154 int w1day
= (wday
- tm
->tm_yday
% 7 + 7) % 7;
157 ret
= (tm
->tm_yday
+ w1day
) / 7;
171 ROKEN_LIB_FUNCTION
size_t ROKEN_LIB_CALL
172 strftime (char *buf
, size_t maxsize
, const char *format
,
178 while (*format
!= '\0' && n
< maxsize
) {
179 if (*format
== '%') {
181 if(*format
== 'E' || *format
== 'O')
185 ret
= snprintf (buf
, maxsize
- n
,
186 "%s", abb_weekdays
[tm
->tm_wday
]);
189 ret
= snprintf (buf
, maxsize
- n
,
190 "%s", full_weekdays
[tm
->tm_wday
]);
194 ret
= snprintf (buf
, maxsize
- n
,
195 "%s", abb_month
[tm
->tm_mon
]);
198 ret
= snprintf (buf
, maxsize
- n
,
199 "%s", full_month
[tm
->tm_mon
]);
202 ret
= snprintf (buf
, maxsize
- n
,
203 "%d:%02d:%02d %02d:%02d:%02d",
212 ret
= snprintf (buf
, maxsize
- n
,
213 "%02d", (tm
->tm_year
+ 1900) / 100);
216 ret
= snprintf (buf
, maxsize
- n
,
217 "%02d", tm
->tm_mday
);
220 ret
= snprintf (buf
, maxsize
- n
,
224 (tm
->tm_year
+ 1900) % 100);
227 ret
= snprintf (buf
, maxsize
- n
,
231 ret
= snprintf (buf
, maxsize
- n
,
232 "%04d-%02d-%02d", tm
->tm_year
+ 1900,
233 tm
->tm_mon
+ 1, tm
->tm_mday
);
236 /* last two digits of week-based year */
239 /* week-based year */
242 ret
= snprintf (buf
, maxsize
- n
,
243 "%02d", tm
->tm_hour
);
246 ret
= snprintf (buf
, maxsize
- n
,
248 hour_24to12 (tm
->tm_hour
));
251 ret
= snprintf (buf
, maxsize
- n
,
252 "%03d", tm
->tm_yday
+ 1);
255 ret
= snprintf (buf
, maxsize
- n
,
259 ret
= snprintf (buf
, maxsize
- n
,
261 hour_24to12 (tm
->tm_hour
));
264 ret
= snprintf (buf
, maxsize
- n
,
265 "%02d", tm
->tm_mon
+ 1);
268 ret
= snprintf (buf
, maxsize
- n
,
272 ret
= snprintf (buf
, maxsize
- n
, "\n");
275 ret
= snprintf (buf
, maxsize
- n
, "%s",
276 hour_to_ampm (tm
->tm_hour
));
279 ret
= snprintf (buf
, maxsize
- n
,
281 hour_24to12 (tm
->tm_hour
),
284 hour_to_ampm (tm
->tm_hour
));
287 ret
= snprintf (buf
, maxsize
- n
,
293 ret
= snprintf (buf
, maxsize
- n
,
294 "%d", (int)mktime(rk_UNCONST(tm
)));
297 ret
= snprintf (buf
, maxsize
- n
,
301 ret
= snprintf (buf
, maxsize
- n
, "\t");
305 ret
= snprintf (buf
, maxsize
- n
,
312 ret
= snprintf (buf
, maxsize
- n
,
313 "%d", (tm
->tm_wday
== 0) ? 7 : tm
->tm_wday
);
316 ret
= snprintf (buf
, maxsize
- n
,
317 "%02d", week_number_sun (tm
));
320 ret
= snprintf (buf
, maxsize
- n
,
321 "%02d", week_number_mon4 (tm
));
324 ret
= snprintf (buf
, maxsize
- n
,
328 ret
= snprintf (buf
, maxsize
- n
,
329 "%02d", week_number_mon (tm
));
332 ret
= snprintf (buf
, maxsize
- n
,
339 ret
= snprintf (buf
, maxsize
- n
,
340 "%02d", (tm
->tm_year
+ 1900) % 100);
343 ret
= snprintf (buf
, maxsize
- n
,
344 "%d", tm
->tm_year
+ 1900);
347 ret
= snprintf (buf
, maxsize
- n
,
349 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
351 #elif defined(HAVE_TIMEZONE)
358 #error Where in timezone chaos are you?
363 ret
= snprintf (buf
, maxsize
- n
,
366 #if defined(HAVE_STRUCT_TM_TM_ZONE)
368 #elif defined(HAVE_TIMEZONE)
379 ret
= snprintf (buf
, maxsize
- n
,
383 ret
= snprintf (buf
, maxsize
- n
,
387 if (ret
< 0 || ret
>= (int)(maxsize
- n
))