xref: /openbsd-src/lib/libc/time/wcsftime.c (revision 8095dfd3b09a86aedce8b1957c816c36afdc20be)
1 /*	$OpenBSD: wcsftime.c,v 1.2 2013/01/20 20:29:02 millert Exp $ */
2 #include "private.h"
3 
4 /*
5 ** Based on the UCB version with the ID appearing below.
6 ** This is ANSIish only when "multibyte character == plain character".
7 **
8 ** Copyright (c) 1989, 1993
9 **	The Regents of the University of California.  All rights reserved.
10 **
11 ** Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions
13 ** are met:
14 ** 1. Redistributions of source code must retain the above copyright
15 **    notice, this list of conditions and the following disclaimer.
16 ** 2. Redistributions in binary form must reproduce the above copyright
17 **    notice, this list of conditions and the following disclaimer in the
18 **    documentation and/or other materials provided with the distribution.
19 ** 3. Neither the name of the University nor the names of its contributors
20 **    may be used to endorse or promote products derived from this software
21 **    without specific prior written permission.
22 **
23 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 ** SUCH DAMAGE.
34 */
35 
36 #include "tzfile.h"
37 #include "fcntl.h"
38 #include <locale.h>
39 #include <wchar.h>
40 
41 struct lc_time_T {
42 	const wchar_t *	mon[MONSPERYEAR];
43 	const wchar_t *	month[MONSPERYEAR];
44 	const wchar_t *	wday[DAYSPERWEEK];
45 	const wchar_t *	weekday[DAYSPERWEEK];
46 	const wchar_t *	X_fmt;
47 	const wchar_t *	x_fmt;
48 	const wchar_t *	c_fmt;
49 	const wchar_t *	am;
50 	const wchar_t *	pm;
51 	const wchar_t *	date_fmt;
52 };
53 
54 #define Locale	(&C_time_locale)
55 
56 static const struct lc_time_T	C_time_locale = {
57 	{
58 		L"Jan", L"Feb", L"Mar", L"Apr", L"May", L"Jun",
59 		L"Jul", L"Aug", L"Sep", L"Oct", L"Nov", L"Dec"
60 	}, {
61 		L"January", L"February", L"March", L"April", L"May", L"June",
62 		L"July", L"August", L"September", L"October", L"November",
63 		L"December"
64 	}, {
65 		L"Sun", L"Mon", L"Tue", L"Wed",
66 		L"Thu", L"Fri", L"Sat"
67 	}, {
68 		L"Sunday", L"Monday", L"Tuesday", L"Wednesday",
69 		L"Thursday", L"Friday", L"Saturday"
70 	},
71 
72 	/* X_fmt */
73 	L"%H:%M:%S",
74 
75 	/*
76 	** x_fmt
77 	** C99 requires this format.
78 	** Using just numbers (as here) makes Quakers happier;
79 	** it's also compatible with SVR4.
80 	*/
81 	L"%m/%d/%y",
82 
83 	/*
84 	** c_fmt
85 	** C99 requires this format.
86 	** Previously this code used "%D %X", but we now conform to C99.
87 	** Note that
88 	**	"%a %b %d %H:%M:%S %Y"
89 	** is used by Solaris 2.3.
90 	*/
91 	L"%a %b %e %T %Y",
92 
93 	/* am */
94 	L"AM",
95 
96 	/* pm */
97 	L"PM",
98 
99 	/* date_fmt */
100 	L"%a %b %e %H:%M:%S %Z %Y"
101 };
102 
103 #define UNKNOWN L"?"
104 static wchar_t *	_add(const wchar_t *, wchar_t *, const wchar_t *);
105 static wchar_t *	_sadd(const char *, wchar_t *, const wchar_t *);
106 static wchar_t *	_conv(int, const wchar_t *, wchar_t *, const wchar_t *);
107 static wchar_t *	_fmt(const wchar_t *, const struct tm *, wchar_t *, const wchar_t *,
108 			int *);
109 static wchar_t *	_yconv(int, int, int, int, wchar_t *, const wchar_t *);
110 
111 extern char *	tzname[];
112 
113 #ifndef YEAR_2000_NAME
114 #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
115 #endif /* !defined YEAR_2000_NAME */
116 
117 #define IN_NONE	0
118 #define IN_SOME	1
119 #define IN_THIS	2
120 #define IN_ALL	3
121 
122 size_t
123 wcsftime(wchar_t *__restrict s, size_t maxsize,
124     const wchar_t *__restrict format, const struct tm *__restrict t)
125 {
126 	wchar_t *p;
127 	int	warn;
128 
129 	tzset();
130 	warn = IN_NONE;
131 	p = _fmt(((format == NULL) ? L"%c" : format), t, s, s + maxsize, &warn);
132 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
133 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
134 		(void) fprintf(stderr, "\n");
135 		if (format == NULL)
136 			(void) fprintf(stderr, "NULL strftime format ");
137 		else	(void) fwprintf(stderr, "strftime format \"%ls\" ",
138 				format);
139 		(void) fprintf(stderr, "yields only two digits of years in ");
140 		if (warn == IN_SOME)
141 			(void) fprintf(stderr, "some locales");
142 		else if (warn == IN_THIS)
143 			(void) fprintf(stderr, "the current locale");
144 		else	(void) fprintf(stderr, "all locales");
145 		(void) fprintf(stderr, "\n");
146 	}
147 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
148 	if (p == s + maxsize) {
149 		if (maxsize > 0)
150 			s[maxsize - 1] = '\0';
151 		return 0;
152 	}
153 	*p = L'\0';
154 	return p - s;
155 }
156 
157 static wchar_t *
158 _fmt(const wchar_t *format, const struct tm *t, wchar_t *pt,
159     const wchar_t *ptlim, int *warnp)
160 {
161 	for ( ; *format; ++format) {
162 		if (*format != L'%') {
163 			if (pt == ptlim)
164 				break;
165 			*pt++ = *format;
166 			continue;
167 		}
168 label:
169 		switch (*++format) {
170 		case '\0':
171 			--format;
172 			break;
173 		case 'A':
174 			pt = _add((t->tm_wday < 0 ||
175 				t->tm_wday >= DAYSPERWEEK) ?
176 				UNKNOWN : Locale->weekday[t->tm_wday],
177 				pt, ptlim);
178 			continue;
179 		case 'a':
180 			pt = _add((t->tm_wday < 0 ||
181 				t->tm_wday >= DAYSPERWEEK) ?
182 				UNKNOWN : Locale->wday[t->tm_wday],
183 				pt, ptlim);
184 			continue;
185 		case 'B':
186 			pt = _add((t->tm_mon < 0 ||
187 				t->tm_mon >= MONSPERYEAR) ?
188 				UNKNOWN : Locale->month[t->tm_mon],
189 				pt, ptlim);
190 			continue;
191 		case 'b':
192 		case 'h':
193 			pt = _add((t->tm_mon < 0 ||
194 				t->tm_mon >= MONSPERYEAR) ?
195 				UNKNOWN : Locale->mon[t->tm_mon],
196 				pt, ptlim);
197 			continue;
198 		case 'C':
199 			/*
200 			** %C used to do a...
201 			**	_fmt("%a %b %e %X %Y", t);
202 			** ...whereas now POSIX 1003.2 calls for
203 			** something completely different.
204 			** (ado, 1993-05-24)
205 			*/
206 			pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
207 				pt, ptlim);
208 			continue;
209 		case 'c':
210 			{
211 			int warn2 = IN_SOME;
212 
213 			pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
214 			if (warn2 == IN_ALL)
215 				warn2 = IN_THIS;
216 			if (warn2 > *warnp)
217 				*warnp = warn2;
218 			}
219 			continue;
220 		case 'D':
221 			pt = _fmt(L"%m/%d/%y", t, pt, ptlim, warnp);
222 			continue;
223 		case 'd':
224 			pt = _conv(t->tm_mday, L"%02d", pt, ptlim);
225 			continue;
226 		case 'E':
227 		case 'O':
228 			/*
229 			** C99 locale modifiers.
230 			** The sequences
231 			**	%Ec %EC %Ex %EX %Ey %EY
232 			**	%Od %oe %OH %OI %Om %OM
233 			**	%OS %Ou %OU %OV %Ow %OW %Oy
234 			** are supposed to provide alternate
235 			** representations.
236 			*/
237 			goto label;
238 		case 'e':
239 			pt = _conv(t->tm_mday, L"%2d", pt, ptlim);
240 			continue;
241 		case 'F':
242 			pt = _fmt(L"%Y-%m-%d", t, pt, ptlim, warnp);
243 			continue;
244 		case 'H':
245 			pt = _conv(t->tm_hour, L"%02d", pt, ptlim);
246 			continue;
247 		case 'I':
248 			pt = _conv((t->tm_hour % 12) ?
249 				(t->tm_hour % 12) : 12,
250 				L"%02d", pt, ptlim);
251 			continue;
252 		case 'j':
253 			pt = _conv(t->tm_yday + 1, L"%03d", pt, ptlim);
254 			continue;
255 		case 'k':
256 			/*
257 			** This used to be...
258 			**	_conv(t->tm_hour % 12 ?
259 			**		t->tm_hour % 12 : 12, 2, ' ');
260 			** ...and has been changed to the below to
261 			** match SunOS 4.1.1 and Arnold Robbins'
262 			** strftime version 3.0. That is, "%k" and
263 			** "%l" have been swapped.
264 			** (ado, 1993-05-24)
265 			*/
266 			pt = _conv(t->tm_hour, L"%2d", pt, ptlim);
267 			continue;
268 		case 'l':
269 			/*
270 			** This used to be...
271 			**	_conv(t->tm_hour, 2, ' ');
272 			** ...and has been changed to the below to
273 			** match SunOS 4.1.1 and Arnold Robbin's
274 			** strftime version 3.0. That is, "%k" and
275 			** "%l" have been swapped.
276 			** (ado, 1993-05-24)
277 			*/
278 			pt = _conv((t->tm_hour % 12) ?
279 				(t->tm_hour % 12) : 12,
280 				L"%2d", pt, ptlim);
281 			continue;
282 		case 'M':
283 			pt = _conv(t->tm_min, L"%02d", pt, ptlim);
284 			continue;
285 		case 'm':
286 			pt = _conv(t->tm_mon + 1, L"%02d", pt, ptlim);
287 			continue;
288 		case 'n':
289 			pt = _add(L"\n", pt, ptlim);
290 			continue;
291 		case 'p':
292 			pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
293 				Locale->pm :
294 				Locale->am,
295 				pt, ptlim);
296 			continue;
297 		case 'R':
298 			pt = _fmt(L"%H:%M", t, pt, ptlim, warnp);
299 			continue;
300 		case 'r':
301 			pt = _fmt(L"%I:%M:%S %p", t, pt, ptlim, warnp);
302 			continue;
303 		case 'S':
304 			pt = _conv(t->tm_sec, L"%02d", pt, ptlim);
305 			continue;
306 		case 's':
307 			{
308 				struct tm	tm;
309 				wchar_t		buf[INT_STRLEN_MAXIMUM(
310 							time_t) + 1];
311 				time_t		mkt;
312 
313 				tm = *t;
314 				mkt = mktime(&tm);
315 				if (TYPE_SIGNED(time_t))
316 					(void) swprintf(buf,
317 					    sizeof buf/sizeof buf[0],
318 					    L"%ld", (long) mkt);
319 				else
320 					(void) swprintf(buf,
321 					    sizeof buf/sizeof buf[0],
322 					    L"%lu", (unsigned long) mkt);
323 				pt = _add(buf, pt, ptlim);
324 			}
325 			continue;
326 		case 'T':
327 			pt = _fmt(L"%H:%M:%S", t, pt, ptlim, warnp);
328 			continue;
329 		case 't':
330 			pt = _add(L"\t", pt, ptlim);
331 			continue;
332 		case 'U':
333 			pt = _conv((t->tm_yday + DAYSPERWEEK -
334 				t->tm_wday) / DAYSPERWEEK,
335 				L"%02d", pt, ptlim);
336 			continue;
337 		case 'u':
338 			/*
339 			** From Arnold Robbins' strftime version 3.0:
340 			** "ISO 8601: Weekday as a decimal number
341 			** [1 (Monday) - 7]"
342 			** (ado, 1993-05-24)
343 			*/
344 			pt = _conv((t->tm_wday == 0) ?
345 				DAYSPERWEEK : t->tm_wday,
346 				L"%d", pt, ptlim);
347 			continue;
348 		case 'V':	/* ISO 8601 week number */
349 		case 'G':	/* ISO 8601 year (four digits) */
350 		case 'g':	/* ISO 8601 year (two digits) */
351 /*
352 ** From Arnold Robbins' strftime version 3.0: "the week number of the
353 ** year (the first Monday as the first day of week 1) as a decimal number
354 ** (01-53)."
355 ** (ado, 1993-05-24)
356 **
357 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
358 ** "Week 01 of a year is per definition the first week which has the
359 ** Thursday in this year, which is equivalent to the week which contains
360 ** the fourth day of January. In other words, the first week of a new year
361 ** is the week which has the majority of its days in the new year. Week 01
362 ** might also contain days from the previous year and the week before week
363 ** 01 of a year is the last week (52 or 53) of the previous year even if
364 ** it contains days from the new year. A week starts with Monday (day 1)
365 ** and ends with Sunday (day 7). For example, the first week of the year
366 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
367 ** (ado, 1996-01-02)
368 */
369 			{
370 			int	year;
371 			int	base;
372 			int	yday;
373 			int	wday;
374 			int	w;
375 
376 			year = t->tm_year;
377 			base = TM_YEAR_BASE;
378 			yday = t->tm_yday;
379 			wday = t->tm_wday;
380 			for ( ; ; ) {
381 				int	len;
382 				int	bot;
383 				int	top;
384 
385 				len = isleap_sum(year, base) ?
386 					DAYSPERLYEAR :
387 					DAYSPERNYEAR;
388 				/*
389 				** What yday (-3 ... 3) does the ISO year
390 				** begin on?
391 				*/
392 				bot = ((yday + 11 - wday) % DAYSPERWEEK) - 3;
393 				/*
394 				** What yday does the NEXT ISO year begin on?
395 				*/
396 				top = bot - (len % DAYSPERWEEK);
397 				if (top < -3)
398 					top += DAYSPERWEEK;
399 				top += len;
400 				if (yday >= top) {
401 					++base;
402 					w = 1;
403 					break;
404 				}
405 				if (yday >= bot) {
406 					w = 1 + ((yday - bot) / DAYSPERWEEK);
407 					break;
408 				}
409 				--base;
410 				yday += isleap_sum(year, base) ?
411 					DAYSPERLYEAR :
412 					DAYSPERNYEAR;
413 			}
414 			if ((w == 52 && t->tm_mon == TM_JANUARY) ||
415 				(w == 1 && t->tm_mon == TM_DECEMBER))
416 					w = 53;
417 			if (*format == 'V')
418 				pt = _conv(w, L"%02d", pt, ptlim);
419 			else if (*format == 'g') {
420 				*warnp = IN_ALL;
421 				pt = _yconv(year, base, 0, 1, pt, ptlim);
422 			} else
423 				pt = _yconv(year, base, 1, 1, pt, ptlim);
424 			}
425 			continue;
426 		case 'v':
427 			/*
428 			** From Arnold Robbins' strftime version 3.0:
429 			** "date as dd-bbb-YYYY"
430 			** (ado, 1993-05-24)
431 			*/
432 			pt = _fmt(L"%e-%b-%Y", t, pt, ptlim, warnp);
433 			continue;
434 		case 'W':
435 			pt = _conv((t->tm_yday + DAYSPERWEEK -
436 				(t->tm_wday ?
437 				(t->tm_wday - 1) :
438 				(DAYSPERWEEK - 1))) / DAYSPERWEEK,
439 				L"%02d", pt, ptlim);
440 			continue;
441 		case 'w':
442 			pt = _conv(t->tm_wday, L"%d", pt, ptlim);
443 			continue;
444 		case 'X':
445 			pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
446 			continue;
447 		case 'x':
448 			{
449 			int	warn2 = IN_SOME;
450 
451 			pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
452 			if (warn2 == IN_ALL)
453 				warn2 = IN_THIS;
454 			if (warn2 > *warnp)
455 				*warnp = warn2;
456 			}
457 			continue;
458 		case 'y':
459 			*warnp = IN_ALL;
460 			pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1, pt, ptlim);
461 			continue;
462 		case 'Y':
463 			pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1, pt, ptlim);
464 			continue;
465 		case 'Z':
466 			if (t->tm_zone != NULL)
467 				pt = _sadd(t->TM_ZONE, pt, ptlim);
468 			else
469 				if (t->tm_isdst >= 0)
470 					pt = _sadd(tzname[t->tm_isdst != 0],
471 					    pt, ptlim);
472 			/*
473 			** C99 says that %Z must be replaced by the
474 			** empty string if the time zone is not
475 			** determinable.
476 			*/
477 			continue;
478 		case 'z':
479 			{
480 			int		diff;
481 			wchar_t const *	sign;
482 
483 			if (t->tm_isdst < 0)
484 				continue;
485 			diff = t->tm_gmtoff;
486 			if (diff < 0) {
487 				sign = L"-";
488 				diff = -diff;
489 			} else
490 				sign = L"+";
491 			pt = _add(sign, pt, ptlim);
492 			diff /= SECSPERMIN;
493 			diff = (diff / MINSPERHOUR) * 100 +
494 				(diff % MINSPERHOUR);
495 			pt = _conv(diff, L"%04d", pt, ptlim);
496 			}
497 			continue;
498 		case '+':
499 			pt = _fmt(Locale->date_fmt, t, pt, ptlim, warnp);
500 			continue;
501 		case '%':
502 		/*
503 		** X311J/88-090 (4.12.3.5): if conversion wchar_t is
504 		** undefined, behavior is undefined. Print out the
505 		** character itself as printf(3) also does.
506 		*/
507 		default:
508 			if (pt != ptlim)
509 				*pt++ = *format;
510 			break;
511 		}
512 	}
513 	return pt;
514 }
515 
516 static wchar_t *
517 _conv(int n, const wchar_t *format, wchar_t *pt, const wchar_t *ptlim)
518 {
519 	wchar_t	buf[INT_STRLEN_MAXIMUM(int) + 1];
520 
521 	(void) swprintf(buf, sizeof buf/sizeof buf[0], format, n);
522 	return _add(buf, pt, ptlim);
523 }
524 
525 static wchar_t *
526 _add(const wchar_t *str, wchar_t *pt, const wchar_t *ptlim)
527 {
528 	while (pt < ptlim && (*pt = *str++) != L'\0')
529 		++pt;
530 	return pt;
531 }
532 
533 static wchar_t *
534 _sadd(const char *str, wchar_t *pt, const wchar_t *ptlim)
535 {
536 	while (pt < ptlim && (*pt = btowc(*str++)) != L'\0')
537 		++pt;
538 	return pt;
539 }
540 /*
541 ** POSIX and the C Standard are unclear or inconsistent about
542 ** what %C and %y do if the year is negative or exceeds 9999.
543 ** Use the convention that %C concatenated with %y yields the
544 ** same output as %Y, and that %Y contains at least 4 bytes,
545 ** with more only if necessary.
546 */
547 
548 static wchar_t *
549 _yconv(int a, int b, int convert_top, int convert_yy, wchar_t *pt,
550     const wchar_t *ptlim)
551 {
552 	register int	lead;
553 	register int	trail;
554 
555 #define DIVISOR	100
556 	trail = a % DIVISOR + b % DIVISOR;
557 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
558 	trail %= DIVISOR;
559 	if (trail < 0 && lead > 0) {
560 		trail += DIVISOR;
561 		--lead;
562 	} else if (lead < 0 && trail > 0) {
563 		trail -= DIVISOR;
564 		++lead;
565 	}
566 	if (convert_top) {
567 		if (lead == 0 && trail < 0)
568 			pt = _add(L"-0", pt, ptlim);
569 		else	pt = _conv(lead, L"%02d", pt, ptlim);
570 	}
571 	if (convert_yy)
572 		pt = _conv(((trail < 0) ? -trail : trail), L"%02d", pt, ptlim);
573 	return pt;
574 }
575 
576