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. */
37 #include "strpftime-test.h"
43 static const char *abb_weekdays
[] = {
53 static const char *full_weekdays
[] = {
63 static const char *abb_month
[] = {
78 static const char *full_month
[] = {
93 static const char *ampm
[] = {
99 * Convert hour in [0, 24] to [12 1 - 11 12 1 - 11 12]
103 hour_24to12 (int hour
)
113 * Return AM or PM for `hour'
117 hour_to_ampm (int hour
)
119 return ampm
[hour
/ 12];
123 * Return the week number of `tm' (Sunday being the first day of the week)
128 week_number_sun (const struct tm
*tm
)
130 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - tm
->tm_wday
+ 7) % 7) / 7;
134 * Return the week number of `tm' (Monday being the first day of the week)
139 week_number_mon (const struct tm
*tm
)
141 int wday
= (tm
->tm_wday
+ 6) % 7;
143 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - wday
+ 7) % 7) / 7;
147 * Return the week number of `tm' (Monday being the first day of the
148 * week) as [01, 53]. Week number one is the one that has four or more
153 week_number_mon4 (const struct tm
*tm
)
155 int wday
= (tm
->tm_wday
+ 6) % 7;
156 int w1day
= (wday
- tm
->tm_yday
% 7 + 7) % 7;
159 ret
= (tm
->tm_yday
+ w1day
) / 7;
173 size_t ROKEN_LIB_FUNCTION
174 strftime (char *buf
, size_t maxsize
, const char *format
,
180 while (*format
!= '\0' && n
< maxsize
) {
181 if (*format
== '%') {
183 if(*format
== 'E' || *format
== 'O')
187 ret
= snprintf (buf
, maxsize
- n
,
188 "%s", abb_weekdays
[tm
->tm_wday
]);
191 ret
= snprintf (buf
, maxsize
- n
,
192 "%s", full_weekdays
[tm
->tm_wday
]);
196 ret
= snprintf (buf
, maxsize
- n
,
197 "%s", abb_month
[tm
->tm_mon
]);
200 ret
= snprintf (buf
, maxsize
- n
,
201 "%s", full_month
[tm
->tm_mon
]);
204 ret
= snprintf (buf
, maxsize
- n
,
205 "%d:%02d:%02d %02d:%02d:%02d",
214 ret
= snprintf (buf
, maxsize
- n
,
215 "%02d", (tm
->tm_year
+ 1900) / 100);
218 ret
= snprintf (buf
, maxsize
- n
,
219 "%02d", tm
->tm_mday
);
222 ret
= snprintf (buf
, maxsize
- n
,
226 (tm
->tm_year
+ 1900) % 100);
229 ret
= snprintf (buf
, maxsize
- n
,
233 ret
= snprintf (buf
, maxsize
- n
,
234 "%04d-%02d-%02d", tm
->tm_year
+ 1900,
235 tm
->tm_mon
+ 1, tm
->tm_mday
);
238 /* last two digits of week-based year */
241 /* week-based year */
244 ret
= snprintf (buf
, maxsize
- n
,
245 "%02d", tm
->tm_hour
);
248 ret
= snprintf (buf
, maxsize
- n
,
250 hour_24to12 (tm
->tm_hour
));
253 ret
= snprintf (buf
, maxsize
- n
,
254 "%03d", tm
->tm_yday
+ 1);
257 ret
= snprintf (buf
, maxsize
- n
,
261 ret
= snprintf (buf
, maxsize
- n
,
263 hour_24to12 (tm
->tm_hour
));
266 ret
= snprintf (buf
, maxsize
- n
,
267 "%02d", tm
->tm_mon
+ 1);
270 ret
= snprintf (buf
, maxsize
- n
,
274 ret
= snprintf (buf
, maxsize
- n
, "\n");
277 ret
= snprintf (buf
, maxsize
- n
, "%s",
278 hour_to_ampm (tm
->tm_hour
));
281 ret
= snprintf (buf
, maxsize
- n
,
283 hour_24to12 (tm
->tm_hour
),
286 hour_to_ampm (tm
->tm_hour
));
289 ret
= snprintf (buf
, maxsize
- n
,
295 ret
= snprintf (buf
, maxsize
- n
,
296 "%d", (int)mktime(rk_UNCONST(tm
)));
299 ret
= snprintf (buf
, maxsize
- n
,
303 ret
= snprintf (buf
, maxsize
- n
, "\t");
307 ret
= snprintf (buf
, maxsize
- n
,
314 ret
= snprintf (buf
, maxsize
- n
,
315 "%d", (tm
->tm_wday
== 0) ? 7 : tm
->tm_wday
);
318 ret
= snprintf (buf
, maxsize
- n
,
319 "%02d", week_number_sun (tm
));
322 ret
= snprintf (buf
, maxsize
- n
,
323 "%02d", week_number_mon4 (tm
));
326 ret
= snprintf (buf
, maxsize
- n
,
330 ret
= snprintf (buf
, maxsize
- n
,
331 "%02d", week_number_mon (tm
));
334 ret
= snprintf (buf
, maxsize
- n
,
341 ret
= snprintf (buf
, maxsize
- n
,
342 "%02d", (tm
->tm_year
+ 1900) % 100);
345 ret
= snprintf (buf
, maxsize
- n
,
346 "%d", tm
->tm_year
+ 1900);
349 ret
= snprintf (buf
, maxsize
- n
,
351 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
353 #elif defined(HAVE_TIMEZONE)
360 #error Where in timezone chaos are you?
365 ret
= snprintf (buf
, maxsize
- n
,
368 #if defined(HAVE_STRUCT_TM_TM_ZONE)
370 #elif defined(HAVE_TIMEZONE)
381 ret
= snprintf (buf
, maxsize
- n
,
385 ret
= snprintf (buf
, maxsize
- n
,
389 if (ret
< 0 || ret
>= maxsize
- n
)