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.2 2003/06/17 04:26:46 dillon Exp $
26 static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
27 #endif /* !defined lint */
28 #endif /* !defined LIBC_SCCS */
33 #include "timelocal.h"
35 static char * _add P((const char *, char *, const char *));
36 static char * _conv P((int, const char *, char *, const char *));
37 static char * _fmt P((const char *, const struct tm *, char *, const char *));
39 size_t strftime P((char *, size_t, const char *, const struct tm *));
41 extern char * tzname[];
44 strftime(s, maxsize, format, t)
47 const char *const format;
48 const struct tm *const t;
53 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
61 _fmt(format, t, pt, ptlim)
63 const struct tm *const t;
65 const char *const ptlim;
67 int Ealternative, Oalternative;
68 struct lc_time_T *tptr = __get_current_time_locale();
70 for ( ; *format; ++format) {
80 pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
81 "?" : tptr->weekday[t->tm_wday],
85 pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
86 "?" : tptr->wday[t->tm_wday],
90 pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
91 "?" : (Oalternative ? tptr->alt_month :
92 tptr->month)[t->tm_mon],
97 pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
98 "?" : tptr->mon[t->tm_mon],
103 ** %C used to do a...
104 ** _fmt("%a %b %e %X %Y", t);
105 ** ...whereas now POSIX 1003.2 calls for
106 ** something completely different.
109 pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
113 pt = _fmt(tptr->c_fmt, t, pt, ptlim);
116 pt = _fmt("%m/%d/%y", t, pt, ptlim);
119 pt = _conv(t->tm_mday, "%02d", pt, ptlim);
122 if (Ealternative || Oalternative)
128 ** POSIX locale extensions, a la
129 ** Arnold Robbins' strftime version 3.0.
131 ** %Ec %EC %Ex %EX %Ey %EY
132 ** %Od %oe %OH %OI %Om %OM
133 ** %OS %Ou %OU %OV %Ow %OW %Oy
134 ** are supposed to provide alternate
138 ** FreeBSD extensions
141 if (Ealternative || Oalternative)
146 pt = _conv(t->tm_mday, "%2d", pt, ptlim);
149 pt = _fmt("%Y-%m-%d", t, pt, ptlim);
152 pt = _conv(t->tm_hour, "%02d", pt, ptlim);
155 pt = _conv((t->tm_hour % 12) ?
156 (t->tm_hour % 12) : 12,
160 pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
164 ** This used to be...
165 ** _conv(t->tm_hour % 12 ?
166 ** t->tm_hour % 12 : 12, 2, ' ');
167 ** ...and has been changed to the below to
168 ** match SunOS 4.1.1 and Arnold Robbins'
169 ** strftime version 3.0. That is, "%k" and
170 ** "%l" have been swapped.
173 pt = _conv(t->tm_hour, "%2d", pt, ptlim);
178 ** After all this time, still unclaimed!
180 pt = _add("kitchen sink", pt, ptlim);
182 #endif /* defined KITCHEN_SINK */
185 ** This used to be...
186 ** _conv(t->tm_hour, 2, ' ');
187 ** ...and has been changed to the below to
188 ** match SunOS 4.1.1 and Arnold Robbin's
189 ** strftime version 3.0. That is, "%k" and
190 ** "%l" have been swapped.
193 pt = _conv((t->tm_hour % 12) ?
194 (t->tm_hour % 12) : 12,
198 pt = _conv(t->tm_min, "%02d", pt, ptlim);
201 pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
204 pt = _add("\n", pt, ptlim);
207 pt = _add((t->tm_hour >= 12) ?
213 pt = _fmt("%H:%M", t, pt, ptlim);
216 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim);
219 pt = _conv(t->tm_sec, "%02d", pt, ptlim);
224 char buf[INT_STRLEN_MAXIMUM(
230 if (TYPE_SIGNED(time_t))
231 (void) sprintf(buf, "%ld",
233 else (void) sprintf(buf, "%lu",
234 (unsigned long) mkt);
235 pt = _add(buf, pt, ptlim);
239 pt = _fmt("%H:%M:%S", t, pt, ptlim);
242 pt = _add("\t", pt, ptlim);
245 pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7,
250 ** From Arnold Robbins' strftime version 3.0:
251 ** "ISO 8601: Weekday as a decimal number
255 pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday,
258 case 'V': /* ISO 8601 week number */
259 case 'G': /* ISO 8601 year (four digits) */
260 case 'g': /* ISO 8601 year (two digits) */
262 ** From Arnold Robbins' strftime version 3.0: "the week number of the
263 ** year (the first Monday as the first day of week 1) as a decimal number
267 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
268 ** "Week 01 of a year is per definition the first week which has the
269 ** Thursday in this year, which is equivalent to the week which contains
270 ** the fourth day of January. In other words, the first week of a new year
271 ** is the week which has the majority of its days in the new year. Week 01
272 ** might also contain days from the previous year and the week before week
273 ** 01 of a year is the last week (52 or 53) of the previous year even if
274 ** it contains days from the new year. A week starts with Monday (day 1)
275 ** and ends with Sunday (day 7). For example, the first week of the year
276 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
285 year = t->tm_year + TM_YEAR_BASE;
297 ** What yday (-3 ... 3) does
298 ** the ISO year begin on?
300 bot = ((yday + 11 - wday) %
303 ** What yday does the NEXT
304 ** ISO year begin on?
317 w = 1 + ((yday - bot) /
322 yday += isleap(year) ?
326 #ifdef XPG4_1994_04_09
328 && t->tm_mon == TM_JANUARY)
330 && t->tm_mon == TM_DECEMBER))
332 #endif /* defined XPG4_1994_04_09 */
334 pt = _conv(w, "%02d",
336 else if (*format == 'g') {
337 pt = _conv(year % 100, "%02d",
339 } else pt = _conv(year, "%04d",
345 ** From Arnold Robbins' strftime version 3.0:
346 ** "date as dd-bbb-YYYY"
349 pt = _fmt("%e-%b-%Y", t, pt, ptlim);
352 pt = _conv((t->tm_yday + 7 -
354 (t->tm_wday - 1) : 6)) / 7,
358 pt = _conv(t->tm_wday, "%d", pt, ptlim);
361 pt = _fmt(tptr->X_fmt, t, pt, ptlim);
364 pt = _fmt(tptr->x_fmt, t, pt, ptlim);
367 pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
371 pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
375 if (t->tm_zone != NULL)
376 pt = _add(t->tm_zone, pt, ptlim);
378 if (t->tm_isdst == 0 || t->tm_isdst == 1) {
379 pt = _add(tzname[t->tm_isdst],
381 } else pt = _add("?", pt, ptlim);
386 if (t->tm_gmtoff >= 0) {
387 absoff = t->tm_gmtoff;
388 pt = _add("+", pt, ptlim);
390 absoff = -t->tm_gmtoff;
391 pt = _add("-", pt, ptlim);
393 pt = _conv(absoff / 3600, "%02d",
395 pt = _conv((absoff % 3600) / 60, "%02d",
400 pt = _fmt(tptr->date_fmt, t, pt, ptlim);
404 * X311J/88-090 (4.12.3.5): if conversion char is
405 * undefined, behavior is undefined. Print out the
406 * character itself as printf(3) also does.
420 _conv(n, format, pt, ptlim)
422 const char *const format;
424 const char *const ptlim;
426 char buf[INT_STRLEN_MAXIMUM(int) + 1];
428 (void) sprintf(buf, format, n);
429 return _add(buf, pt, ptlim);
436 const char *const ptlim;
438 while (pt < ptlim && (*pt = *str++) != '\0')