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 fprintf_l(stderr
, loc
, "\n");
92 fprintf_l(stderr
, loc
, "NULL strftime format ");
93 else fprintf_l(stderr
, loc
, "strftime format \"%s\" ",
95 fprintf_l(stderr
, loc
, "yields only two digits of years in ");
97 fprintf_l(stderr
, loc
, "some locales");
98 else if (warn
== IN_THIS
)
99 fprintf_l(stderr
, loc
, "the current locale");
100 else fprintf_l(stderr
, loc
, "all locales");
101 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(const char *format
, const struct tm
* const t
, char *pt
,
119 const char * const ptlim
, int *warnp
, locale_t loc
)
121 int Ealternative
, Oalternative
, PadIndex
;
122 struct lc_time_T
*tptr
= __get_current_time_locale(loc
);
124 for ( ; *format
; ++format
) {
125 if (*format
== '%') {
128 PadIndex
= PAD_DEFAULT
;
135 pt
= _add((t
->tm_wday
< 0 ||
136 t
->tm_wday
>= DAYSPERWEEK
) ?
137 "?" : tptr
->weekday
[t
->tm_wday
],
141 pt
= _add((t
->tm_wday
< 0 ||
142 t
->tm_wday
>= DAYSPERWEEK
) ?
143 "?" : tptr
->wday
[t
->tm_wday
],
147 pt
= _add((t
->tm_mon
< 0 ||
148 t
->tm_mon
>= MONSPERYEAR
) ?
149 "?" : (Oalternative
? tptr
->alt_month
:
150 tptr
->month
)[t
->tm_mon
],
155 pt
= _add((t
->tm_mon
< 0 ||
156 t
->tm_mon
>= MONSPERYEAR
) ?
157 "?" : tptr
->mon
[t
->tm_mon
],
163 * _fmt("%a %b %e %X %Y", t);
164 * ...whereas now POSIX 1003.2 calls for
165 * something completely different.
168 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 0,
175 pt
= _fmt(tptr
->c_fmt
, t
, pt
, ptlim
, &warn2
, loc
);
183 pt
= _fmt("%m/%d/%y", t
, pt
, ptlim
, warnp
, loc
);
186 pt
= _conv(t
->tm_mday
, fmt_padding
[PAD_FMT_DAYOFMONTH
][PadIndex
],
190 if (Ealternative
|| Oalternative
)
196 * C99 locale modifiers.
198 * %Ec %EC %Ex %EX %Ey %EY
199 * %Od %oe %OH %OI %Om %OM
200 * %OS %Ou %OU %OV %Ow %OW %Oy
201 * are supposed to provide alternate
207 if (Ealternative
|| Oalternative
)
212 pt
= _conv(t
->tm_mday
,
213 fmt_padding
[PAD_FMT_SDAYOFMONTH
][PadIndex
], pt
, ptlim
);
216 pt
= _fmt("%Y-%m-%d", t
, pt
, ptlim
, warnp
, loc
);
219 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
223 pt
= _conv((t
->tm_hour
% 12) ?
224 (t
->tm_hour
% 12) : 12,
225 fmt_padding
[PAD_FMT_HMS
][PadIndex
], pt
, ptlim
);
228 pt
= _conv(t
->tm_yday
+ 1,
229 fmt_padding
[PAD_FMT_DAYOFYEAR
][PadIndex
], pt
, ptlim
);
234 * _conv(t->tm_hour % 12 ?
235 * t->tm_hour % 12 : 12, 2, ' ');
236 * ...and has been changed to the below to
237 * match SunOS 4.1.1 and Arnold Robbins'
238 * strftime version 3.0. That is, "%k" and
239 * "%l" have been swapped.
242 pt
= _conv(t
->tm_hour
, fmt_padding
[PAD_FMT_SHMS
][PadIndex
],
248 * After all this time, still unclaimed!
250 pt
= _add("kitchen sink", pt
, ptlim
);
252 #endif /* defined KITCHEN_SINK */
256 * _conv(t->tm_hour, 2, ' ');
257 * ...and has been changed to the below to
258 * match SunOS 4.1.1 and Arnold Robbin's
259 * strftime version 3.0. That is, "%k" and
260 * "%l" have been swapped.
263 pt
= _conv((t
->tm_hour
% 12) ?
264 (t
->tm_hour
% 12) : 12,
265 fmt_padding
[PAD_FMT_SHMS
][PadIndex
], pt
, ptlim
);
268 pt
= _conv(t
->tm_min
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
272 pt
= _conv(t
->tm_mon
+ 1,
273 fmt_padding
[PAD_FMT_MONTH
][PadIndex
], pt
, ptlim
);
276 pt
= _add("\n", pt
, ptlim
);
279 pt
= _add((t
->tm_hour
>= (HOURSPERDAY
/ 2)) ?
285 pt
= _fmt("%H:%M", t
, pt
, ptlim
, warnp
, loc
);
288 pt
= _fmt(tptr
->ampm_fmt
, t
, pt
, ptlim
,
292 pt
= _conv(t
->tm_sec
, fmt_padding
[PAD_FMT_HMS
][PadIndex
],
298 char buf
[INT_STRLEN_MAXIMUM(
304 if (TYPE_SIGNED(time_t))
305 snprintf(buf
, sizeof(buf
),
308 else snprintf(buf
, sizeof(buf
),
311 pt
= _add(buf
, pt
, ptlim
);
315 pt
= _fmt("%H:%M:%S", t
, pt
, ptlim
, warnp
, loc
);
318 pt
= _add("\t", pt
, ptlim
);
321 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
322 t
->tm_wday
) / DAYSPERWEEK
,
323 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
);
327 * From Arnold Robbins' strftime version 3.0:
328 * "ISO 8601: Weekday as a decimal number
332 pt
= _conv((t
->tm_wday
== 0) ?
333 DAYSPERWEEK
: t
->tm_wday
,
336 case 'V': /* ISO 8601 week number */
337 case 'G': /* ISO 8601 year (four digits) */
338 case 'g': /* ISO 8601 year (two digits) */
340 * From Arnold Robbins' strftime version 3.0: "the week number of the
341 * year (the first Monday as the first day of week 1) as a decimal number
345 * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
346 * "Week 01 of a year is per definition the first week which has the
347 * Thursday in this year, which is equivalent to the week which contains
348 * the fourth day of January. In other words, the first week of a new year
349 * is the week which has the majority of its days in the new year. Week 01
350 * might also contain days from the previous year and the week before week
351 * 01 of a year is the last week (52 or 53) of the previous year even if
352 * it contains days from the new year. A week starts with Monday (day 1)
353 * and ends with Sunday (day 7). For example, the first week of the year
354 * 1997 lasts from 1996-12-30 to 1997-01-05..."
373 len
= isleap_sum(year
, base
) ?
377 * What yday (-3 ... 3) does
378 * the ISO year begin on?
380 bot
= ((yday
+ 11 - wday
) %
383 * What yday does the NEXT
397 w
= 1 + ((yday
- bot
) /
402 yday
+= isleap_sum(year
, base
) ?
406 #ifdef XPG4_1994_04_09
408 t
->tm_mon
== TM_JANUARY
) ||
410 t
->tm_mon
== TM_DECEMBER
))
412 #endif /* defined XPG4_1994_04_09 */
414 pt
= _conv(w
, fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
],
416 else if (*format
== 'g') {
418 pt
= _yconv(year
, base
, 0, 1,
420 } else pt
= _yconv(year
, base
, 1, 1,
426 * From Arnold Robbins' strftime version 3.0:
427 * "date as dd-bbb-YYYY"
430 pt
= _fmt("%e-%b-%Y", t
, pt
, ptlim
, warnp
, loc
);
433 pt
= _conv((t
->tm_yday
+ DAYSPERWEEK
-
436 (DAYSPERWEEK
- 1))) / DAYSPERWEEK
,
437 fmt_padding
[PAD_FMT_WEEKOFYEAR
][PadIndex
], pt
, ptlim
);
440 pt
= _conv(t
->tm_wday
, "%d", pt
, ptlim
);
443 pt
= _fmt(tptr
->X_fmt
, t
, pt
, ptlim
, warnp
, loc
);
449 pt
= _fmt(tptr
->x_fmt
, t
, pt
, ptlim
, &warn2
, loc
);
458 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 0, 1,
462 pt
= _yconv(t
->tm_year
, TM_YEAR_BASE
, 1, 1,
466 if (t
->TM_ZONE
!= NULL
)
467 pt
= _add(t
->TM_ZONE
, pt
, ptlim
);
469 if (t
->tm_isdst
>= 0)
470 pt
= _add(tzname
[t
->tm_isdst
!= 0],
473 * C99 says that %Z must be replaced by the
474 * empty string if the time zone is not
491 pt
= _add(sign
, pt
, ptlim
);
493 diff
= (diff
/ MINSPERHOUR
) * 100 +
494 (diff
% MINSPERHOUR
);
496 fmt_padding
[PAD_FMT_YEAR
][PadIndex
], pt
, ptlim
);
500 pt
= _fmt(tptr
->date_fmt
, t
, pt
, ptlim
,
504 if (PadIndex
!= PAD_DEFAULT
)
509 if (PadIndex
!= PAD_DEFAULT
)
511 PadIndex
= PAD_SPACE
;
514 if (PadIndex
!= PAD_DEFAULT
)
520 * X311J/88-090 (4.12.3.5): if conversion char is
521 * undefined, behavior is undefined. Print out the
522 * character itself as printf(3) also does.
536 _conv(const int n
, const char * const format
, char * const pt
,
537 const char * const ptlim
)
539 char buf
[INT_STRLEN_MAXIMUM(int) + 1];
541 snprintf(buf
, sizeof(buf
), format
, n
);
542 return _add(buf
, pt
, ptlim
);
546 _add(const char *str
, char *pt
, const char * const ptlim
)
548 while (pt
< ptlim
&& (*pt
= *str
++) != '\0')
554 * POSIX and the C Standard are unclear or inconsistent about
555 * what %C and %y do if the year is negative or exceeds 9999.
556 * Use the convention that %C concatenated with %y yields the
557 * same output as %Y, and that %Y contains at least 4 bytes,
558 * with more only if necessary.
562 _yconv(const int a
, const int b
, const int convert_top
, const int convert_yy
,
563 char *pt
, const char * const ptlim
)
569 trail
= a
% DIVISOR
+ b
% DIVISOR
;
570 lead
= a
/ DIVISOR
+ b
/ DIVISOR
+ trail
/ DIVISOR
;
572 if (trail
< 0 && lead
> 0) {
575 } else if (lead
< 0 && trail
> 0) {
580 if (lead
== 0 && trail
< 0)
581 pt
= _add("-0", pt
, ptlim
);
582 else pt
= _conv(lead
, "%02d", pt
, ptlim
);
585 pt
= _conv(((trail
< 0) ? -trail
: trail
), "%02d", pt
, ptlim
);