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