xref: /freebsd/lib/libc/stdtime/strftime.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
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 #ifdef LIBC_RCS
19 static const char rcsid[] =
20 	"$Id: strftime.c,v 1.5 1995/08/07 23:35:41 ache Exp $";
21 #endif
22 
23 #ifndef lint
24 #ifndef NOID
25 static const char	elsieid[] = "@(#)strftime.c	7.38";
26 /*
27 ** Based on the UCB version with the ID appearing below.
28 ** This is ANSIish only when "multibyte character == plain character".
29 */
30 #endif /* !defined NOID */
31 #endif /* !defined lint */
32 
33 #include "private.h"
34 
35 #ifndef LIBC_SCCS
36 #ifndef lint
37 static const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
38 #endif /* !defined lint */
39 #endif /* !defined LIBC_SCCS */
40 
41 #include "tzfile.h"
42 #include <fcntl.h>
43 #include <locale.h>
44 #include <rune.h>		/* for _PATH_LOCALE */
45 #include <sys/stat.h>
46 
47 #define LOCALE_HOME _PATH_LOCALE
48 
49 struct lc_time_T {
50 	const char *	mon[12];
51 	const char *	month[12];
52 	const char *	wday[7];
53 	const char *	weekday[7];
54 	const char *	X_fmt;
55 	const char *	x_fmt;
56 	const char *	c_fmt;
57 	const char *	am;
58 	const char *	pm;
59 	const char *	date_fmt;
60 };
61 
62 static struct lc_time_T		localebuf;
63 static struct lc_time_T *	_loc P((void));
64 static int using_locale;
65 
66 #define Locale	(using_locale ? &localebuf : &C_time_locale)
67 
68 static const struct lc_time_T	C_time_locale = {
69 	{
70 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
71 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
72 	}, {
73 		"January", "February", "March", "April", "May", "June",
74 		"July", "August", "September", "October", "November", "December"
75 	}, {
76 		"Sun", "Mon", "Tue", "Wed",
77 		"Thu", "Fri", "Sat"
78 	}, {
79 		"Sunday", "Monday", "Tuesday", "Wednesday",
80 		"Thursday", "Friday", "Saturday"
81 	},
82 
83 	/* X_fmt */
84 	"%H:%M:%S",
85 
86 	/*
87 	** x_fmt
88 	** Since the C language standard calls for
89 	** "date, using locale's date format," anything goes.
90 	** Using just numbers (as here) makes Quakers happier;
91 	** it's also compatible with SVR4.
92 	*/
93 	"%m/%d/%y",
94 
95 	/*
96 	** c_fmt (ctime-compatible)
97 	** Note that
98 	**	"%a %b %d %H:%M:%S %Y"
99 	** is used by Solaris 2.3.
100 	*/
101 	"%a %b %e %X %Y",
102 
103 	/* am */
104 	"AM",
105 
106 	/* pm */
107 	"PM",
108 
109 	/* date_fmt */
110 	"%a %b %e %X %Z %Y"
111 };
112 
113 static char *	_add P((const char *, char *, const char *));
114 static char *	_conv P((int, const char *, char *, const char *));
115 static char *	_fmt P((const char *, const struct tm *, char *, const char *));
116 static char *   _secs P((const struct tm *, char *, const char *));
117 
118 size_t strftime P((char *, size_t, const char *, const struct tm *));
119 
120 extern char *	tzname[];
121 
122 size_t
123 strftime(s, maxsize, format, t)
124 	char *const s;
125 	const size_t maxsize;
126 	const char *const format;
127 	const struct tm *const t;
128 {
129 	char *p;
130 
131 	tzset();
132 	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
133 	if (p == s + maxsize)
134 		return 0;
135 	*p = '\0';
136 	return p - s;
137 }
138 
139 static char *
140 _fmt(format, t, pt, ptlim)
141 	const char *format;
142 	const struct tm *const t;
143 	char *pt;
144 	const char *const ptlim;
145 {
146 	for ( ; *format; ++format) {
147 		if (*format == '%') {
148 label:
149 			switch (*++format) {
150 			case '\0':
151 				--format;
152 				break;
153 			case 'A':
154 				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
155 					"?" : Locale->weekday[t->tm_wday],
156 					pt, ptlim);
157 				continue;
158 			case 'a':
159 				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
160 					"?" : Locale->wday[t->tm_wday],
161 					pt, ptlim);
162 				continue;
163 			case 'B':
164 				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
165 					"?" : Locale->month[t->tm_mon],
166 					pt, ptlim);
167 				continue;
168 			case 'b':
169 			case 'h':
170 				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
171 					"?" : Locale->mon[t->tm_mon],
172 					pt, ptlim);
173 				continue;
174 			case 'C':
175 				/*
176 				** %C used to do a...
177 				**	_fmt("%a %b %e %X %Y", t);
178 				** ...whereas now POSIX 1003.2 calls for
179 				** something completely different.
180 				** (ado, 5/24/93)
181 				*/
182 				pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
183 					"%02d", pt, ptlim);
184 				continue;
185 			case 'c':
186 				pt = _fmt(Locale->c_fmt, t, pt, ptlim);
187 				continue;
188 			case 'D':
189 				pt = _fmt("%m/%d/%y", t, pt, ptlim);
190 				continue;
191 			case 'd':
192 				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
193 				continue;
194 			case 'E':
195 			case 'O':
196 				/*
197 				** POSIX locale extensions, a la
198 				** Arnold Robbins' strftime version 3.0.
199 				** The sequences
200 				**	%Ec %EC %Ex %Ey %EY
201 				**	%Od %oe %OH %OI %Om %OM
202 				**	%OS %Ou %OU %OV %Ow %OW %Oy
203 				** are supposed to provide alternate
204 				** representations.
205 				** (ado, 5/24/93)
206 				*/
207 				goto label;
208 			case 'e':
209 				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
210 				continue;
211 			case 'H':
212 				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
213 				continue;
214 			case 'I':
215 				pt = _conv((t->tm_hour % 12) ?
216 					(t->tm_hour % 12) : 12,
217 					"%02d", pt, ptlim);
218 				continue;
219 			case 'j':
220 				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
221 				continue;
222 			case 'k':
223 				/*
224 				** This used to be...
225 				**	_conv(t->tm_hour % 12 ?
226 				**		t->tm_hour % 12 : 12, 2, ' ');
227 				** ...and has been changed to the below to
228 				** match SunOS 4.1.1 and Arnold Robbins'
229 				** strftime version 3.0.  That is, "%k" and
230 				** "%l" have been swapped.
231 				** (ado, 5/24/93)
232 				*/
233 				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
234 				continue;
235 #ifdef KITCHEN_SINK
236 			case 'K':
237 				/*
238 				** After all this time, still unclaimed!
239 				*/
240 				pt = _add("kitchen sink", pt, ptlim);
241 				continue;
242 #endif /* defined KITCHEN_SINK */
243 			case 'l':
244 				/*
245 				** This used to be...
246 				**	_conv(t->tm_hour, 2, ' ');
247 				** ...and has been changed to the below to
248 				** match SunOS 4.1.1 and Arnold Robbin's
249 				** strftime version 3.0.  That is, "%k" and
250 				** "%l" have been swapped.
251 				** (ado, 5/24/93)
252 				*/
253 				pt = _conv((t->tm_hour % 12) ?
254 					(t->tm_hour % 12) : 12,
255 					"%2d", pt, ptlim);
256 				continue;
257 			case 'M':
258 				pt = _conv(t->tm_min, "%02d", pt, ptlim);
259 				continue;
260 			case 'm':
261 				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
262 				continue;
263 			case 'n':
264 				pt = _add("\n", pt, ptlim);
265 				continue;
266 			case 'p':
267 				pt = _add((t->tm_hour >= 12) ?
268 					Locale->pm :
269 					Locale->am,
270 					pt, ptlim);
271 				continue;
272 			case 'R':
273 				pt = _fmt("%H:%M", t, pt, ptlim);
274 				continue;
275 			case 'r':
276 				pt = _fmt("%I:%M:%S %p", t, pt, ptlim);
277 				continue;
278 			case 'S':
279 				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
280 				continue;
281 			case 's':
282 				pt = _secs(t, pt, ptlim);
283 				continue;
284 			case 'T':
285 				pt = _fmt("%H:%M:%S", t, pt, ptlim);
286 				continue;
287 			case 't':
288 				pt = _add("\t", pt, ptlim);
289 				continue;
290 			case 'U':
291 				pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7,
292 					"%02d", pt, ptlim);
293 				continue;
294 			case 'u':
295 				/*
296 				** From Arnold Robbins' strftime version 3.0:
297 				** "ISO 8601: Weekday as a decimal number
298 				** [1 (Monday) - 7]"
299 				** (ado, 5/24/93)
300 				*/
301 				pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday,
302 					"%d", pt, ptlim);
303 				continue;
304 			case 'V':
305 				/*
306 				** From Arnold Robbins' strftime version 3.0:
307 				** "the week number of the year (the first
308 				** Monday as the first day of week 1) as a
309 				** decimal number (01-53).  The method for
310 				** determining the week number is as specified
311 				** by ISO 8601 (to wit: if the week containing
312 				** January 1 has four or more days in the new
313 				** year, then it is week 1, otherwise it is
314 				** week 53 of the previous year and the next
315 				** week is week 1)."
316 				** (ado, 5/24/93)
317 				*/
318 				/*
319 				** XXX--If January 1 falls on a Friday,
320 				** January 1-3 are part of week 53 of the
321 				** previous year.  By analogy, if January
322 				** 1 falls on a Thursday, are December 29-31
323 				** of the PREVIOUS year part of week 1???
324 				** (ado 5/24/93)
325 				*/
326 				/*
327 				** You are understood not to expect this.
328 				*/
329 				{
330 					int	i;
331 
332 					i = (t->tm_yday + 10 - (t->tm_wday ?
333 						(t->tm_wday - 1) : 6)) / 7;
334 					if (i == 0) {
335 						/*
336 						** What day of the week does
337 						** January 1 fall on?
338 						*/
339 						i = t->tm_wday -
340 							(t->tm_yday - 1);
341 						/*
342 						** Fri Jan 1: 53
343 						** Sun Jan 1: 52
344 						** Sat Jan 1: 53 if previous
345 						**		 year a leap
346 						**		 year, else 52
347 						*/
348 						if (i == TM_FRIDAY)
349 							i = 53;
350 						else if (i == TM_SUNDAY)
351 							i = 52;
352 						else	i = isleap(t->tm_year +
353 								TM_YEAR_BASE) ?
354 								53 : 52;
355 #ifdef XPG4_1994_04_09
356 						/*
357 						** As of 4/9/94, though,
358 						** XPG4 calls for 53
359 						** unconditionally.
360 						*/
361 						i = 53;
362 #endif /* defined XPG4_1994_04_09 */
363 					}
364 					pt = _conv(i, "%02d", 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, 5/24/93)
372 				*/
373 				pt = _fmt("%e-%b-%Y", t, pt, ptlim);
374 				continue;
375 			case 'W':
376 				pt = _conv((t->tm_yday + 7 -
377 					(t->tm_wday ?
378 					(t->tm_wday - 1) : 6)) / 7,
379 					"%02d", pt, ptlim);
380 				continue;
381 			case 'w':
382 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
383 				continue;
384 			case 'X':
385 				pt = _fmt(Locale->X_fmt, t, pt, ptlim);
386 				continue;
387 			case 'x':
388 				pt = _fmt(Locale->x_fmt, t, pt, ptlim);
389 				continue;
390 			case 'y':
391 				pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
392 					"%02d", pt, ptlim);
393 				continue;
394 			case 'Y':
395 				pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
396 					pt, ptlim);
397 				continue;
398 			case 'Z':
399 				if (t->tm_zone != NULL)
400 					pt = _add(t->tm_zone, pt, ptlim);
401 				else
402 				if (t->tm_isdst == 0 || t->tm_isdst == 1) {
403 					pt = _add(tzname[t->tm_isdst],
404 						pt, ptlim);
405 				} else  pt = _add("?", pt, ptlim);
406 				continue;
407 			case '+':
408 				pt = _fmt(Locale->date_fmt, t, pt, ptlim);
409 				continue;
410 			case '%':
411 			/*
412 			 * X311J/88-090 (4.12.3.5): if conversion char is
413 			 * undefined, behavior is undefined.  Print out the
414 			 * character itself as printf(3) also does.
415 			 */
416 			default:
417 				break;
418 			}
419 		}
420 		if (pt == ptlim)
421 			break;
422 		*pt++ = *format;
423 	}
424 	return pt;
425 }
426 
427 static char *
428 _conv(n, format, pt, ptlim)
429 	const int n;
430 	const char *const format;
431 	char *const pt;
432 	const char *const ptlim;
433 {
434 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
435 
436 	(void) sprintf(buf, format, n);
437 	return _add(buf, pt, ptlim);
438 }
439 
440 static char *
441 _secs(t, pt, ptlim)
442 	const struct tm *t;
443 	char *pt;
444 	const char *ptlim;
445 {
446 	char    buf[INT_STRLEN_MAXIMUM(int) + 1];
447 	register time_t s;
448 	struct tm tmp;
449 
450 	/* Make a copy, mktime(3) modifies the tm struct. */
451 	tmp = *t;
452 	s = mktime(&tmp);
453 	(void) sprintf(buf, "%ld", s);
454 	return _add(buf, pt, ptlim);
455 }
456 
457 static char *
458 _add(str, pt, ptlim)
459 	const char *str;
460 	char *pt;
461 	const char *const ptlim;
462 {
463 	while (pt < ptlim && (*pt = *str++) != '\0')
464 		++pt;
465 	return pt;
466 }
467 
468 extern char *_PathLocale;
469 
470 int
471 __time_load_locale(const char *name)
472 {
473 	static const char	lc_time[] = "LC_TIME";
474 	static char *		locale_buf;
475 	static char		locale_buf_C[] = "C";
476 
477 	int			fd;
478 	char *			lbuf;
479 	char *			p;
480 	const char **		ap;
481 	const char *		plim;
482 	char			filename[FILENAME_MAX];
483 	struct stat		st;
484 	size_t			namesize;
485 	size_t			bufsize;
486 	int                     save_using_locale;
487 
488 	save_using_locale = using_locale;
489 	using_locale = 0;
490 
491 	if (name == NULL)
492 		goto no_locale;
493 
494 	if (!*name || !strcmp(name, "C") || !strcmp(name, "POSIX"))
495 		return 0;
496 
497 	/*
498 	** If the locale name is the same as our cache, use the cache.
499 	*/
500 	lbuf = locale_buf;
501 	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
502 		p = lbuf;
503 		for (ap = (const char **) &localebuf;
504 			ap < (const char **) (&localebuf + 1);
505 				++ap)
506 					*ap = p += strlen(p) + 1;
507 		using_locale = 1;
508 		return 0;
509 	}
510 	/*
511 	** Slurp the locale file into the cache.
512 	*/
513 	namesize = strlen(name) + 1;
514 
515 	snprintf(filename, sizeof filename,
516 		 "%s/%s/%s",
517 		 _PathLocale, name, lc_time);
518 	fd = open(filename, O_RDONLY);
519 	if (fd < 0)
520 		goto no_locale;
521 	if (fstat(fd, &st) != 0)
522 		goto bad_locale;
523 	if (st.st_size <= 0)
524 		goto bad_locale;
525 	bufsize = namesize + st.st_size;
526 	locale_buf = NULL;
527 	lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
528 		malloc(bufsize) : realloc(lbuf, bufsize);
529 	if (lbuf == NULL)
530 		goto bad_locale;
531 	(void) strcpy(lbuf, name);
532 	p = lbuf + namesize;
533 	plim = p + st.st_size;
534 	if (read(fd, p, (size_t) st.st_size) != st.st_size)
535 		goto bad_lbuf;
536 	if (close(fd) != 0)
537 		goto bad_lbuf;
538 	/*
539 	** Parse the locale file into localebuf.
540 	*/
541 	if (plim[-1] != '\n')
542 		goto bad_lbuf;
543 	for (ap = (const char **) &localebuf;
544 		ap < (const char **) (&localebuf + 1);
545 			++ap) {
546 				if (p == plim)
547 					goto reset_locale;
548 				*ap = p;
549 				while (*p != '\n')
550 					++p;
551 				*p++ = '\0';
552 	}
553 	/*
554 	** Record the successful parse in the cache.
555 	*/
556 	locale_buf = lbuf;
557 
558 	using_locale = 1;
559 	return 0;
560 
561 reset_locale:
562 	/*
563 	 * XXX - This may not be the correct thing to do in this case.
564 	 * setlocale() assumes that we left the old locale alone.
565 	 */
566 	locale_buf = locale_buf_C;
567 	localebuf = C_time_locale;
568 	save_using_locale = 0;
569 bad_lbuf:
570 	free(lbuf);
571 bad_locale:
572 	(void) close(fd);
573 no_locale:
574 	using_locale = save_using_locale;
575 	return -1;
576 }
577