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. */
38 RCSID("$Id: strftime.c,v 1.13 2002/08/20 12:42:37 joda Exp $");
40 static const char *abb_weekdays
[] = {
50 static const char *full_weekdays
[] = {
60 static const char *abb_month
[] = {
75 static const char *full_month
[] = {
90 static const char *ampm
[] = {
96 * Convert hour in [0, 24] to [12 1 - 11 12 1 - 11 12]
100 hour_24to12 (int hour
)
110 * Return AM or PM for `hour'
114 hour_to_ampm (int hour
)
116 return ampm
[hour
/ 12];
120 * Return the week number of `tm' (Sunday being the first day of the week)
125 week_number_sun (const struct tm
*tm
)
127 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - tm
->tm_wday
+ 7) % 7) / 7;
131 * Return the week number of `tm' (Monday being the first day of the week)
136 week_number_mon (const struct tm
*tm
)
138 int wday
= (tm
->tm_wday
+ 6) % 7;
140 return (tm
->tm_yday
+ 7 - (tm
->tm_yday
% 7 - wday
+ 7) % 7) / 7;
144 * Return the week number of `tm' (Monday being the first day of the
145 * week) as [01, 53]. Week number one is the one that has four or more
150 week_number_mon4 (const struct tm
*tm
)
152 int wday
= (tm
->tm_wday
+ 6) % 7;
153 int w1day
= (wday
- tm
->tm_yday
% 7 + 7) % 7;
156 ret
= (tm
->tm_yday
+ w1day
) / 7;
171 strftime (char *buf
, size_t maxsize
, const char *format
,
177 while (*format
!= '\0' && n
< maxsize
) {
178 if (*format
== '%') {
180 if(*format
== 'E' || *format
== 'O')
184 ret
= snprintf (buf
, maxsize
- n
,
185 "%s", abb_weekdays
[tm
->tm_wday
]);
188 ret
= snprintf (buf
, maxsize
- n
,
189 "%s", full_weekdays
[tm
->tm_wday
]);
193 ret
= snprintf (buf
, maxsize
- n
,
194 "%s", abb_month
[tm
->tm_mon
]);
197 ret
= snprintf (buf
, maxsize
- n
,
198 "%s", full_month
[tm
->tm_mon
]);
201 ret
= snprintf (buf
, maxsize
- n
,
202 "%d:%02d:%02d %02d:%02d:%02d",
211 ret
= snprintf (buf
, maxsize
- n
,
212 "%02d", (tm
->tm_year
+ 1900) / 100);
215 ret
= snprintf (buf
, maxsize
- n
,
216 "%02d", tm
->tm_mday
);
219 ret
= snprintf (buf
, maxsize
- n
,
223 (tm
->tm_year
+ 1900) % 100);
226 ret
= snprintf (buf
, maxsize
- n
,
230 ret
= snprintf (buf
, maxsize
- n
,
231 "%04d-%02d-%02d", tm
->tm_year
+ 1900,
232 tm
->tm_mon
+ 1, tm
->tm_mday
);
235 /* last two digits of week-based year */
238 /* week-based year */
241 ret
= snprintf (buf
, maxsize
- n
,
242 "%02d", tm
->tm_hour
);
245 ret
= snprintf (buf
, maxsize
- n
,
247 hour_24to12 (tm
->tm_hour
));
250 ret
= snprintf (buf
, maxsize
- n
,
251 "%03d", tm
->tm_yday
+ 1);
254 ret
= snprintf (buf
, maxsize
- n
,
258 ret
= snprintf (buf
, maxsize
- n
,
260 hour_24to12 (tm
->tm_hour
));
263 ret
= snprintf (buf
, maxsize
- n
,
264 "%02d", tm
->tm_mon
+ 1);
267 ret
= snprintf (buf
, maxsize
- n
,
271 ret
= snprintf (buf
, maxsize
- n
, "\n");
274 ret
= snprintf (buf
, maxsize
- n
, "%s",
275 hour_to_ampm (tm
->tm_hour
));
278 ret
= snprintf (buf
, maxsize
- n
,
280 hour_24to12 (tm
->tm_hour
),
283 hour_to_ampm (tm
->tm_hour
));
286 ret
= snprintf (buf
, maxsize
- n
,
292 ret
= snprintf (buf
, maxsize
- n
,
293 "%d", (int)mktime((struct tm
*)tm
));
296 ret
= snprintf (buf
, maxsize
- n
,
300 ret
= snprintf (buf
, maxsize
- n
, "\t");
304 ret
= snprintf (buf
, maxsize
- n
,
311 ret
= snprintf (buf
, maxsize
- n
,
312 "%d", (tm
->tm_wday
== 0) ? 7 : tm
->tm_wday
);
315 ret
= snprintf (buf
, maxsize
- n
,
316 "%02d", week_number_sun (tm
));
319 ret
= snprintf (buf
, maxsize
- n
,
320 "%02d", week_number_mon4 (tm
));
323 ret
= snprintf (buf
, maxsize
- n
,
327 ret
= snprintf (buf
, maxsize
- n
,
328 "%02d", week_number_mon (tm
));
331 ret
= snprintf (buf
, maxsize
- n
,
338 ret
= snprintf (buf
, maxsize
- n
,
339 "%02d", (tm
->tm_year
+ 1900) % 100);
342 ret
= snprintf (buf
, maxsize
- n
,
343 "%d", tm
->tm_year
+ 1900);
346 ret
= snprintf (buf
, maxsize
- n
,
348 #if defined(HAVE_STRUCT_TM_TM_GMTOFF)
350 #elif defined(HAVE_TIMEZONE)
357 #error Where in timezone chaos are you?
362 ret
= snprintf (buf
, maxsize
- n
,
365 #if defined(HAVE_STRUCT_TM_TM_ZONE)
367 #elif defined(HAVE_TIMEZONE)
378 ret
= snprintf (buf
, maxsize
- n
,
382 ret
= snprintf (buf
, maxsize
- n
,
386 if (ret
< 0 || ret
>= maxsize
- n
)