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