strftime.c revision 17141
1107120Sjulian/*
2107120Sjulian * Copyright (c) 1989 The Regents of the University of California.
3139823Simp * All rights reserved.
4139823Simp *
5139823Simp * Redistribution and use in source and binary forms are permitted
6107120Sjulian * provided that the above copyright notice and this paragraph are
7107120Sjulian * duplicated in all such forms and that any documentation,
8107120Sjulian * advertising materials, and other materials related to such
9107120Sjulian * distribution and use acknowledge that the software was developed
10107120Sjulian * by the University of California, Berkeley.  The name of the
11107120Sjulian * University may not be used to endorse or promote products derived
12107120Sjulian * from this software without specific prior written permission.
13107120Sjulian * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14107120Sjulian * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15107120Sjulian * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16107120Sjulian */
17107120Sjulian
18107120Sjulian#ifdef LIBC_RCS
19107120Sjulianstatic const char rcsid[] =
20107120Sjulian	"$Id: strftime.c,v 1.8 1996/05/27 06:54:01 scrappy Exp $";
21107120Sjulian#endif
22107120Sjulian
23107120Sjulian#ifndef lint
24107120Sjulian#ifndef NOID
25107120Sjulianstatic const char	elsieid[] = "@(#)strftime.c	7.38";
26107120Sjulian/*
27107120Sjulian** Based on the UCB version with the ID appearing below.
28107120Sjulian** This is ANSIish only when "multibyte character == plain character".
29107120Sjulian*/
30114878Sjulian#endif /* !defined NOID */
31107120Sjulian#endif /* !defined lint */
32107120Sjulian
33107120Sjulian#include "private.h"
34107120Sjulian
35122634Semax#ifndef LIBC_SCCS
36107120Sjulian#ifndef lint
37107120Sjulianstatic const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
38107120Sjulian#endif /* !defined lint */
39107120Sjulian#endif /* !defined LIBC_SCCS */
40107120Sjulian
41107120Sjulian#include "tzfile.h"
42107120Sjulian#include <fcntl.h>
43107120Sjulian#include <locale.h>
44107120Sjulian#include <rune.h>		/* for _PATH_LOCALE */
45107120Sjulian#include <sys/stat.h>
46107120Sjulian
47114878Sjulian#define LOCALE_HOME _PATH_LOCALE
48107120Sjulian
49107120Sjulianstruct lc_time_T {
50107120Sjulian	const char *	mon[12];
51107120Sjulian	const char *	month[12];
52107120Sjulian	const char *	wday[7];
53107120Sjulian	const char *	weekday[7];
54107120Sjulian	const char *	X_fmt;
55107120Sjulian	const char *	x_fmt;
56107120Sjulian	const char *	c_fmt;
57107120Sjulian	const char *	am;
58107120Sjulian	const char *	pm;
59107120Sjulian	const char *	date_fmt;
60107120Sjulian};
61107120Sjulian
62107120Sjulianstatic struct lc_time_T		localebuf;
63107120Sjulianstatic int using_locale;
64107120Sjulian
65107120Sjulian#define Locale	(using_locale ? &localebuf : &C_time_locale)
66107120Sjulian
67107120Sjulianstatic const struct lc_time_T	C_time_locale = {
68114878Sjulian	{
69114878Sjulian		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
70114878Sjulian		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
71107120Sjulian	}, {
72114878Sjulian		"January", "February", "March", "April", "May", "June",
73107120Sjulian		"July", "August", "September", "October", "November", "December"
74107120Sjulian	}, {
75107120Sjulian		"Sun", "Mon", "Tue", "Wed",
76107120Sjulian		"Thu", "Fri", "Sat"
77107120Sjulian	}, {
78114878Sjulian		"Sunday", "Monday", "Tuesday", "Wednesday",
79114878Sjulian		"Thursday", "Friday", "Saturday"
80107120Sjulian	},
81107120Sjulian
82107120Sjulian	/* X_fmt */
83107120Sjulian	"%H:%M:%S",
84107120Sjulian
85107120Sjulian	/*
86107120Sjulian	** x_fmt
87107120Sjulian	** Since the C language standard calls for
88107120Sjulian	** "date, using locale's date format," anything goes.
89107120Sjulian	** Using just numbers (as here) makes Quakers happier;
90107120Sjulian	** it's also compatible with SVR4.
91107120Sjulian	*/
92107120Sjulian	"%m/%d/%y",
93107120Sjulian
94107120Sjulian	/*
95157366Srwatson	** c_fmt (ctime-compatible)
96160549Srwatson	** Note that
97107120Sjulian	**	"%a %b %d %H:%M:%S %Y"
98107120Sjulian	** is used by Solaris 2.3.
99107120Sjulian	*/
100107120Sjulian	"%a %b %e %X %Y",
101107120Sjulian
102107120Sjulian	/* am */
103107120Sjulian	"AM",
104157370Srwatson
105107120Sjulian	/* pm */
106107120Sjulian	"PM",
107107120Sjulian
108107120Sjulian	/* date_fmt */
109107120Sjulian	"%a %b %e %X %Z %Y"
110107120Sjulian};
111107120Sjulian
112107120Sjulianstatic char *	_add P((const char *, char *, const char *));
113107120Sjulianstatic char *	_conv P((int, const char *, char *, const char *));
114107120Sjulianstatic char *	_fmt P((const char *, const struct tm *, char *, const char *));
115107120Sjulianstatic char *   _secs P((const struct tm *, char *, const char *));
116107120Sjulian
117107120Sjuliansize_t strftime P((char *, size_t, const char *, const struct tm *));
118107120Sjulian
119107120Sjulianextern char *	tzname[];
120107120Sjulian
121107120Sjuliansize_t
122107120Sjulianstrftime(s, maxsize, format, t)
123107120Sjulian	char *const s;
124107120Sjulian	const size_t maxsize;
125107120Sjulian	const char *const format;
126107120Sjulian	const struct tm *const t;
127107120Sjulian{
128107120Sjulian	char *p;
129107120Sjulian
130107120Sjulian	tzset();
131107120Sjulian	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
132107120Sjulian	if (p == s + maxsize)
133107120Sjulian		return 0;
134107120Sjulian	*p = '\0';
135107120Sjulian	return p - s;
136107120Sjulian}
137107120Sjulian
138107120Sjulianstatic char *
139107120Sjulian_fmt(format, t, pt, ptlim)
140107120Sjulian	const char *format;
141107120Sjulian	const struct tm *const t;
142107120Sjulian	char *pt;
143107120Sjulian	const char *const ptlim;
144107120Sjulian{
145107120Sjulian	for ( ; *format; ++format) {
146107120Sjulian		if (*format == '%') {
147107120Sjulianlabel:
148107120Sjulian			switch (*++format) {
149107120Sjulian			case '\0':
150107120Sjulian				--format;
151107120Sjulian				break;
152107120Sjulian			case 'A':
153107120Sjulian				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
154107120Sjulian					"?" : Locale->weekday[t->tm_wday],
155107120Sjulian					pt, ptlim);
156107120Sjulian				continue;
157107120Sjulian			case 'a':
158107120Sjulian				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
159107120Sjulian					"?" : Locale->wday[t->tm_wday],
160107120Sjulian					pt, ptlim);
161107120Sjulian				continue;
162107120Sjulian			case 'B':
163107120Sjulian				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
164107120Sjulian					"?" : Locale->month[t->tm_mon],
165107120Sjulian					pt, ptlim);
166107120Sjulian				continue;
167107120Sjulian			case 'b':
168107120Sjulian			case 'h':
169107120Sjulian				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
170107120Sjulian					"?" : Locale->mon[t->tm_mon],
171107120Sjulian					pt, ptlim);
172107120Sjulian				continue;
173107120Sjulian			case 'C':
174107120Sjulian				/*
175107120Sjulian				** %C used to do a...
176107120Sjulian				**	_fmt("%a %b %e %X %Y", t);
177107120Sjulian				** ...whereas now POSIX 1003.2 calls for
178107120Sjulian				** something completely different.
179107120Sjulian				** (ado, 5/24/93)
180107120Sjulian				*/
181107120Sjulian				pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
182107120Sjulian					"%02d", pt, ptlim);
183107120Sjulian				continue;
184107120Sjulian			case 'c':
185107120Sjulian				pt = _fmt(Locale->c_fmt, t, pt, ptlim);
186107120Sjulian				continue;
187157366Srwatson			case 'D':
188160549Srwatson				pt = _fmt("%m/%d/%y", t, pt, ptlim);
189107120Sjulian				continue;
190107120Sjulian			case 'd':
191107120Sjulian				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
192107120Sjulian				continue;
193107120Sjulian			case 'E':
194107120Sjulian			case 'O':
195107120Sjulian				/*
196107120Sjulian				** POSIX locale extensions, a la
197107120Sjulian				** Arnold Robbins' strftime version 3.0.
198157370Srwatson				** The sequences
199107120Sjulian				**	%Ec %EC %Ex %Ey %EY
200151888Srwatson				**	%Od %oe %OH %OI %Om %OM
201107120Sjulian				**	%OS %Ou %OU %OV %Ow %OW %Oy
202107120Sjulian				** are supposed to provide alternate
203107120Sjulian				** representations.
204107120Sjulian				** (ado, 5/24/93)
205107120Sjulian				*/
206107120Sjulian				goto label;
207107120Sjulian			case 'e':
208107120Sjulian				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
209107120Sjulian				continue;
210107120Sjulian			case 'H':
211				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
212				continue;
213			case 'I':
214				pt = _conv((t->tm_hour % 12) ?
215					(t->tm_hour % 12) : 12,
216					"%02d", pt, ptlim);
217				continue;
218			case 'j':
219				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
220				continue;
221			case 'k':
222				/*
223				** This used to be...
224				**	_conv(t->tm_hour % 12 ?
225				**		t->tm_hour % 12 : 12, 2, ' ');
226				** ...and has been changed to the below to
227				** match SunOS 4.1.1 and Arnold Robbins'
228				** strftime version 3.0.  That is, "%k" and
229				** "%l" have been swapped.
230				** (ado, 5/24/93)
231				*/
232				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
233				continue;
234#ifdef KITCHEN_SINK
235			case 'K':
236				/*
237				** After all this time, still unclaimed!
238				*/
239				pt = _add("kitchen sink", pt, ptlim);
240				continue;
241#endif /* defined KITCHEN_SINK */
242			case 'l':
243				/*
244				** This used to be...
245				**	_conv(t->tm_hour, 2, ' ');
246				** ...and has been changed to the below to
247				** match SunOS 4.1.1 and Arnold Robbin's
248				** strftime version 3.0.  That is, "%k" and
249				** "%l" have been swapped.
250				** (ado, 5/24/93)
251				*/
252				pt = _conv((t->tm_hour % 12) ?
253					(t->tm_hour % 12) : 12,
254					"%2d", pt, ptlim);
255				continue;
256			case 'M':
257				pt = _conv(t->tm_min, "%02d", pt, ptlim);
258				continue;
259			case 'm':
260				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
261				continue;
262			case 'n':
263				pt = _add("\n", pt, ptlim);
264				continue;
265			case 'p':
266				pt = _add((t->tm_hour >= 12) ?
267					Locale->pm :
268					Locale->am,
269					pt, ptlim);
270				continue;
271			case 'R':
272				pt = _fmt("%H:%M", t, pt, ptlim);
273				continue;
274			case 'r':
275				pt = _fmt("%I:%M:%S %p", t, pt, ptlim);
276				continue;
277			case 'S':
278				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
279				continue;
280			case 's':
281				pt = _secs(t, pt, ptlim);
282				continue;
283			case 'T':
284				pt = _fmt("%H:%M:%S", t, pt, ptlim);
285				continue;
286			case 't':
287				pt = _add("\t", pt, ptlim);
288				continue;
289			case 'U':
290				pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7,
291					"%02d", pt, ptlim);
292				continue;
293			case 'u':
294				/*
295				** From Arnold Robbins' strftime version 3.0:
296				** "ISO 8601: Weekday as a decimal number
297				** [1 (Monday) - 7]"
298				** (ado, 5/24/93)
299				*/
300				pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday,
301					"%d", pt, ptlim);
302				continue;
303			case 'V':
304				/*
305				** From Arnold Robbins' strftime version 3.0:
306				** "the week number of the year (the first
307				** Monday as the first day of week 1) as a
308				** decimal number (01-53).  The method for
309				** determining the week number is as specified
310				** by ISO 8601 (to wit: if the week containing
311				** January 1 has four or more days in the new
312				** year, then it is week 1, otherwise it is
313				** week 53 of the previous year and the next
314				** week is week 1)."
315				** (ado, 5/24/93)
316				*/
317				/*
318				** XXX--If January 1 falls on a Friday,
319				** January 1-3 are part of week 53 of the
320				** previous year.  By analogy, if January
321				** 1 falls on a Thursday, are December 29-31
322				** of the PREVIOUS year part of week 1???
323				** (ado 5/24/93)
324				*/
325				/*
326				** You are understood not to expect this.
327				*/
328				{
329					int	i;
330
331					i = (t->tm_yday + 10 - (t->tm_wday ?
332						(t->tm_wday - 1) : 6)) / 7;
333					if (i == 0) {
334						/*
335						** What day of the week does
336						** January 1 fall on?
337						*/
338						i = t->tm_wday -
339							(t->tm_yday - 1);
340						/*
341						** Fri Jan 1: 53
342						** Sun Jan 1: 52
343						** Sat Jan 1: 53 if previous
344						**		 year a leap
345						**		 year, else 52
346						*/
347						if (i == TM_FRIDAY)
348							i = 53;
349						else if (i == TM_SUNDAY)
350							i = 52;
351						else	i = isleap(t->tm_year +
352								TM_YEAR_BASE) ?
353								53 : 52;
354#ifdef XPG4_1994_04_09
355						/*
356						** As of 4/9/94, though,
357						** XPG4 calls for 53
358						** unconditionally.
359						*/
360						i = 53;
361#endif /* defined XPG4_1994_04_09 */
362					}
363					pt = _conv(i, "%02d", pt, ptlim);
364				}
365				continue;
366			case 'v':
367				/*
368				** From Arnold Robbins' strftime version 3.0:
369				** "date as dd-bbb-YYYY"
370				** (ado, 5/24/93)
371				*/
372				pt = _fmt("%e-%b-%Y", t, pt, ptlim);
373				continue;
374			case 'W':
375				pt = _conv((t->tm_yday + 7 -
376					(t->tm_wday ?
377					(t->tm_wday - 1) : 6)) / 7,
378					"%02d", pt, ptlim);
379				continue;
380			case 'w':
381				pt = _conv(t->tm_wday, "%d", pt, ptlim);
382				continue;
383			case 'X':
384				pt = _fmt(Locale->X_fmt, t, pt, ptlim);
385				continue;
386			case 'x':
387				pt = _fmt(Locale->x_fmt, t, pt, ptlim);
388				continue;
389			case 'y':
390				pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
391					"%02d", pt, ptlim);
392				continue;
393			case 'Y':
394				pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
395					pt, ptlim);
396				continue;
397			case 'Z':
398				if (t->tm_zone != NULL)
399					pt = _add(t->tm_zone, pt, ptlim);
400				else
401				if (t->tm_isdst == 0 || t->tm_isdst == 1) {
402					pt = _add(tzname[t->tm_isdst],
403						pt, ptlim);
404				} else  pt = _add("?", pt, ptlim);
405				continue;
406			case '+':
407				pt = _fmt(Locale->date_fmt, t, pt, ptlim);
408				continue;
409			case '%':
410			/*
411			 * X311J/88-090 (4.12.3.5): if conversion char is
412			 * undefined, behavior is undefined.  Print out the
413			 * character itself as printf(3) also does.
414			 */
415			default:
416				break;
417			}
418		}
419		if (pt == ptlim)
420			break;
421		*pt++ = *format;
422	}
423	return pt;
424}
425
426static char *
427_conv(n, format, pt, ptlim)
428	const int n;
429	const char *const format;
430	char *const pt;
431	const char *const ptlim;
432{
433	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
434
435	(void) sprintf(buf, format, n);
436	return _add(buf, pt, ptlim);
437}
438
439static char *
440_secs(t, pt, ptlim)
441	const struct tm *t;
442	char *pt;
443	const char *ptlim;
444{
445	char    buf[INT_STRLEN_MAXIMUM(int) + 1];
446	register time_t s;
447	struct tm tmp;
448
449	/* Make a copy, mktime(3) modifies the tm struct. */
450	tmp = *t;
451	s = mktime(&tmp);
452	(void) sprintf(buf, "%ld", s);
453	return _add(buf, pt, ptlim);
454}
455
456static char *
457_add(str, pt, ptlim)
458	const char *str;
459	char *pt;
460	const char *const ptlim;
461{
462	while (pt < ptlim && (*pt = *str++) != '\0')
463		++pt;
464	return pt;
465}
466
467extern char *_PathLocale;
468
469int
470__time_load_locale(const char *name)
471{
472	static const char	lc_time[] = "LC_TIME";
473	static char *		locale_buf;
474	static char		locale_buf_C[] = "C";
475
476	int			fd;
477	char *			lbuf;
478	char *			p;
479	const char **		ap;
480	const char *		plim;
481	char			filename[FILENAME_MAX];
482	struct stat		st;
483	size_t			namesize;
484	size_t			bufsize;
485	int                     save_using_locale;
486
487	save_using_locale = using_locale;
488	using_locale = 0;
489
490	if (name == NULL)
491		goto no_locale;
492
493	if (!*name || !strcmp(name, "C") || !strcmp(name, "POSIX"))
494		return 0;
495
496	/*
497	** If the locale name is the same as our cache, use the cache.
498	*/
499	lbuf = locale_buf;
500	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
501		p = lbuf;
502		for (ap = (const char **) &localebuf;
503			ap < (const char **) (&localebuf + 1);
504				++ap)
505					*ap = p += strlen(p) + 1;
506		using_locale = 1;
507		return 0;
508	}
509	/*
510	** Slurp the locale file into the cache.
511	*/
512	namesize = strlen(name) + 1;
513
514	snprintf(filename, sizeof filename,
515		 "%s/%s/%s",
516		 _PathLocale, name, lc_time);
517	fd = open(filename, O_RDONLY);
518	if (fd < 0)
519		goto no_locale;
520	if (fstat(fd, &st) != 0)
521		goto bad_locale;
522	if (st.st_size <= 0)
523		goto bad_locale;
524	bufsize = namesize + st.st_size;
525	locale_buf = NULL;
526	lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
527		malloc(bufsize) : realloc(lbuf, bufsize);
528	if (lbuf == NULL)
529		goto bad_locale;
530	(void) strcpy(lbuf, name);
531	p = lbuf + namesize;
532	plim = p + st.st_size;
533	if (read(fd, p, (size_t) st.st_size) != st.st_size)
534		goto bad_lbuf;
535	if (close(fd) != 0)
536		goto bad_lbuf;
537	/*
538	** Parse the locale file into localebuf.
539	*/
540	if (plim[-1] != '\n')
541		goto bad_lbuf;
542	for (ap = (const char **) &localebuf;
543		ap < (const char **) (&localebuf + 1);
544			++ap) {
545				if (p == plim)
546					goto reset_locale;
547				*ap = p;
548				while (*p != '\n')
549					++p;
550				*p++ = '\0';
551	}
552	/*
553	** Record the successful parse in the cache.
554	*/
555	locale_buf = lbuf;
556
557	using_locale = 1;
558	return 0;
559
560reset_locale:
561	/*
562	 * XXX - This may not be the correct thing to do in this case.
563	 * setlocale() assumes that we left the old locale alone.
564	 */
565	locale_buf = locale_buf_C;
566	localebuf = C_time_locale;
567	save_using_locale = 0;
568bad_lbuf:
569	free(lbuf);
570bad_locale:
571	(void) close(fd);
572no_locale:
573	using_locale = save_using_locale;
574	return -1;
575}
576