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.
22 * @(#)strftime.c 5.4 (Berkeley) 3/14/89
23 * $FreeBSD: head/lib/libc/stdtime/strftime.c 237211 2012-06-17 21:40:13Z jilles $
27 #include "namespace.h"
33 #include "un-namespace.h"
34 #include "timelocal.h"
36 static char * _add(const char *, char *, const char *);
37 static char * _conv(int, const char *, char *, const char *);
38 static char * _fmt(const char *, const struct tm *, char *, const char *,
40 static char * _yconv(int, int, int, int, char *, const char *);
42 extern char * tzname[];
44 #ifndef YEAR_2000_NAME
45 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
46 #endif /* !defined YEAR_2000_NAME */
58 static const char fmt_padding[][4][5] = {
59 /* DEFAULT, LESS, SPACE, ZERO */
60 #define PAD_FMT_MONTHDAY 0
62 #define PAD_FMT_CENTURY 0
63 #define PAD_FMT_SHORTYEAR 0
64 #define PAD_FMT_MONTH 0
65 #define PAD_FMT_WEEKOFYEAR 0
66 #define PAD_FMT_DAYOFMONTH 0
67 { "%02d", "%d", "%2d", "%02d" },
68 #define PAD_FMT_SDAYOFMONTH 1
69 #define PAD_FMT_SHMS 1
70 { "%2d", "%d", "%2d", "%02d" },
71 #define PAD_FMT_DAYOFYEAR 2
72 { "%03d", "%d", "%3d", "%03d" },
73 #define PAD_FMT_YEAR 3
74 { "%04d", "%d", "%4d", "%04d" }
78 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
79 const struct tm * __restrict t, locale_t loc)
87 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, loc);
88 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
89 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
90 (void) fprintf_l(stderr, loc, "\n");
92 (void) fprintf_l(stderr, loc, "NULL strftime format ");
93 else (void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
95 (void) fprintf_l(stderr, loc, "yields only two digits of years in ");
97 (void) fprintf_l(stderr, loc, "some locales");
98 else if (warn == IN_THIS)
99 (void) fprintf_l(stderr, loc, "the current locale");
100 else (void) fprintf_l(stderr, loc, "all locales");
101 (void) fprintf_l(stderr, loc, "\n");
103 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
104 if (p == s + maxsize)
111 strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
112 const struct tm * __restrict t)
114 return strftime_l(s, maxsize, format, t, __get_locale());
118 _fmt(format, t, pt, ptlim, warnp, loc)
120 const struct tm * const t;
122 const char * const ptlim;
126 int Ealternative, Oalternative, PadIndex;
127 struct lc_time_T *tptr = __get_current_time_locale(loc);
129 for ( ; *format; ++format) {
130 if (*format == '%') {
133 PadIndex = PAD_DEFAULT;
140 pt = _add((t->tm_wday < 0 ||
141 t->tm_wday >= DAYSPERWEEK) ?
142 "?" : tptr->weekday[t->tm_wday],
146 pt = _add((t->tm_wday < 0 ||
147 t->tm_wday >= DAYSPERWEEK) ?
148 "?" : tptr->wday[t->tm_wday],
152 pt = _add((t->tm_mon < 0 ||
153 t->tm_mon >= MONSPERYEAR) ?
154 "?" : (Oalternative ? tptr->alt_month :
155 tptr->month)[t->tm_mon],
160 pt = _add((t->tm_mon < 0 ||
161 t->tm_mon >= MONSPERYEAR) ?
162 "?" : tptr->mon[t->tm_mon],
167 ** %C used to do a...
168 ** _fmt("%a %b %e %X %Y", t);
169 ** ...whereas now POSIX 1003.2 calls for
170 ** something completely different.
173 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
180 pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, loc);
188 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, loc);
191 pt = _conv(t->tm_mday, fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
195 if (Ealternative || Oalternative)
201 ** C99 locale modifiers.
203 ** %Ec %EC %Ex %EX %Ey %EY
204 ** %Od %oe %OH %OI %Om %OM
205 ** %OS %Ou %OU %OV %Ow %OW %Oy
206 ** are supposed to provide alternate
212 if (Ealternative || Oalternative)
217 pt = _conv(t->tm_mday,
218 fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex], pt, ptlim);
221 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, loc);
224 pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
228 pt = _conv((t->tm_hour % 12) ?
229 (t->tm_hour % 12) : 12,
230 fmt_padding[PAD_FMT_HMS][PadIndex], pt, ptlim);
233 pt = _conv(t->tm_yday + 1,
234 fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex], pt, ptlim);
238 ** This used to be...
239 ** _conv(t->tm_hour % 12 ?
240 ** t->tm_hour % 12 : 12, 2, ' ');
241 ** ...and has been changed to the below to
242 ** match SunOS 4.1.1 and Arnold Robbins'
243 ** strftime version 3.0. That is, "%k" and
244 ** "%l" have been swapped.
247 pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex],
253 ** After all this time, still unclaimed!
255 pt = _add("kitchen sink", pt, ptlim);
257 #endif /* defined KITCHEN_SINK */
260 ** This used to be...
261 ** _conv(t->tm_hour, 2, ' ');
262 ** ...and has been changed to the below to
263 ** match SunOS 4.1.1 and Arnold Robbin's
264 ** strftime version 3.0. That is, "%k" and
265 ** "%l" have been swapped.
268 pt = _conv((t->tm_hour % 12) ?
269 (t->tm_hour % 12) : 12,
270 fmt_padding[PAD_FMT_SHMS][PadIndex], pt, ptlim);
273 pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
277 pt = _conv(t->tm_mon + 1,
278 fmt_padding[PAD_FMT_MONTH][PadIndex], pt, ptlim);
281 pt = _add("\n", pt, ptlim);
284 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
290 pt = _fmt("%H:%M", t, pt, ptlim, warnp, loc);
293 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
297 pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
303 char buf[INT_STRLEN_MAXIMUM(
309 if (TYPE_SIGNED(time_t))
310 (void) sprintf(buf, "%ld",
312 else (void) sprintf(buf, "%lu",
313 (unsigned long) mkt);
314 pt = _add(buf, pt, ptlim);
318 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, loc);
321 pt = _add("\t", pt, ptlim);
324 pt = _conv((t->tm_yday + DAYSPERWEEK -
325 t->tm_wday) / DAYSPERWEEK,
326 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim);
330 ** From Arnold Robbins' strftime version 3.0:
331 ** "ISO 8601: Weekday as a decimal number
335 pt = _conv((t->tm_wday == 0) ?
336 DAYSPERWEEK : t->tm_wday,
339 case 'V': /* ISO 8601 week number */
340 case 'G': /* ISO 8601 year (four digits) */
341 case 'g': /* ISO 8601 year (two digits) */
343 ** From Arnold Robbins' strftime version 3.0: "the week number of the
344 ** year (the first Monday as the first day of week 1) as a decimal number
348 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
349 ** "Week 01 of a year is per definition the first week which has the
350 ** Thursday in this year, which is equivalent to the week which contains
351 ** the fourth day of January. In other words, the first week of a new year
352 ** is the week which has the majority of its days in the new year. Week 01
353 ** might also contain days from the previous year and the week before week
354 ** 01 of a year is the last week (52 or 53) of the previous year even if
355 ** it contains days from the new year. A week starts with Monday (day 1)
356 ** and ends with Sunday (day 7). For example, the first week of the year
357 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
376 len = isleap_sum(year, base) ?
380 ** What yday (-3 ... 3) does
381 ** the ISO year begin on?
383 bot = ((yday + 11 - wday) %
386 ** What yday does the NEXT
387 ** ISO year begin on?
400 w = 1 + ((yday - bot) /
405 yday += isleap_sum(year, base) ?
409 #ifdef XPG4_1994_04_09
411 t->tm_mon == TM_JANUARY) ||
413 t->tm_mon == TM_DECEMBER))
415 #endif /* defined XPG4_1994_04_09 */
417 pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
419 else if (*format == 'g') {
421 pt = _yconv(year, base, 0, 1,
423 } else pt = _yconv(year, base, 1, 1,
429 ** From Arnold Robbins' strftime version 3.0:
430 ** "date as dd-bbb-YYYY"
433 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, loc);
436 pt = _conv((t->tm_yday + DAYSPERWEEK -
439 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
440 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim);
443 pt = _conv(t->tm_wday, "%d", pt, ptlim);
446 pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, loc);
452 pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, loc);
461 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
465 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
470 if (t->TM_ZONE != NULL)
471 pt = _add(t->TM_ZONE, pt, ptlim);
473 #endif /* defined TM_ZONE */
474 if (t->tm_isdst >= 0)
475 pt = _add(tzname[t->tm_isdst != 0],
478 ** C99 says that %Z must be replaced by the
479 ** empty string if the time zone is not
492 #else /* !defined TM_GMTOFF */
494 ** C99 says that the UTC offset must
495 ** be computed by looking only at
496 ** tm_isdst. This requirement is
497 ** incorrect, since it means the code
498 ** must rely on magic (in this case
499 ** altzone and timezone), and the
500 ** magic might not have the correct
501 ** offset. Doing things correctly is
502 ** tricky and requires disobeying C99;
503 ** see GNU C strftime for details.
504 ** For now, punt and conform to the
505 ** standard, even though it's incorrect.
507 ** C99 says that %z must be replaced by the
508 ** empty string if the time zone is not
509 ** determinable, so output nothing if the
510 ** appropriate variables are not available.
512 if (t->tm_isdst == 0)
517 #else /* !defined ALTZONE */
519 #endif /* !defined ALTZONE */
520 #endif /* !defined TM_GMTOFF */
525 pt = _add(sign, pt, ptlim);
527 diff = (diff / MINSPERHOUR) * 100 +
528 (diff % MINSPERHOUR);
530 fmt_padding[PAD_FMT_YEAR][PadIndex], pt, ptlim);
534 pt = _fmt(tptr->date_fmt, t, pt, ptlim,
538 if (PadIndex != PAD_DEFAULT)
543 if (PadIndex != PAD_DEFAULT)
545 PadIndex = PAD_SPACE;
548 if (PadIndex != PAD_DEFAULT)
554 ** X311J/88-090 (4.12.3.5): if conversion char is
555 ** undefined, behavior is undefined. Print out the
556 ** character itself as printf(3) also does.
570 _conv(n, format, pt, ptlim)
572 const char * const format;
574 const char * const ptlim;
576 char buf[INT_STRLEN_MAXIMUM(int) + 1];
578 (void) sprintf(buf, format, n);
579 return _add(buf, pt, ptlim);
586 const char * const ptlim;
588 while (pt < ptlim && (*pt = *str++) != '\0')
594 ** POSIX and the C Standard are unclear or inconsistent about
595 ** what %C and %y do if the year is negative or exceeds 9999.
596 ** Use the convention that %C concatenated with %y yields the
597 ** same output as %Y, and that %Y contains at least 4 bytes,
598 ** with more only if necessary.
602 _yconv(a, b, convert_top, convert_yy, pt, ptlim)
605 const int convert_top;
606 const int convert_yy;
608 const char * const ptlim;
614 trail = a % DIVISOR + b % DIVISOR;
615 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
617 if (trail < 0 && lead > 0) {
620 } else if (lead < 0 && trail > 0) {
625 if (lead == 0 && trail < 0)
626 pt = _add("-0", pt, ptlim);
627 else pt = _conv(lead, "%02d", pt, ptlim);
630 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);