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