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