xref: /freebsd/lib/libc/stdtime/strftime.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Copyright (c) 2011 The FreeBSD Foundation
6  *
7  * Portions of this software were developed by David Chisnall
8  * under sponsorship from the FreeBSD Foundation.
9  *
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.
21  */
22 
23 #include "namespace.h"
24 #include "private.h"
25 
26 #include "tzfile.h"
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <stdio.h>
30 #include "un-namespace.h"
31 #include "timelocal.h"
32 
33 static char *	_add(const char *, char *, const char *);
34 static char *	_conv(int, const char *, char *, const char *, locale_t);
35 static char *	_fmt(const char *, const struct tm *, char *, const char *,
36 			int *, locale_t);
37 static char *	_yconv(int, int, int, int, char *, const char *, locale_t);
38 
39 extern char *	tzname[];
40 
41 #ifndef YEAR_2000_NAME
42 #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
43 #endif /* !defined YEAR_2000_NAME */
44 
45 #define	IN_NONE	0
46 #define	IN_SOME	1
47 #define	IN_THIS	2
48 #define	IN_ALL	3
49 
50 #define	PAD_DEFAULT	0
51 #define	PAD_LESS	1
52 #define	PAD_SPACE	2
53 #define	PAD_ZERO	3
54 
55 static const char fmt_padding[][4][5] = {
56 	/* DEFAULT,	LESS,	SPACE,	ZERO */
57 #define	PAD_FMT_MONTHDAY	0
58 #define	PAD_FMT_HMS		0
59 #define	PAD_FMT_CENTURY		0
60 #define	PAD_FMT_SHORTYEAR	0
61 #define	PAD_FMT_MONTH		0
62 #define	PAD_FMT_WEEKOFYEAR	0
63 #define	PAD_FMT_DAYOFMONTH	0
64 	{ "%02d",	"%d",	"%2d",	"%02d" },
65 #define	PAD_FMT_SDAYOFMONTH	1
66 #define	PAD_FMT_SHMS		1
67 	{ "%2d",	"%d",	"%2d",	"%02d" },
68 #define	PAD_FMT_DAYOFYEAR	2
69 	{ "%03d",	"%d",	"%3d",	"%03d" },
70 #define	PAD_FMT_YEAR		3
71 	{ "%04d",	"%d",	"%4d",	"%04d" }
72 };
73 
74 size_t
75 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
76     const struct tm * __restrict t, locale_t loc)
77 {
78 	char *	p;
79 	int	warn;
80 	FIX_LOCALE(loc);
81 
82 	tzset();
83 	warn = IN_NONE;
84 	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, loc);
85 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
86 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
87 		(void) fprintf_l(stderr, loc, "\n");
88 		if (format == NULL)
89 			(void) fputs("NULL strftime format ", stderr);
90 		else	(void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
91 				format);
92 		(void) fputs("yields only two digits of years in ", stderr);
93 		if (warn == IN_SOME)
94 			(void) fputs("some locales", stderr);
95 		else if (warn == IN_THIS)
96 			(void) fputs("the current locale", stderr);
97 		else	(void) fputs("all locales", stderr);
98 		(void) fputs("\n", stderr);
99 	}
100 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
101 	if (p == s + maxsize)
102 		return (0);
103 	*p = '\0';
104 	return p - s;
105 }
106 
107 size_t
108 strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
109     const struct tm * __restrict t)
110 {
111 	return strftime_l(s, maxsize, format, t, __get_locale());
112 }
113 
114 static char *
115 _fmt(const char *format, const struct tm * const t, char *pt,
116     const char * const ptlim, int *warnp, locale_t loc)
117 {
118 	int Ealternative, Oalternative, PadIndex;
119 	struct lc_time_T *tptr = __get_current_time_locale(loc);
120 
121 	for ( ; *format; ++format) {
122 		if (*format == '%') {
123 			Ealternative = 0;
124 			Oalternative = 0;
125 			PadIndex	 = PAD_DEFAULT;
126 label:
127 			switch (*++format) {
128 			case '\0':
129 				--format;
130 				break;
131 			case 'A':
132 				pt = _add((t->tm_wday < 0 ||
133 					t->tm_wday >= DAYSPERWEEK) ?
134 					"?" : tptr->weekday[t->tm_wday],
135 					pt, ptlim);
136 				continue;
137 			case 'a':
138 				pt = _add((t->tm_wday < 0 ||
139 					t->tm_wday >= DAYSPERWEEK) ?
140 					"?" : tptr->wday[t->tm_wday],
141 					pt, ptlim);
142 				continue;
143 			case 'B':
144 				pt = _add((t->tm_mon < 0 ||
145 					t->tm_mon >= MONSPERYEAR) ?
146 					"?" : (Oalternative ? tptr->alt_month :
147 					tptr->month)[t->tm_mon],
148 					pt, ptlim);
149 				continue;
150 			case 'b':
151 			case 'h':
152 				pt = _add((t->tm_mon < 0 ||
153 					t->tm_mon >= MONSPERYEAR) ?
154 					"?" : tptr->mon[t->tm_mon],
155 					pt, ptlim);
156 				continue;
157 			case 'C':
158 				/*
159 				 * %C used to do a...
160 				 *	_fmt("%a %b %e %X %Y", t);
161 				 * ...whereas now POSIX 1003.2 calls for
162 				 * something completely different.
163 				 * (ado, 1993-05-24)
164 				 */
165 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
166 					pt, ptlim, loc);
167 				continue;
168 			case 'c':
169 				{
170 				int warn2 = IN_SOME;
171 
172 				pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, loc);
173 				if (warn2 == IN_ALL)
174 					warn2 = IN_THIS;
175 				if (warn2 > *warnp)
176 					*warnp = warn2;
177 				}
178 				continue;
179 			case 'D':
180 				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, loc);
181 				continue;
182 			case 'd':
183 				pt = _conv(t->tm_mday,
184 					fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
185 					pt, ptlim, loc);
186 				continue;
187 			case 'E':
188 				if (Ealternative || Oalternative)
189 					break;
190 				Ealternative++;
191 				goto label;
192 			case 'O':
193 				/*
194 				 * C99 locale modifiers.
195 				 * The sequences
196 				 *	%Ec %EC %Ex %EX %Ey %EY
197 				 *	%Od %oe %OH %OI %Om %OM
198 				 *	%OS %Ou %OU %OV %Ow %OW %Oy
199 				 * are supposed to provide alternate
200 				 * representations.
201 				 *
202 				 * FreeBSD extension
203 				 *      %OB
204 				 */
205 				if (Ealternative || Oalternative)
206 					break;
207 				Oalternative++;
208 				goto label;
209 			case 'e':
210 				pt = _conv(t->tm_mday,
211 					fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
212 					pt, ptlim, loc);
213 				continue;
214 			case 'F':
215 				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, loc);
216 				continue;
217 			case 'H':
218 				pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
219 					pt, ptlim, loc);
220 				continue;
221 			case 'I':
222 				pt = _conv((t->tm_hour % 12) ?
223 					(t->tm_hour % 12) : 12,
224 					fmt_padding[PAD_FMT_HMS][PadIndex],
225 					pt, ptlim, loc);
226 				continue;
227 			case 'j':
228 				pt = _conv(t->tm_yday + 1,
229 					fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
230 					pt, ptlim, loc);
231 				continue;
232 			case 'k':
233 				/*
234 				 * This used to be...
235 				 *	_conv(t->tm_hour % 12 ?
236 				 *		t->tm_hour % 12 : 12, 2, ' ');
237 				 * ...and has been changed to the below to
238 				 * match SunOS 4.1.1 and Arnold Robbins'
239 				 * strftime version 3.0. That is, "%k" and
240 				 * "%l" have been swapped.
241 				 * (ado, 1993-05-24)
242 				 */
243 				pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex],
244 					pt, ptlim, loc);
245 				continue;
246 #ifdef KITCHEN_SINK
247 			case 'K':
248 				/*
249 				** After all this time, still unclaimed!
250 				*/
251 				pt = _add("kitchen sink", pt, ptlim);
252 				continue;
253 #endif /* defined KITCHEN_SINK */
254 			case 'l':
255 				/*
256 				 * This used to be...
257 				 *	_conv(t->tm_hour, 2, ' ');
258 				 * ...and has been changed to the below to
259 				 * match SunOS 4.1.1 and Arnold Robbin's
260 				 * strftime version 3.0. That is, "%k" and
261 				 * "%l" have been swapped.
262 				 * (ado, 1993-05-24)
263 				 */
264 				pt = _conv((t->tm_hour % 12) ?
265 					(t->tm_hour % 12) : 12,
266 					fmt_padding[PAD_FMT_SHMS][PadIndex],
267 					pt, ptlim, loc);
268 				continue;
269 			case 'M':
270 				pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
271 					pt, ptlim, loc);
272 				continue;
273 			case 'm':
274 				pt = _conv(t->tm_mon + 1,
275 					fmt_padding[PAD_FMT_MONTH][PadIndex],
276 					pt, ptlim, loc);
277 				continue;
278 			case 'n':
279 				pt = _add("\n", pt, ptlim);
280 				continue;
281 			case 'p':
282 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
283 					tptr->pm : tptr->am,
284 					pt, ptlim);
285 				continue;
286 			case 'R':
287 				pt = _fmt("%H:%M", t, pt, ptlim, warnp, loc);
288 				continue;
289 			case 'r':
290 				pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
291 					warnp, loc);
292 				continue;
293 			case 'S':
294 				pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
295 					pt, ptlim, loc);
296 				continue;
297 			case 's':
298 				{
299 					struct tm	tm;
300 					char		buf[INT_STRLEN_MAXIMUM(
301 								time_t) + 1];
302 					time_t		mkt;
303 
304 					tm = *t;
305 					mkt = timeoff(&tm, t->tm_gmtoff);
306 					if (TYPE_SIGNED(time_t))
307 						(void) sprintf_l(buf, loc, "%ld",
308 							(long) mkt);
309 					else	(void) sprintf_l(buf, loc, "%lu",
310 							(unsigned long) mkt);
311 					pt = _add(buf, pt, ptlim);
312 				}
313 				continue;
314 			case 'T':
315 				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, loc);
316 				continue;
317 			case 't':
318 				pt = _add("\t", pt, ptlim);
319 				continue;
320 			case 'U':
321 				pt = _conv((t->tm_yday + DAYSPERWEEK -
322 					t->tm_wday) / DAYSPERWEEK,
323 					fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
324 					pt, ptlim, loc);
325 				continue;
326 			case 'u':
327 				/*
328 				 * From Arnold Robbins' strftime version 3.0:
329 				 * "ISO 8601: Weekday as a decimal number
330 				 * [1 (Monday) - 7]"
331 				 * (ado, 1993-05-24)
332 				 */
333 				pt = _conv((t->tm_wday == 0) ?
334 					DAYSPERWEEK : t->tm_wday,
335 					"%d", pt, ptlim, loc);
336 				continue;
337 			case 'V':	/* ISO 8601 week number */
338 			case 'G':	/* ISO 8601 year (four digits) */
339 			case 'g':	/* ISO 8601 year (two digits) */
340 /*
341  * From Arnold Robbins' strftime version 3.0: "the week number of the
342  * year (the first Monday as the first day of week 1) as a decimal number
343  * (01-53)."
344  * (ado, 1993-05-24)
345  *
346  * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
347  * "Week 01 of a year is per definition the first week which has the
348  * Thursday in this year, which is equivalent to the week which contains
349  * the fourth day of January. In other words, the first week of a new year
350  * is the week which has the majority of its days in the new year. Week 01
351  * might also contain days from the previous year and the week before week
352  * 01 of a year is the last week (52 or 53) of the previous year even if
353  * it contains days from the new year. A week starts with Monday (day 1)
354  * and ends with Sunday (day 7). For example, the first week of the year
355  * 1997 lasts from 1996-12-30 to 1997-01-05..."
356  * (ado, 1996-01-02)
357  */
358 				{
359 					int	year;
360 					int	base;
361 					int	yday;
362 					int	wday;
363 					int	w;
364 
365 					year = t->tm_year;
366 					base = TM_YEAR_BASE;
367 					yday = t->tm_yday;
368 					wday = t->tm_wday;
369 					for ( ; ; ) {
370 						int	len;
371 						int	bot;
372 						int	top;
373 
374 						len = isleap_sum(year, base) ?
375 							DAYSPERLYEAR :
376 							DAYSPERNYEAR;
377 						/*
378 						 * What yday (-3 ... 3) does
379 						 * the ISO year begin on?
380 						 */
381 						bot = ((yday + 11 - wday) %
382 							DAYSPERWEEK) - 3;
383 						/*
384 						 * What yday does the NEXT
385 						 * ISO year begin on?
386 						 */
387 						top = bot -
388 							(len % DAYSPERWEEK);
389 						if (top < -3)
390 							top += DAYSPERWEEK;
391 						top += len;
392 						if (yday >= top) {
393 							++base;
394 							w = 1;
395 							break;
396 						}
397 						if (yday >= bot) {
398 							w = 1 + ((yday - bot) /
399 								DAYSPERWEEK);
400 							break;
401 						}
402 						--base;
403 						yday += isleap_sum(year, base) ?
404 							DAYSPERLYEAR :
405 							DAYSPERNYEAR;
406 					}
407 #ifdef XPG4_1994_04_09
408 					if ((w == 52 &&
409 						t->tm_mon == TM_JANUARY) ||
410 						(w == 1 &&
411 						t->tm_mon == TM_DECEMBER))
412 							w = 53;
413 #endif /* defined XPG4_1994_04_09 */
414 					if (*format == 'V')
415 						pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
416 							pt, ptlim, loc);
417 					else if (*format == 'g') {
418 						*warnp = IN_ALL;
419 						pt = _yconv(year, base, 0, 1,
420 							pt, ptlim, loc);
421 					} else	pt = _yconv(year, base, 1, 1,
422 							pt, ptlim, loc);
423 				}
424 				continue;
425 			case 'v':
426 				/*
427 				 * From Arnold Robbins' strftime version 3.0:
428 				 * "date as dd-bbb-YYYY"
429 				 * (ado, 1993-05-24)
430 				 */
431 				pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, loc);
432 				continue;
433 			case 'W':
434 				pt = _conv((t->tm_yday + DAYSPERWEEK -
435 					(t->tm_wday ?
436 					(t->tm_wday - 1) :
437 					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
438 					fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
439 					pt, ptlim, loc);
440 				continue;
441 			case 'w':
442 				pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
443 				continue;
444 			case 'X':
445 				pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, loc);
446 				continue;
447 			case 'x':
448 				{
449 				int	warn2 = IN_SOME;
450 
451 				pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, loc);
452 				if (warn2 == IN_ALL)
453 					warn2 = IN_THIS;
454 				if (warn2 > *warnp)
455 					*warnp = warn2;
456 				}
457 				continue;
458 			case 'y':
459 				*warnp = IN_ALL;
460 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
461 					pt, ptlim, loc);
462 				continue;
463 			case 'Y':
464 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
465 					pt, ptlim, loc);
466 				continue;
467 			case 'Z':
468 #ifdef TM_ZONE
469 				if (t->TM_ZONE != NULL)
470 					pt = _add(t->TM_ZONE, pt, ptlim);
471 				else
472 #endif /* defined TM_ZONE */
473 				if (t->tm_isdst >= 0)
474 					pt = _add(tzname[t->tm_isdst != 0],
475 						pt, ptlim);
476 				/*
477 				 * C99 says that %Z must be replaced by the
478 				 * empty string if the time zone is not
479 				 * determinable.
480 				 */
481 				continue;
482 			case 'z':
483 				{
484 				int		diff;
485 				char const *	sign;
486 
487 				if (t->tm_isdst < 0)
488 					continue;
489 #ifdef TM_GMTOFF
490 				diff = t->TM_GMTOFF;
491 #else /* !defined TM_GMTOFF */
492 				/*
493 				 * C99 says that the UTC offset must
494 				 * be computed by looking only at
495 				 * tm_isdst. This requirement is
496 				 * incorrect, since it means the code
497 				 * must rely on magic (in this case
498 				 * altzone and timezone), and the
499 				 * magic might not have the correct
500 				 * offset. Doing things correctly is
501 				 * tricky and requires disobeying C99;
502 				 * see GNU C strftime for details.
503 				 * For now, punt and conform to the
504 				 * standard, even though it's incorrect.
505 				 *
506 				 * C99 says that %z must be replaced by the
507 				 * empty string if the time zone is not
508 				 * determinable, so output nothing if the
509 				 * appropriate variables are not available.
510 				 */
511 				if (t->tm_isdst == 0)
512 #ifdef USG_COMPAT
513 					diff = -timezone;
514 #else /* !defined USG_COMPAT */
515 					continue;
516 #endif /* !defined USG_COMPAT */
517 				else
518 #ifdef ALTZONE
519 					diff = -altzone;
520 #else /* !defined ALTZONE */
521 					continue;
522 #endif /* !defined ALTZONE */
523 #endif /* !defined TM_GMTOFF */
524 				if (diff < 0) {
525 					sign = "-";
526 					diff = -diff;
527 				} else
528 					sign = "+";
529 				pt = _add(sign, pt, ptlim);
530 				diff /= SECSPERMIN;
531 				diff = (diff / MINSPERHOUR) * 100 +
532 					(diff % MINSPERHOUR);
533 				pt = _conv(diff,
534 					fmt_padding[PAD_FMT_YEAR][PadIndex],
535 					pt, ptlim, loc);
536 				}
537 				continue;
538 			case '+':
539 				pt = _fmt(tptr->date_fmt, t, pt, ptlim,
540 					warnp, loc);
541 				continue;
542 			case '-':
543 				if (PadIndex != PAD_DEFAULT)
544 					break;
545 				PadIndex = PAD_LESS;
546 				goto label;
547 			case '_':
548 				if (PadIndex != PAD_DEFAULT)
549 					break;
550 				PadIndex = PAD_SPACE;
551 				goto label;
552 			case '0':
553 				if (PadIndex != PAD_DEFAULT)
554 					break;
555 				PadIndex = PAD_ZERO;
556 				goto label;
557 			case '%':
558 			/*
559 			 * X311J/88-090 (4.12.3.5): if conversion char is
560 			 * undefined, behavior is undefined. Print out the
561 			 * character itself as printf(3) also does.
562 			 */
563 			default:
564 				break;
565 			}
566 		}
567 		if (pt == ptlim)
568 			break;
569 		*pt++ = *format;
570 	}
571 	return (pt);
572 }
573 
574 static char *
575 _conv(const int n, const char * const format, char * const pt,
576     const char * const ptlim, locale_t  loc)
577 {
578 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
579 
580 	(void) sprintf_l(buf, loc, format, n);
581 	return _add(buf, pt, ptlim);
582 }
583 
584 static char *
585 _add(const char *str, char *pt, const char * const ptlim)
586 {
587 	while (pt < ptlim && (*pt = *str++) != '\0')
588 		++pt;
589 	return (pt);
590 }
591 
592 /*
593  * POSIX and the C Standard are unclear or inconsistent about
594  * what %C and %y do if the year is negative or exceeds 9999.
595  * Use the convention that %C concatenated with %y yields the
596  * same output as %Y, and that %Y contains at least 4 bytes,
597  * with more only if necessary.
598  */
599 
600 static char *
601 _yconv(const int a, const int b, const int convert_top, const int convert_yy,
602     char *pt, const char * const ptlim, locale_t  loc)
603 {
604 	register int	lead;
605 	register int	trail;
606 
607 #define	DIVISOR	100
608 	trail = a % DIVISOR + b % DIVISOR;
609 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
610 	trail %= DIVISOR;
611 	if (trail < 0 && lead > 0) {
612 		trail += DIVISOR;
613 		--lead;
614 	} else if (lead < 0 && trail > 0) {
615 		trail -= DIVISOR;
616 		++lead;
617 	}
618 	if (convert_top) {
619 		if (lead == 0 && trail < 0)
620 			pt = _add("-0", pt, ptlim);
621 		else	pt = _conv(lead, "%02d", pt, ptlim, loc);
622 	}
623 	if (convert_yy)
624 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt,
625 		     ptlim, loc);
626 	return (pt);
627 }
628