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$");
45 #include "un-namespace.h"
46 #include "timelocal.h"
48 static char * _add(const char *, char *, const char *);
49 static char * _conv(int, const char *, char *, const char *);
50 static char * _fmt(const char *, const struct tm *, char *, const char *,
52 static char * _yconv(int, int, int, int, char *, const char *);
54 extern char * tzname[];
56 #ifndef YEAR_2000_NAME
57 #define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
58 #endif /* !defined YEAR_2000_NAME */
70 static const char fmt_padding[][4][5] = {
71 /* DEFAULT, LESS, SPACE, ZERO */
72 #define PAD_FMT_MONTHDAY 0
74 #define PAD_FMT_CENTURY 0
75 #define PAD_FMT_SHORTYEAR 0
76 #define PAD_FMT_MONTH 0
77 #define PAD_FMT_WEEKOFYEAR 0
78 #define PAD_FMT_DAYOFMONTH 0
79 { "%02d", "%d", "%2d", "%02d" },
80 #define PAD_FMT_SDAYOFMONTH 1
81 #define PAD_FMT_SHMS 1
82 { "%2d", "%d", "%2d", "%02d" },
83 #define PAD_FMT_DAYOFYEAR 2
84 { "%03d", "%d", "%3d", "%03d" },
85 #define PAD_FMT_YEAR 3
86 { "%04d", "%d", "%4d", "%04d" }
90 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
91 const struct tm * __restrict t, locale_t loc)
99 p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, loc);
100 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
101 if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
102 (void) fprintf_l(stderr, loc, "\n");
104 (void) fprintf_l(stderr, loc, "NULL strftime format ");
105 else (void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
107 (void) fprintf_l(stderr, loc, "yields only two digits of years in ");
109 (void) fprintf_l(stderr, loc, "some locales");
110 else if (warn == IN_THIS)
111 (void) fprintf_l(stderr, loc, "the current locale");
112 else (void) fprintf_l(stderr, loc, "all locales");
113 (void) fprintf_l(stderr, loc, "\n");
115 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
116 if (p == s + maxsize)
123 strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
124 const struct tm * __restrict t)
126 return strftime_l(s, maxsize, format, t, __get_locale());
130 _fmt(format, t, pt, ptlim, warnp, loc)
132 const struct tm * const t;
134 const char * const ptlim;
138 int Ealternative, Oalternative, PadIndex;
139 struct lc_time_T *tptr = __get_current_time_locale(loc);
141 for ( ; *format; ++format) {
142 if (*format == '%') {
145 PadIndex = PAD_DEFAULT;
152 pt = _add((t->tm_wday < 0 ||
153 t->tm_wday >= DAYSPERWEEK) ?
154 "?" : tptr->weekday[t->tm_wday],
158 pt = _add((t->tm_wday < 0 ||
159 t->tm_wday >= DAYSPERWEEK) ?
160 "?" : tptr->wday[t->tm_wday],
164 pt = _add((t->tm_mon < 0 ||
165 t->tm_mon >= MONSPERYEAR) ?
166 "?" : (Oalternative ? tptr->alt_month :
167 tptr->month)[t->tm_mon],
172 pt = _add((t->tm_mon < 0 ||
173 t->tm_mon >= MONSPERYEAR) ?
174 "?" : tptr->mon[t->tm_mon],
179 ** %C used to do a...
180 ** _fmt("%a %b %e %X %Y", t);
181 ** ...whereas now POSIX 1003.2 calls for
182 ** something completely different.
185 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
192 pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, loc);
200 pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, loc);
203 pt = _conv(t->tm_mday, fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
207 if (Ealternative || Oalternative)
213 ** C99 locale modifiers.
215 ** %Ec %EC %Ex %EX %Ey %EY
216 ** %Od %oe %OH %OI %Om %OM
217 ** %OS %Ou %OU %OV %Ow %OW %Oy
218 ** are supposed to provide alternate
224 if (Ealternative || Oalternative)
229 pt = _conv(t->tm_mday,
230 fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex], pt, ptlim);
233 pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, loc);
236 pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
240 pt = _conv((t->tm_hour % 12) ?
241 (t->tm_hour % 12) : 12,
242 fmt_padding[PAD_FMT_HMS][PadIndex], pt, ptlim);
245 pt = _conv(t->tm_yday + 1,
246 fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex], pt, ptlim);
250 ** This used to be...
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 */
272 ** This used to be...
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], pt, ptlim);
285 pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
289 pt = _conv(t->tm_mon + 1,
290 fmt_padding[PAD_FMT_MONTH][PadIndex], pt, ptlim);
293 pt = _add("\n", pt, ptlim);
296 pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
302 pt = _fmt("%H:%M", t, pt, ptlim, warnp, loc);
305 pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
309 pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
315 char buf[INT_STRLEN_MAXIMUM(
321 if (TYPE_SIGNED(time_t))
322 (void) sprintf(buf, "%ld",
324 else (void) sprintf(buf, "%lu",
325 (unsigned long) mkt);
326 pt = _add(buf, pt, ptlim);
330 pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, loc);
333 pt = _add("\t", pt, ptlim);
336 pt = _conv((t->tm_yday + DAYSPERWEEK -
337 t->tm_wday) / DAYSPERWEEK,
338 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim);
342 ** From Arnold Robbins' strftime version 3.0:
343 ** "ISO 8601: Weekday as a decimal number
347 pt = _conv((t->tm_wday == 0) ?
348 DAYSPERWEEK : t->tm_wday,
351 case 'V': /* ISO 8601 week number */
352 case 'G': /* ISO 8601 year (four digits) */
353 case 'g': /* ISO 8601 year (two digits) */
355 ** From Arnold Robbins' strftime version 3.0: "the week number of the
356 ** year (the first Monday as the first day of week 1) as a decimal number
360 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
361 ** "Week 01 of a year is per definition the first week which has the
362 ** Thursday in this year, which is equivalent to the week which contains
363 ** the fourth day of January. In other words, the first week of a new year
364 ** is the week which has the majority of its days in the new year. Week 01
365 ** might also contain days from the previous year and the week before week
366 ** 01 of a year is the last week (52 or 53) of the previous year even if
367 ** it contains days from the new year. A week starts with Monday (day 1)
368 ** and ends with Sunday (day 7). For example, the first week of the year
369 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
388 len = isleap_sum(year, base) ?
392 ** What yday (-3 ... 3) does
393 ** the ISO year begin on?
395 bot = ((yday + 11 - wday) %
398 ** What yday does the NEXT
399 ** ISO year begin on?
412 w = 1 + ((yday - bot) /
417 yday += isleap_sum(year, base) ?
421 #ifdef XPG4_1994_04_09
423 t->tm_mon == TM_JANUARY) ||
425 t->tm_mon == TM_DECEMBER))
427 #endif /* defined XPG4_1994_04_09 */
429 pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
431 else if (*format == 'g') {
433 pt = _yconv(year, base, 0, 1,
435 } else pt = _yconv(year, base, 1, 1,
441 ** From Arnold Robbins' strftime version 3.0:
442 ** "date as dd-bbb-YYYY"
445 pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, loc);
448 pt = _conv((t->tm_yday + DAYSPERWEEK -
451 (DAYSPERWEEK - 1))) / DAYSPERWEEK,
452 fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex], pt, ptlim);
455 pt = _conv(t->tm_wday, "%d", pt, ptlim);
458 pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, loc);
464 pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, loc);
473 pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
477 pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
482 if (t->TM_ZONE != NULL)
483 pt = _add(t->TM_ZONE, pt, ptlim);
485 #endif /* defined TM_ZONE */
486 if (t->tm_isdst >= 0)
487 pt = _add(tzname[t->tm_isdst != 0],
490 ** C99 says that %Z must be replaced by the
491 ** empty string if the time zone is not
504 #else /* !defined TM_GMTOFF */
506 ** C99 says that the UTC offset must
507 ** be computed by looking only at
508 ** tm_isdst. This requirement is
509 ** incorrect, since it means the code
510 ** must rely on magic (in this case
511 ** altzone and timezone), and the
512 ** magic might not have the correct
513 ** offset. Doing things correctly is
514 ** tricky and requires disobeying C99;
515 ** see GNU C strftime for details.
516 ** For now, punt and conform to the
517 ** standard, even though it's incorrect.
519 ** C99 says that %z must be replaced by the
520 ** empty string if the time zone is not
521 ** determinable, so output nothing if the
522 ** appropriate variables are not available.
524 if (t->tm_isdst == 0)
527 #else /* !defined USG_COMPAT */
529 #endif /* !defined USG_COMPAT */
533 #else /* !defined ALTZONE */
535 #endif /* !defined ALTZONE */
536 #endif /* !defined TM_GMTOFF */
541 pt = _add(sign, pt, ptlim);
543 diff = (diff / MINSPERHOUR) * 100 +
544 (diff % MINSPERHOUR);
546 fmt_padding[PAD_FMT_YEAR][PadIndex], pt, ptlim);
550 pt = _fmt(tptr->date_fmt, t, pt, ptlim,
554 if (PadIndex != PAD_DEFAULT)
559 if (PadIndex != PAD_DEFAULT)
561 PadIndex = PAD_SPACE;
564 if (PadIndex != PAD_DEFAULT)
570 ** X311J/88-090 (4.12.3.5): if conversion char is
571 ** undefined, behavior is undefined. Print out the
572 ** character itself as printf(3) also does.
586 _conv(n, format, pt, ptlim)
588 const char * const format;
590 const char * const ptlim;
592 char buf[INT_STRLEN_MAXIMUM(int) + 1];
594 (void) sprintf(buf, format, n);
595 return _add(buf, pt, ptlim);
602 const char * const ptlim;
604 while (pt < ptlim && (*pt = *str++) != '\0')
610 ** POSIX and the C Standard are unclear or inconsistent about
611 ** what %C and %y do if the year is negative or exceeds 9999.
612 ** Use the convention that %C concatenated with %y yields the
613 ** same output as %Y, and that %Y contains at least 4 bytes,
614 ** with more only if necessary.
618 _yconv(a, b, convert_top, convert_yy, pt, ptlim)
621 const int convert_top;
622 const int convert_yy;
624 const char * const ptlim;
630 trail = a % DIVISOR + b % DIVISOR;
631 lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
633 if (trail < 0 && lead > 0) {
636 } else if (lead < 0 && trail > 0) {
641 if (lead == 0 && trail < 0)
642 pt = _add("-0", pt, ptlim);
643 else pt = _conv(lead, "%02d", pt, ptlim);
646 pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);