2 * Copyright (c) 1989 The Regents of the University of California.
5 * Copyright (c) 2011 The FreeBSD Foundation
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms and that any documentation,
13 * advertising materials, and other materials related to such
14 * distribution and use acknowledge that the software was developed
15 * by the University of California, Berkeley. The name of the
16 * University may not be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 static const char elsieid[] = "@(#)strftime.3 8.3";
27 * Based on the UCB version with the ID appearing below.
28 * This is ANSIish only when "multibyte character == plain character".
30 #endif /* !defined NOID */
31 #endif /* !defined lint */
33 #include "namespace.h"
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static const char sccsid[] = "@(#)strftime.c 5.4 (Berkeley) 3/14/89";
38 #endif /* LIBC_SCCS and not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
46 #include "un-namespace.h"
47 #include "timelocal.h"
49 static char * _add(const char *, char *, const char *);
50 static char * _conv(int, const char *, char *, const char *, locale_t);
51 static char * _fmt(const char *, const struct tm *, char *, const char *,
53 static char * _yconv(int, int, int, int, char *, const char *, locale_t);
55 extern char * tzname[];
57 #ifndef YEAR_2000_NAME
58 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
59 #endif /* !defined YEAR_2000_NAME */
71 static const char fmt_padding[][4][5] = {
72 /* DEFAULT, LESS, SPACE, ZERO */
73 #define PAD_FMT_MONTHDAY 0
75 #define PAD_FMT_CENTURY 0
76 #define PAD_FMT_SHORTYEAR 0
77 #define PAD_FMT_MONTH 0
78 #define PAD_FMT_WEEKOFYEAR 0
79 #define PAD_FMT_DAYOFMONTH 0
80 { "%02d", "%d", "%2d", "%02d" },
81 #define PAD_FMT_SDAYOFMONTH 1
82 #define PAD_FMT_SHMS 1
83 { "%2d", "%d", "%2d", "%02d" },
84 #define PAD_FMT_DAYOFYEAR 2
85 { "%03d", "%d", "%3d", "%03d" },
86 #define PAD_FMT_YEAR 3
87 { "%04d", "%d", "%4d", "%04d" }
91 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
92 const struct tm * __restrict t, locale_t loc)
100 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, loc);
101 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
102 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
103 (void) fprintf_l(stderr, loc, "\n");
105 (void) fputs("NULL strftime format ", stderr);
106 else (void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
108 (void) fputs("yields only two digits of years in ", stderr);
110 (void) fputs("some locales", stderr);
111 else if (warn == IN_THIS)
112 (void) fputs("the current locale", stderr);
113 else (void) fputs("all locales", stderr);
114 (void) fputs("\n", stderr);
116 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
117 if (p == s + maxsize)
124 strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
125 const struct tm * __restrict t)
127 return strftime_l(s, maxsize, format, t, __get_locale());
131 _fmt(const char *format, const struct tm * const t, char *pt,
132 const char * const ptlim, int *warnp, locale_t loc)
134 int Ealternative, Oalternative, PadIndex;
135 struct lc_time_T *tptr = __get_current_time_locale(loc);
137 for ( ; *format; ++format) {
138 if (*format == '%') {
141 PadIndex = PAD_DEFAULT;
148 pt = _add((t->tm_wday < 0 ||
149 t->tm_wday >= DAYSPERWEEK) ?
150 "?" : tptr->weekday[t->tm_wday],
154 pt = _add((t->tm_wday < 0 ||
155 t->tm_wday >= DAYSPERWEEK) ?
156 "?" : tptr->wday[t->tm_wday],
160 pt = _add((t->tm_mon < 0 ||
161 t->tm_mon >= MONSPERYEAR) ?
162 "?" : (Oalternative ? tptr->alt_month :
163 tptr->month)[t->tm_mon],
168 pt = _add((t->tm_mon < 0 ||
169 t->tm_mon >= MONSPERYEAR) ?
170 "?" : tptr->mon[t->tm_mon],
176 * _fmt("%a %b %e %X %Y", t);
177 * ...whereas now POSIX 1003.2 calls for
178 * something completely different.
181 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
188 pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, loc);
196 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, loc);
199 pt = _conv(t->tm_mday,
200 fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
204 if (Ealternative || Oalternative)
210 * C99 locale modifiers.
212 * %Ec %EC %Ex %EX %Ey %EY
213 * %Od %oe %OH %OI %Om %OM
214 * %OS %Ou %OU %OV %Ow %OW %Oy
215 * are supposed to provide alternate
221 if (Ealternative || Oalternative)
226 pt = _conv(t->tm_mday,
227 fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
231 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, loc);
234 pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
238 pt = _conv((t->tm_hour % 12) ?
239 (t->tm_hour % 12) : 12,
240 fmt_padding[PAD_FMT_HMS][PadIndex],
244 pt = _conv(t->tm_yday + 1,
245 fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
251 * _conv(t->tm_hour % 12 ?
252 * t->tm_hour % 12 : 12, 2, ' ');
253 * ...and has been changed to the below to
254 * match SunOS 4.1.1 and Arnold Robbins'
255 * strftime version 3.0. That is, "%k" and
256 * "%l" have been swapped.
259 pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex],
265 ** After all this time, still unclaimed!
267 pt = _add("kitchen sink", pt, ptlim);
269 #endif /* defined KITCHEN_SINK */
273 * _conv(t->tm_hour, 2, ' ');
274 * ...and has been changed to the below to
275 * match SunOS 4.1.1 and Arnold Robbin's
276 * strftime version 3.0. That is, "%k" and
277 * "%l" have been swapped.
280 pt = _conv((t->tm_hour % 12) ?
281 (t->tm_hour % 12) : 12,
282 fmt_padding[PAD_FMT_SHMS][PadIndex],
286 pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
290 pt = _conv(t->tm_mon + 1,
291 fmt_padding[PAD_FMT_MONTH][PadIndex],
295 pt = _add("\n", pt, ptlim);
298 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
303 pt = _fmt("%H:%M", t, pt, ptlim, warnp, loc);
306 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
310 pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
316 char buf[INT_STRLEN_MAXIMUM(
322 if (TYPE_SIGNED(time_t))
323 (void) sprintf_l(buf, loc, "%ld",
325 else (void) sprintf_l(buf, loc, "%lu",
326 (unsigned long) mkt);
327 pt = _add(buf, pt, ptlim);
331 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, loc);
334 pt = _add("\t", pt, ptlim);
337 pt = _conv((t->tm_yday + DAYSPERWEEK -
338 t->tm_wday) / DAYSPERWEEK,
339 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
344 * From Arnold Robbins' strftime version 3.0:
345 * "ISO 8601: Weekday as a decimal number
349 pt = _conv((t->tm_wday == 0) ?
350 DAYSPERWEEK : t->tm_wday,
351 "%d", pt, ptlim, loc);
353 case 'V': /* ISO 8601 week number */
354 case 'G': /* ISO 8601 year (four digits) */
355 case 'g': /* ISO 8601 year (two digits) */
357 * From Arnold Robbins' strftime version 3.0: "the week number of the
358 * year (the first Monday as the first day of week 1) as a decimal number
362 * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
363 * "Week 01 of a year is per definition the first week which has the
364 * Thursday in this year, which is equivalent to the week which contains
365 * the fourth day of January. In other words, the first week of a new year
366 * is the week which has the majority of its days in the new year. Week 01
367 * might also contain days from the previous year and the week before week
368 * 01 of a year is the last week (52 or 53) of the previous year even if
369 * it contains days from the new year. A week starts with Monday (day 1)
370 * and ends with Sunday (day 7). For example, the first week of the year
371 * 1997 lasts from 1996-12-30 to 1997-01-05..."
390 len = isleap_sum(year, base) ?
394 * What yday (-3 ... 3) does
395 * the ISO year begin on?
397 bot = ((yday + 11 - wday) %
400 * What yday does the NEXT
414 w = 1 + ((yday - bot) /
419 yday += isleap_sum(year, base) ?
423 #ifdef XPG4_1994_04_09
425 t->tm_mon == TM_JANUARY) ||
427 t->tm_mon == TM_DECEMBER))
429 #endif /* defined XPG4_1994_04_09 */
431 pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
433 else if (*format == 'g') {
435 pt = _yconv(year, base, 0, 1,
437 } else pt = _yconv(year, base, 1, 1,
443 * From Arnold Robbins' strftime version 3.0:
444 * "date as dd-bbb-YYYY"
447 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, loc);
450 pt = _conv((t->tm_yday + DAYSPERWEEK -
453 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
454 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
458 pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
461 pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, loc);
467 pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, loc);
476 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
480 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
485 if (t->TM_ZONE != NULL)
486 pt = _add(t->TM_ZONE, pt, ptlim);
488 #endif /* defined TM_ZONE */
489 if (t->tm_isdst >= 0)
490 pt = _add(tzname[t->tm_isdst != 0],
493 * C99 says that %Z must be replaced by the
494 * empty string if the time zone is not
507 #else /* !defined TM_GMTOFF */
509 * C99 says that the UTC offset must
510 * be computed by looking only at
511 * tm_isdst. This requirement is
512 * incorrect, since it means the code
513 * must rely on magic (in this case
514 * altzone and timezone), and the
515 * magic might not have the correct
516 * offset. Doing things correctly is
517 * tricky and requires disobeying C99;
518 * see GNU C strftime for details.
519 * For now, punt and conform to the
520 * standard, even though it's incorrect.
522 * C99 says that %z must be replaced by the
523 * empty string if the time zone is not
524 * determinable, so output nothing if the
525 * appropriate variables are not available.
527 if (t->tm_isdst == 0)
530 #else /* !defined USG_COMPAT */
532 #endif /* !defined USG_COMPAT */
536 #else /* !defined ALTZONE */
538 #endif /* !defined ALTZONE */
539 #endif /* !defined TM_GMTOFF */
545 pt = _add(sign, pt, ptlim);
547 diff = (diff / MINSPERHOUR) * 100 +
548 (diff % MINSPERHOUR);
550 fmt_padding[PAD_FMT_YEAR][PadIndex],
555 pt = _fmt(tptr->date_fmt, t, pt, ptlim,
559 if (PadIndex != PAD_DEFAULT)
564 if (PadIndex != PAD_DEFAULT)
566 PadIndex = PAD_SPACE;
569 if (PadIndex != PAD_DEFAULT)
575 * X311J/88-090 (4.12.3.5): if conversion char is
576 * undefined, behavior is undefined. Print out the
577 * character itself as printf(3) also does.
591 _conv(const int n, const char * const format, char * const pt,
592 const char * const ptlim, locale_t loc)
594 char buf[INT_STRLEN_MAXIMUM(int) + 1];
596 (void) sprintf_l(buf, loc, format, n);
597 return _add(buf, pt, ptlim);
601 _add(const char *str, char *pt, const char * const ptlim)
603 while (pt < ptlim && (*pt = *str++) != '\0')
609 * POSIX and the C Standard are unclear or inconsistent about
610 * what %C and %y do if the year is negative or exceeds 9999.
611 * Use the convention that %C concatenated with %y yields the
612 * same output as %Y, and that %Y contains at least 4 bytes,
613 * with more only if necessary.
617 _yconv(const int a, const int b, const int convert_top, const int convert_yy,
618 char *pt, const char * const ptlim, locale_t loc)
624 trail = a % DIVISOR + b % DIVISOR;
625 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
627 if (trail < 0 && lead > 0) {
630 } else if (lead < 0 && trail > 0) {
635 if (lead == 0 && trail < 0)
636 pt = _add("-0", pt, ptlim);
637 else pt = _conv(lead, "%02d", pt, ptlim, loc);
640 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt,