strftime.c revision 74578
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
19static const char rcsid[] =
20  "$FreeBSD: head/lib/libc/stdtime/strftime.c 74578 2001-03-21 14:52:12Z ache $";
21#endif
22
23#ifndef lint
24#ifndef NOID
25static 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 "namespace.h"
34#include "private.h"
35
36#ifndef LIBC_SCCS
37#ifndef lint
38static const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
39#endif /* !defined lint */
40#endif /* !defined LIBC_SCCS */
41
42#include "tzfile.h"
43#include <fcntl.h>
44#include <sys/stat.h>
45#include "un-namespace.h"
46#include "timelocal.h"
47
48static char *	_add P((const char *, char *, const char *));
49static char *	_conv P((int, const char *, char *, const char *));
50static char *	_fmt P((const char *, const struct tm *, char *, const char *));
51
52size_t strftime P((char *, size_t, const char *, const struct tm *));
53
54extern char *	tzname[];
55
56size_t
57strftime(s, maxsize, format, t)
58	char *const s;
59	const size_t maxsize;
60	const char *const format;
61	const struct tm *const t;
62{
63	char *p;
64
65	tzset();
66	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize);
67	if (p == s + maxsize)
68		return 0;
69	*p = '\0';
70	return p - s;
71}
72
73static char *
74_fmt(format, t, pt, ptlim)
75	const char *format;
76	const struct tm *const t;
77	char *pt;
78	const char *const ptlim;
79{
80	int Ealternative, Oalternative;
81	struct lc_time_T *tptr = __get_current_time_locale();
82
83	for ( ; *format; ++format) {
84		if (*format == '%') {
85			Ealternative = 0;
86			Oalternative = 0;
87label:
88			switch (*++format) {
89			case '\0':
90				--format;
91				break;
92			case 'A':
93				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
94					"?" : tptr->weekday[t->tm_wday],
95					pt, ptlim);
96				continue;
97			case 'a':
98				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
99					"?" : tptr->wday[t->tm_wday],
100					pt, ptlim);
101				continue;
102			case 'B':
103				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
104					"?" : (Oalternative ? tptr->alt_month :
105					tptr->month)[t->tm_mon],
106					pt, ptlim);
107				continue;
108			case 'b':
109			case 'h':
110				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
111					"?" : tptr->mon[t->tm_mon],
112					pt, ptlim);
113				continue;
114			case 'C':
115				/*
116				** %C used to do a...
117				**	_fmt("%a %b %e %X %Y", t);
118				** ...whereas now POSIX 1003.2 calls for
119				** something completely different.
120				** (ado, 5/24/93)
121				*/
122				pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
123					"%02d", pt, ptlim);
124				continue;
125			case 'c':
126				pt = _fmt(tptr->c_fmt, t, pt, ptlim);
127				continue;
128			case 'D':
129				pt = _fmt("%m/%d/%y", t, pt, ptlim);
130				continue;
131			case 'd':
132				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
133				continue;
134			case 'E':
135				if (Ealternative || Oalternative)
136					break;
137				Ealternative++;
138				goto label;
139			case 'O':
140				/*
141				** POSIX locale extensions, a la
142				** Arnold Robbins' strftime version 3.0.
143				** The sequences
144				**      %Ec %EC %Ex %EX %Ey %EY
145				**	%Od %oe %OH %OI %Om %OM
146				**	%OS %Ou %OU %OV %Ow %OW %Oy
147				** are supposed to provide alternate
148				** representations.
149				** (ado, 5/24/93)
150				**
151				** FreeBSD extensions
152				**      %OB %Ef %EF
153				*/
154				if (Ealternative || Oalternative)
155					break;
156				Oalternative++;
157				goto label;
158			case 'e':
159				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
160				continue;
161			case 'F':
162				pt = _fmt("%Y-%m-%d", t, pt, ptlim);
163				continue;
164			case 'H':
165				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
166				continue;
167			case 'I':
168				pt = _conv((t->tm_hour % 12) ?
169					(t->tm_hour % 12) : 12,
170					"%02d", pt, ptlim);
171				continue;
172			case 'j':
173				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
174				continue;
175			case 'k':
176				/*
177				** This used to be...
178				**	_conv(t->tm_hour % 12 ?
179				**		t->tm_hour % 12 : 12, 2, ' ');
180				** ...and has been changed to the below to
181				** match SunOS 4.1.1 and Arnold Robbins'
182				** strftime version 3.0.  That is, "%k" and
183				** "%l" have been swapped.
184				** (ado, 5/24/93)
185				*/
186				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
187				continue;
188#ifdef KITCHEN_SINK
189			case 'K':
190				/*
191				** After all this time, still unclaimed!
192				*/
193				pt = _add("kitchen sink", pt, ptlim);
194				continue;
195#endif /* defined KITCHEN_SINK */
196			case 'l':
197				/*
198				** This used to be...
199				**	_conv(t->tm_hour, 2, ' ');
200				** ...and has been changed to the below to
201				** match SunOS 4.1.1 and Arnold Robbin's
202				** strftime version 3.0.  That is, "%k" and
203				** "%l" have been swapped.
204				** (ado, 5/24/93)
205				*/
206				pt = _conv((t->tm_hour % 12) ?
207					(t->tm_hour % 12) : 12,
208					"%2d", pt, ptlim);
209				continue;
210			case 'M':
211				pt = _conv(t->tm_min, "%02d", pt, ptlim);
212				continue;
213			case 'm':
214				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
215				continue;
216			case 'n':
217				pt = _add("\n", pt, ptlim);
218				continue;
219			case 'p':
220				pt = _add((t->tm_hour >= 12) ?
221					tptr->pm :
222					tptr->am,
223					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, "%02d", pt, ptlim);
233				continue;
234			case 's':
235				{
236					struct tm	tm;
237					char		buf[INT_STRLEN_MAXIMUM(
238								time_t) + 1];
239					time_t		mkt;
240
241					tm = *t;
242					mkt = mktime(&tm);
243					if (TYPE_SIGNED(time_t))
244						(void) sprintf(buf, "%ld",
245							(long) mkt);
246					else	(void) sprintf(buf, "%lu",
247							(unsigned long) mkt);
248					pt = _add(buf, pt, ptlim);
249				}
250				continue;
251			case 'T':
252				pt = _fmt("%H:%M:%S", t, pt, ptlim);
253				continue;
254			case 't':
255				pt = _add("\t", pt, ptlim);
256				continue;
257			case 'U':
258				pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7,
259					"%02d", pt, ptlim);
260				continue;
261			case 'u':
262				/*
263				** From Arnold Robbins' strftime version 3.0:
264				** "ISO 8601: Weekday as a decimal number
265				** [1 (Monday) - 7]"
266				** (ado, 5/24/93)
267				*/
268				pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday,
269					"%d", pt, ptlim);
270				continue;
271			case 'V':	/* ISO 8601 week number */
272			case 'G':	/* ISO 8601 year (four digits) */
273			case 'g':	/* ISO 8601 year (two digits) */
274/*
275** From Arnold Robbins' strftime version 3.0:  "the week number of the
276** year (the first Monday as the first day of week 1) as a decimal number
277** (01-53)."
278** (ado, 1993-05-24)
279**
280** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
281** "Week 01 of a year is per definition the first week which has the
282** Thursday in this year, which is equivalent to the week which contains
283** the fourth day of January. In other words, the first week of a new year
284** is the week which has the majority of its days in the new year. Week 01
285** might also contain days from the previous year and the week before week
286** 01 of a year is the last week (52 or 53) of the previous year even if
287** it contains days from the new year. A week starts with Monday (day 1)
288** and ends with Sunday (day 7).  For example, the first week of the year
289** 1997 lasts from 1996-12-30 to 1997-01-05..."
290** (ado, 1996-01-02)
291*/
292				{
293					int	year;
294					int	yday;
295					int	wday;
296					int	w;
297
298					year = t->tm_year + TM_YEAR_BASE;
299					yday = t->tm_yday;
300					wday = t->tm_wday;
301					for ( ; ; ) {
302						int	len;
303						int	bot;
304						int	top;
305
306						len = isleap(year) ?
307							DAYSPERLYEAR :
308							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 -
320							(len % DAYSPERWEEK);
321						if (top < -3)
322							top += DAYSPERWEEK;
323						top += len;
324						if (yday >= top) {
325							++year;
326							w = 1;
327							break;
328						}
329						if (yday >= bot) {
330							w = 1 + ((yday - bot) /
331								DAYSPERWEEK);
332							break;
333						}
334						--year;
335						yday += isleap(year) ?
336							DAYSPERLYEAR :
337							DAYSPERNYEAR;
338					}
339#ifdef XPG4_1994_04_09
340					if ((w == 52
341					     && t->tm_mon == TM_JANUARY)
342					    || (w == 1
343						&& t->tm_mon == TM_DECEMBER))
344						w = 53;
345#endif /* defined XPG4_1994_04_09 */
346					if (*format == 'V')
347						pt = _conv(w, "%02d",
348							pt, ptlim);
349					else if (*format == 'g') {
350						pt = _conv(year % 100, "%02d",
351							pt, ptlim);
352					} else	pt = _conv(year, "%04d",
353							pt, ptlim);
354				}
355				continue;
356			case 'v':
357				/*
358				** From Arnold Robbins' strftime version 3.0:
359				** "date as dd-bbb-YYYY"
360				** (ado, 5/24/93)
361				*/
362				pt = _fmt("%e-%b-%Y", t, pt, ptlim);
363				continue;
364			case 'W':
365				pt = _conv((t->tm_yday + 7 -
366					(t->tm_wday ?
367					(t->tm_wday - 1) : 6)) / 7,
368					"%02d", 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 = _conv((t->tm_year + TM_YEAR_BASE) % 100,
381					"%02d", pt, ptlim);
382				continue;
383			case 'Y':
384				pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
385					pt, ptlim);
386				continue;
387			case 'Z':
388				if (t->tm_zone != NULL)
389					pt = _add(t->tm_zone, pt, ptlim);
390				else
391				if (t->tm_isdst == 0 || t->tm_isdst == 1) {
392					pt = _add(tzname[t->tm_isdst],
393						pt, ptlim);
394				} else  pt = _add("?", pt, ptlim);
395				continue;
396			case 'z':
397				{
398					long absoff;
399					if (t->tm_gmtoff >= 0) {
400						absoff = t->tm_gmtoff;
401						pt = _add("+", pt, ptlim);
402					} else {
403						absoff = -t->tm_gmtoff;
404						pt = _add("-", pt, ptlim);
405					}
406					pt = _conv(absoff / 3600, "%02d",
407						pt, ptlim);
408					pt = _conv((absoff % 3600) / 60, "%02d",
409						pt, ptlim);
410				};
411				continue;
412			case '+':
413				pt = _fmt(tptr->date_fmt, t, pt, ptlim);
414				continue;
415			case '%':
416			/*
417			 * X311J/88-090 (4.12.3.5): if conversion char is
418			 * undefined, behavior is undefined.  Print out the
419			 * character itself as printf(3) also does.
420			 */
421			default:
422				break;
423			}
424		}
425		if (pt == ptlim)
426			break;
427		*pt++ = *format;
428	}
429	return pt;
430}
431
432static char *
433_conv(n, format, pt, ptlim)
434	const int n;
435	const char *const format;
436	char *const pt;
437	const char *const ptlim;
438{
439	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
440
441	(void) sprintf(buf, format, n);
442	return _add(buf, pt, ptlim);
443}
444
445static char *
446_add(str, pt, ptlim)
447	const char *str;
448	char *pt;
449	const char *const ptlim;
450{
451	while (pt < ptlim && (*pt = *str++) != '\0')
452		++pt;
453	return pt;
454}
455