2 * Copyright (c) 1989 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 * $FreeBSD: src/lib/libc/stdtime/strftime.c,v 1.25.2.4 2002/03/12 17:24:54 phantom Exp $
19 * $DragonFly: src/lib/libc/stdtime/strftime.c,v 1.4 2005/03/16 07:22:07 joerg Exp $
22 #include "namespace.h"
27 static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
28 #endif /* !defined lint */
29 #endif /* !defined LIBC_SCCS */
34 #include "un-namespace.h"
35 #include "timelocal.h"
37 static char * _add(const char *, char *, const char *);
38 static char * _conv(int, const char *, char *, const char *);
39 static char * _fmt(const char *, const struct tm *, char *, const char *);
42 strftime(char *const s, const size_t maxsize, const char * const format,
43 const struct tm * const t)
48 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
56 _fmt(const char *format, const struct tm * const t, char *pt,
57 const char * const ptlim)
59 int Ealternative, Oalternative;
60 struct lc_time_T *tptr = __get_current_time_locale();
62 for ( ; *format; ++format) {
72 pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
73 "?" : tptr->weekday[t->tm_wday],
77 pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
78 "?" : tptr->wday[t->tm_wday],
82 pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
83 "?" : (Oalternative ? tptr->alt_month :
84 tptr->month)[t->tm_mon],
89 pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
90 "?" : tptr->mon[t->tm_mon],
96 ** _fmt("%a %b %e %X %Y", t);
97 ** ...whereas now POSIX 1003.2 calls for
98 ** something completely different.
101 pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
105 pt = _fmt(tptr->c_fmt, t, pt, ptlim);
108 pt = _fmt("%m/%d/%y", t, pt, ptlim);
111 pt = _conv(t->tm_mday, "%02d", pt, ptlim);
114 if (Ealternative || Oalternative)
120 ** POSIX locale extensions, a la
121 ** Arnold Robbins' strftime version 3.0.
123 ** %Ec %EC %Ex %EX %Ey %EY
124 ** %Od %oe %OH %OI %Om %OM
125 ** %OS %Ou %OU %OV %Ow %OW %Oy
126 ** are supposed to provide alternate
130 ** FreeBSD extensions
133 if (Ealternative || Oalternative)
138 pt = _conv(t->tm_mday, "%2d", pt, ptlim);
141 pt = _fmt("%Y-%m-%d", t, pt, ptlim);
144 pt = _conv(t->tm_hour, "%02d", pt, ptlim);
147 pt = _conv((t->tm_hour % 12) ?
148 (t->tm_hour % 12) : 12,
152 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
156 ** This used to be...
157 ** _conv(t->tm_hour % 12 ?
158 ** t->tm_hour % 12 : 12, 2, ' ');
159 ** ...and has been changed to the below to
160 ** match SunOS 4.1.1 and Arnold Robbins'
161 ** strftime version 3.0. That is, "%k" and
162 ** "%l" have been swapped.
165 pt = _conv(t->tm_hour, "%2d", pt, ptlim);
170 ** After all this time, still unclaimed!
172 pt = _add("kitchen sink", pt, ptlim);
174 #endif /* defined KITCHEN_SINK */
177 ** This used to be...
178 ** _conv(t->tm_hour, 2, ' ');
179 ** ...and has been changed to the below to
180 ** match SunOS 4.1.1 and Arnold Robbin's
181 ** strftime version 3.0. That is, "%k" and
182 ** "%l" have been swapped.
185 pt = _conv((t->tm_hour % 12) ?
186 (t->tm_hour % 12) : 12,
190 pt = _conv(t->tm_min, "%02d", pt, ptlim);
193 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
196 pt = _add("\n", pt, ptlim);
199 pt = _add((t->tm_hour >= 12) ?
205 pt = _fmt("%H:%M", t, pt, ptlim);
208 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim);
211 pt = _conv(t->tm_sec, "%02d", pt, ptlim);
216 char buf[INT_STRLEN_MAXIMUM(
222 if (TYPE_SIGNED(time_t))
223 (void) sprintf(buf, "%ld",
225 else (void) sprintf(buf, "%lu",
226 (unsigned long) mkt);
227 pt = _add(buf, pt, ptlim);
231 pt = _fmt("%H:%M:%S", t, pt, ptlim);
234 pt = _add("\t", pt, ptlim);
237 pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7,
242 ** From Arnold Robbins' strftime version 3.0:
243 ** "ISO 8601: Weekday as a decimal number
247 pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday,
250 case 'V': /* ISO 8601 week number */
251 case 'G': /* ISO 8601 year (four digits) */
252 case 'g': /* ISO 8601 year (two digits) */
254 ** From Arnold Robbins' strftime version 3.0: "the week number of the
255 ** year (the first Monday as the first day of week 1) as a decimal number
259 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
260 ** "Week 01 of a year is per definition the first week which has the
261 ** Thursday in this year, which is equivalent to the week which contains
262 ** the fourth day of January. In other words, the first week of a new year
263 ** is the week which has the majority of its days in the new year. Week 01
264 ** might also contain days from the previous year and the week before week
265 ** 01 of a year is the last week (52 or 53) of the previous year even if
266 ** it contains days from the new year. A week starts with Monday (day 1)
267 ** and ends with Sunday (day 7). For example, the first week of the year
268 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
277 year = t->tm_year + TM_YEAR_BASE;
289 ** What yday (-3 ... 3) does
290 ** the ISO year begin on?
292 bot = ((yday + 11 - wday) %
295 ** What yday does the NEXT
296 ** ISO year begin on?
309 w = 1 + ((yday - bot) /
314 yday += isleap(year) ?
318 #ifdef XPG4_1994_04_09
320 && t->tm_mon == TM_JANUARY)
322 && t->tm_mon == TM_DECEMBER))
324 #endif /* defined XPG4_1994_04_09 */
326 pt = _conv(w, "%02d",
328 else if (*format == 'g') {
329 pt = _conv(year % 100, "%02d",
331 } else pt = _conv(year, "%04d",
337 ** From Arnold Robbins' strftime version 3.0:
338 ** "date as dd-bbb-YYYY"
341 pt = _fmt("%e-%b-%Y", t, pt, ptlim);
344 pt = _conv((t->tm_yday + 7 -
346 (t->tm_wday - 1) : 6)) / 7,
350 pt = _conv(t->tm_wday, "%d", pt, ptlim);
353 pt = _fmt(tptr->X_fmt, t, pt, ptlim);
356 pt = _fmt(tptr->x_fmt, t, pt, ptlim);
359 pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
363 pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
367 if (t->tm_zone != NULL)
368 pt = _add(t->tm_zone, pt, ptlim);
370 if (t->tm_isdst == 0 || t->tm_isdst == 1) {
371 pt = _add(tzname[t->tm_isdst],
373 } else pt = _add("?", pt, ptlim);
378 if (t->tm_gmtoff >= 0) {
379 absoff = t->tm_gmtoff;
380 pt = _add("+", pt, ptlim);
382 absoff = -t->tm_gmtoff;
383 pt = _add("-", pt, ptlim);
385 pt = _conv(absoff / 3600, "%02d",
387 pt = _conv((absoff % 3600) / 60, "%02d",
392 pt = _fmt(tptr->date_fmt, t, pt, ptlim);
396 * X311J/88-090 (4.12.3.5): if conversion char is
397 * undefined, behavior is undefined. Print out the
398 * character itself as printf(3) also does.
412 _conv(const int n, const char * const format, char * const pt,
413 const char * const ptlim)
415 char buf[INT_STRLEN_MAXIMUM(int) + 1];
417 (void) sprintf(buf, format, n);
418 return _add(buf, pt, ptlim);
422 _add(const char *str, char *pt, const char * const ptlim)
424 while (pt < ptlim && (*pt = *str++) != '\0')