xref: /freebsd/lib/libc/stdtime/strftime.c (revision ba4c5e6950b14ce5eb69bc5d953574f203e01dc4)
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   "$FreeBSD$";
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 "namespace.h"
34 #include "private.h"
35 
36 #ifndef LIBC_SCCS
37 #ifndef lint
38 static 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 
48 static char *	_add P((const char *, char *, const char *));
49 static char *	_conv P((int, const char *, char *, const char *));
50 static char *	_fmt P((const char *, const struct tm *, char *, const char *));
51 
52 size_t strftime P((char *, size_t, const char *, const struct tm *));
53 
54 extern char *	tzname[];
55 
56 size_t
57 strftime(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 
73 static 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 
82 	for ( ; *format; ++format) {
83 		if (*format == '%') {
84 			Ealternative = 0;
85 			Oalternative = 0;
86 label:
87 			switch (*++format) {
88 			case '\0':
89 				--format;
90 				break;
91 			case 'A':
92 				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
93 					"?" : Locale->weekday[t->tm_wday],
94 					pt, ptlim);
95 				continue;
96 			case 'a':
97 				pt = _add((t->tm_wday < 0 || t->tm_wday > 6) ?
98 					"?" : Locale->wday[t->tm_wday],
99 					pt, ptlim);
100 				continue;
101 			case 'B':
102 				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
103 					"?" : (Oalternative ? Locale->alt_month :
104 					Locale->month)[t->tm_mon],
105 					pt, ptlim);
106 				continue;
107 			case 'b':
108 			case 'h':
109 				pt = _add((t->tm_mon < 0 || t->tm_mon > 11) ?
110 					"?" : Locale->mon[t->tm_mon],
111 					pt, ptlim);
112 				continue;
113 			case 'C':
114 				/*
115 				** %C used to do a...
116 				**	_fmt("%a %b %e %X %Y", t);
117 				** ...whereas now POSIX 1003.2 calls for
118 				** something completely different.
119 				** (ado, 5/24/93)
120 				*/
121 				pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
122 					"%02d", pt, ptlim);
123 				continue;
124 			case 'c':
125 				/* NOTE: c_fmt is intentionally ignored */
126 				pt = _fmt("%a %Ef %T %Y", 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 				if (!Ealternative)
163 					break;
164 				pt = _fmt(Locale->Ef_fmt, t, pt, ptlim);
165 				continue;
166 			case 'F':
167 				if (!Ealternative)
168 					break;
169 				pt = _fmt(Locale->EF_fmt, t, pt, ptlim);
170 				continue;
171 			case 'H':
172 				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
173 				continue;
174 			case 'I':
175 				pt = _conv((t->tm_hour % 12) ?
176 					(t->tm_hour % 12) : 12,
177 					"%02d", pt, ptlim);
178 				continue;
179 			case 'j':
180 				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
181 				continue;
182 			case 'k':
183 				/*
184 				** This used to be...
185 				**	_conv(t->tm_hour % 12 ?
186 				**		t->tm_hour % 12 : 12, 2, ' ');
187 				** ...and has been changed to the below to
188 				** match SunOS 4.1.1 and Arnold Robbins'
189 				** strftime version 3.0.  That is, "%k" and
190 				** "%l" have been swapped.
191 				** (ado, 5/24/93)
192 				*/
193 				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
194 				continue;
195 #ifdef KITCHEN_SINK
196 			case 'K':
197 				/*
198 				** After all this time, still unclaimed!
199 				*/
200 				pt = _add("kitchen sink", pt, ptlim);
201 				continue;
202 #endif /* defined KITCHEN_SINK */
203 			case 'l':
204 				/*
205 				** This used to be...
206 				**	_conv(t->tm_hour, 2, ' ');
207 				** ...and has been changed to the below to
208 				** match SunOS 4.1.1 and Arnold Robbin's
209 				** strftime version 3.0.  That is, "%k" and
210 				** "%l" have been swapped.
211 				** (ado, 5/24/93)
212 				*/
213 				pt = _conv((t->tm_hour % 12) ?
214 					(t->tm_hour % 12) : 12,
215 					"%2d", pt, ptlim);
216 				continue;
217 			case 'M':
218 				pt = _conv(t->tm_min, "%02d", pt, ptlim);
219 				continue;
220 			case 'm':
221 				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
222 				continue;
223 			case 'n':
224 				pt = _add("\n", pt, ptlim);
225 				continue;
226 			case 'p':
227 				pt = _add((t->tm_hour >= 12) ?
228 					Locale->pm :
229 					Locale->am,
230 					pt, ptlim);
231 				continue;
232 			case 'R':
233 				pt = _fmt("%H:%M", t, pt, ptlim);
234 				continue;
235 			case 'r':
236 				pt = _fmt("%I:%M:%S %p", t, pt, ptlim);
237 				continue;
238 			case 'S':
239 				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
240 				continue;
241 			case 's':
242 				{
243 					struct tm	tm;
244 					char		buf[INT_STRLEN_MAXIMUM(
245 								time_t) + 1];
246 					time_t		mkt;
247 
248 					tm = *t;
249 					mkt = mktime(&tm);
250 					if (TYPE_SIGNED(time_t))
251 						(void) sprintf(buf, "%ld",
252 							(long) mkt);
253 					else	(void) sprintf(buf, "%lu",
254 							(unsigned long) mkt);
255 					pt = _add(buf, pt, ptlim);
256 				}
257 				continue;
258 			case 'T':
259 				pt = _fmt("%H:%M:%S", t, pt, ptlim);
260 				continue;
261 			case 't':
262 				pt = _add("\t", pt, ptlim);
263 				continue;
264 			case 'U':
265 				pt = _conv((t->tm_yday + 7 - t->tm_wday) / 7,
266 					"%02d", pt, ptlim);
267 				continue;
268 			case 'u':
269 				/*
270 				** From Arnold Robbins' strftime version 3.0:
271 				** "ISO 8601: Weekday as a decimal number
272 				** [1 (Monday) - 7]"
273 				** (ado, 5/24/93)
274 				*/
275 				pt = _conv((t->tm_wday == 0) ? 7 : t->tm_wday,
276 					"%d", pt, ptlim);
277 				continue;
278 			case 'V':	/* ISO 8601 week number */
279 			case 'G':	/* ISO 8601 year (four digits) */
280 			case 'g':	/* ISO 8601 year (two digits) */
281 /*
282 ** From Arnold Robbins' strftime version 3.0:  "the week number of the
283 ** year (the first Monday as the first day of week 1) as a decimal number
284 ** (01-53)."
285 ** (ado, 1993-05-24)
286 **
287 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
288 ** "Week 01 of a year is per definition the first week which has the
289 ** Thursday in this year, which is equivalent to the week which contains
290 ** the fourth day of January. In other words, the first week of a new year
291 ** is the week which has the majority of its days in the new year. Week 01
292 ** might also contain days from the previous year and the week before week
293 ** 01 of a year is the last week (52 or 53) of the previous year even if
294 ** it contains days from the new year. A week starts with Monday (day 1)
295 ** and ends with Sunday (day 7).  For example, the first week of the year
296 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
297 ** (ado, 1996-01-02)
298 */
299 				{
300 					int	year;
301 					int	yday;
302 					int	wday;
303 					int	w;
304 
305 					year = t->tm_year + TM_YEAR_BASE;
306 					yday = t->tm_yday;
307 					wday = t->tm_wday;
308 					for ( ; ; ) {
309 						int	len;
310 						int	bot;
311 						int	top;
312 
313 						len = isleap(year) ?
314 							DAYSPERLYEAR :
315 							DAYSPERNYEAR;
316 						/*
317 						** What yday (-3 ... 3) does
318 						** the ISO year begin on?
319 						*/
320 						bot = ((yday + 11 - wday) %
321 							DAYSPERWEEK) - 3;
322 						/*
323 						** What yday does the NEXT
324 						** ISO year begin on?
325 						*/
326 						top = bot -
327 							(len % DAYSPERWEEK);
328 						if (top < -3)
329 							top += DAYSPERWEEK;
330 						top += len;
331 						if (yday >= top) {
332 							++year;
333 							w = 1;
334 							break;
335 						}
336 						if (yday >= bot) {
337 							w = 1 + ((yday - bot) /
338 								DAYSPERWEEK);
339 							break;
340 						}
341 						--year;
342 						yday += isleap(year) ?
343 							DAYSPERLYEAR :
344 							DAYSPERNYEAR;
345 					}
346 #ifdef XPG4_1994_04_09
347 					if ((w == 52
348 					     && t->tm_mon == TM_JANUARY)
349 					    || (w == 1
350 						&& t->tm_mon == TM_DECEMBER))
351 						w = 53;
352 #endif /* defined XPG4_1994_04_09 */
353 					if (*format == 'V')
354 						pt = _conv(w, "%02d",
355 							pt, ptlim);
356 					else if (*format == 'g') {
357 						pt = _conv(year % 100, "%02d",
358 							pt, ptlim);
359 					} else	pt = _conv(year, "%04d",
360 							pt, ptlim);
361 				}
362 				continue;
363 			case 'v':
364 				/*
365 				** From Arnold Robbins' strftime version 3.0:
366 				** "date as dd-bbb-YYYY"
367 				** (ado, 5/24/93)
368 				*/
369 				pt = _fmt("%e-%b-%Y", t, pt, ptlim);
370 				continue;
371 			case 'W':
372 				pt = _conv((t->tm_yday + 7 -
373 					(t->tm_wday ?
374 					(t->tm_wday - 1) : 6)) / 7,
375 					"%02d", pt, ptlim);
376 				continue;
377 			case 'w':
378 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
379 				continue;
380 			case 'X':
381 				pt = _fmt(Locale->X_fmt, t, pt, ptlim);
382 				continue;
383 			case 'x':
384 				pt = _fmt(Locale->x_fmt, t, pt, ptlim);
385 				continue;
386 			case 'y':
387 				pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
388 					"%02d", pt, ptlim);
389 				continue;
390 			case 'Y':
391 				pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
392 					pt, ptlim);
393 				continue;
394 			case 'Z':
395 				if (t->tm_zone != NULL)
396 					pt = _add(t->tm_zone, pt, ptlim);
397 				else
398 				if (t->tm_isdst == 0 || t->tm_isdst == 1) {
399 					pt = _add(tzname[t->tm_isdst],
400 						pt, ptlim);
401 				} else  pt = _add("?", pt, ptlim);
402 				continue;
403 			case 'z':
404 				{
405 					long absoff;
406 					if (t->tm_gmtoff >= 0) {
407 						absoff = t->tm_gmtoff;
408 						pt = _add("+", pt, ptlim);
409 					} else {
410 						absoff = -t->tm_gmtoff;
411 						pt = _add("-", pt, ptlim);
412 					}
413 					pt = _conv(absoff / 3600, "%02d",
414 						pt, ptlim);
415 					pt = _conv((absoff % 3600) / 60, "%02d",
416 						pt, ptlim);
417 				};
418 				continue;
419 			case '+':
420 				pt = _fmt(Locale->date_fmt, t, pt, ptlim);
421 				continue;
422 			case '%':
423 			/*
424 			 * X311J/88-090 (4.12.3.5): if conversion char is
425 			 * undefined, behavior is undefined.  Print out the
426 			 * character itself as printf(3) also does.
427 			 */
428 			default:
429 				break;
430 			}
431 		}
432 		if (pt == ptlim)
433 			break;
434 		*pt++ = *format;
435 	}
436 	return pt;
437 }
438 
439 static char *
440 _conv(n, format, pt, ptlim)
441 	const int n;
442 	const char *const format;
443 	char *const pt;
444 	const char *const ptlim;
445 {
446 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
447 
448 	(void) snprintf(buf, sizeof(buf), format, n);
449 	return _add(buf, pt, ptlim);
450 }
451 
452 static char *
453 _add(str, pt, ptlim)
454 	const char *str;
455 	char *pt;
456 	const char *const ptlim;
457 {
458 	while (pt < ptlim && (*pt = *str++) != '\0')
459 		++pt;
460 	return pt;
461 }
462