xref: /titanic_41/usr/src/lib/libc/port/locale/strftime.c (revision 62c3776affc53153ce74e14a6e7ce91950c28102)
14297a3b0SGarrett D'Amore /*
2*62c3776aSGarrett D'Amore  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
36b5e5868SGarrett D'Amore  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
44297a3b0SGarrett D'Amore  * Copyright (c) 1989 The Regents of the University of California.
54297a3b0SGarrett D'Amore  * All rights reserved.
64297a3b0SGarrett D'Amore  *
74297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms are permitted
84297a3b0SGarrett D'Amore  * provided that the above copyright notice and this paragraph are
94297a3b0SGarrett D'Amore  * duplicated in all such forms and that any documentation,
104297a3b0SGarrett D'Amore  * advertising materials, and other materials related to such
114297a3b0SGarrett D'Amore  * distribution and use acknowledge that the software was developed
124297a3b0SGarrett D'Amore  * by the University of California, Berkeley. The name of the
134297a3b0SGarrett D'Amore  * University may not be used to endorse or promote products derived
144297a3b0SGarrett D'Amore  * from this software without specific prior written permission.
154297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
164297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
174297a3b0SGarrett D'Amore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
184297a3b0SGarrett D'Amore  */
194297a3b0SGarrett D'Amore 
204297a3b0SGarrett D'Amore #include "lint.h"
214297a3b0SGarrett D'Amore #include "tzfile.h"
224297a3b0SGarrett D'Amore #include <fcntl.h>
234297a3b0SGarrett D'Amore #include <sys/stat.h>
244297a3b0SGarrett D'Amore #include <string.h>
254297a3b0SGarrett D'Amore #include <stdio.h>
26*62c3776aSGarrett D'Amore #include <locale.h>
274297a3b0SGarrett D'Amore #include "timelocal.h"
28*62c3776aSGarrett D'Amore #include "localeimpl.h"
294297a3b0SGarrett D'Amore 
304297a3b0SGarrett D'Amore static char *_add(const char *, char *, const char *);
314297a3b0SGarrett D'Amore static char *_conv(int, const char *, char *, const char *);
32*62c3776aSGarrett D'Amore static char *_fmt(locale_t, const char *, const struct tm *, char *,
33*62c3776aSGarrett D'Amore     const char * const);
344297a3b0SGarrett D'Amore static char *_yconv(int, int, int, int, char *, const char *);
354297a3b0SGarrett D'Amore 
364297a3b0SGarrett D'Amore extern char *tzname[];
374297a3b0SGarrett D'Amore 
384297a3b0SGarrett D'Amore #define	IN_NONE	0
394297a3b0SGarrett D'Amore #define	IN_SOME	1
404297a3b0SGarrett D'Amore #define	IN_THIS	2
414297a3b0SGarrett D'Amore #define	IN_ALL	3
424297a3b0SGarrett D'Amore 
434297a3b0SGarrett D'Amore #define	PAD_DEFAULT	0
444297a3b0SGarrett D'Amore #define	PAD_LESS	1
454297a3b0SGarrett D'Amore #define	PAD_SPACE	2
464297a3b0SGarrett D'Amore #define	PAD_ZERO	3
474297a3b0SGarrett D'Amore 
484297a3b0SGarrett D'Amore static const char *fmt_padding[][4] = {
494297a3b0SGarrett D'Amore 	/* DEFAULT,	LESS,	SPACE,	ZERO */
504297a3b0SGarrett D'Amore #define	PAD_FMT_MONTHDAY	0
514297a3b0SGarrett D'Amore #define	PAD_FMT_HMS		0
524297a3b0SGarrett D'Amore #define	PAD_FMT_CENTURY		0
534297a3b0SGarrett D'Amore #define	PAD_FMT_SHORTYEAR	0
544297a3b0SGarrett D'Amore #define	PAD_FMT_MONTH		0
554297a3b0SGarrett D'Amore #define	PAD_FMT_WEEKOFYEAR	0
564297a3b0SGarrett D'Amore #define	PAD_FMT_DAYOFMONTH	0
574297a3b0SGarrett D'Amore 	{ "%02d",	"%d",	"%2d",	"%02d" },
584297a3b0SGarrett D'Amore #define	PAD_FMT_SDAYOFMONTH	1
594297a3b0SGarrett D'Amore #define	PAD_FMT_SHMS		1
604297a3b0SGarrett D'Amore 	{ "%2d",	"%d",	"%2d",	"%02d" },
614297a3b0SGarrett D'Amore #define	PAD_FMT_DAYOFYEAR	2
624297a3b0SGarrett D'Amore 	{ "%03d",	"%d",	"%3d",	"%03d" },
634297a3b0SGarrett D'Amore #define	PAD_FMT_YEAR		3
644297a3b0SGarrett D'Amore 	{ "%04d",	"%d",	"%4d",	"%04d" }
654297a3b0SGarrett D'Amore };
664297a3b0SGarrett D'Amore 
674297a3b0SGarrett D'Amore 
684297a3b0SGarrett D'Amore size_t
strftime_l(char * _RESTRICT_KYWD s,size_t maxsize,const char * _RESTRICT_KYWD format,const struct tm * _RESTRICT_KYWD t,locale_t loc)69*62c3776aSGarrett D'Amore strftime_l(char *_RESTRICT_KYWD s, size_t maxsize,
70*62c3776aSGarrett D'Amore     const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t,
71*62c3776aSGarrett D'Amore     locale_t loc)
724297a3b0SGarrett D'Amore {
734297a3b0SGarrett D'Amore 	char *p;
744297a3b0SGarrett D'Amore 
754297a3b0SGarrett D'Amore 	tzset();
76*62c3776aSGarrett D'Amore 	p = _fmt(loc, ((format == NULL) ? "%c" : format), t, s, s + maxsize);
774297a3b0SGarrett D'Amore 	if (p == s + maxsize)
784297a3b0SGarrett D'Amore 		return (0);
794297a3b0SGarrett D'Amore 	*p = '\0';
804297a3b0SGarrett D'Amore 	return (p - s);
814297a3b0SGarrett D'Amore }
824297a3b0SGarrett D'Amore 
83*62c3776aSGarrett D'Amore size_t
strftime(char * _RESTRICT_KYWD s,size_t maxsize,const char * _RESTRICT_KYWD format,const struct tm * _RESTRICT_KYWD t)84*62c3776aSGarrett D'Amore strftime(char *_RESTRICT_KYWD s, size_t maxsize,
85*62c3776aSGarrett D'Amore     const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t)
86*62c3776aSGarrett D'Amore {
87*62c3776aSGarrett D'Amore 	return (strftime_l(s, maxsize, format, t, uselocale(NULL)));
88*62c3776aSGarrett D'Amore }
89*62c3776aSGarrett D'Amore 
904297a3b0SGarrett D'Amore static char *
_fmt(locale_t loc,const char * format,const struct tm * t,char * pt,const char * const ptlim)91*62c3776aSGarrett D'Amore _fmt(locale_t loc, const char *format, const struct tm *t, char *pt,
92*62c3776aSGarrett D'Amore     const char * const ptlim)
934297a3b0SGarrett D'Amore {
944297a3b0SGarrett D'Amore 	int Ealternative, Oalternative, PadIndex;
95*62c3776aSGarrett D'Amore 	const struct lc_time *tptr = loc->time;
964297a3b0SGarrett D'Amore 
974297a3b0SGarrett D'Amore #define	PADDING(x)	fmt_padding[x][PadIndex]
984297a3b0SGarrett D'Amore 
994297a3b0SGarrett D'Amore 	for (; *format; ++format) {
1004297a3b0SGarrett D'Amore 		if (*format == '%') {
1014297a3b0SGarrett D'Amore 			Ealternative = 0;
1024297a3b0SGarrett D'Amore 			Oalternative = 0;
1034297a3b0SGarrett D'Amore 			PadIndex	 = PAD_DEFAULT;
1044297a3b0SGarrett D'Amore label:
1054297a3b0SGarrett D'Amore 			switch (*++format) {
1064297a3b0SGarrett D'Amore 			case '\0':
1074297a3b0SGarrett D'Amore 				--format;
1084297a3b0SGarrett D'Amore 				break;
1094297a3b0SGarrett D'Amore 			case 'A':
1104297a3b0SGarrett D'Amore 				pt = _add((t->tm_wday < 0 ||
1114297a3b0SGarrett D'Amore 				    t->tm_wday >= DAYSPERWEEK) ?
1124297a3b0SGarrett D'Amore 				    "?" : tptr->weekday[t->tm_wday],
1134297a3b0SGarrett D'Amore 				    pt, ptlim);
1144297a3b0SGarrett D'Amore 				continue;
1154297a3b0SGarrett D'Amore 			case 'a':
1164297a3b0SGarrett D'Amore 				pt = _add((t->tm_wday < 0 ||
1174297a3b0SGarrett D'Amore 				    t->tm_wday >= DAYSPERWEEK) ?
1184297a3b0SGarrett D'Amore 				    "?" : tptr->wday[t->tm_wday],
1194297a3b0SGarrett D'Amore 				    pt, ptlim);
1204297a3b0SGarrett D'Amore 				continue;
1214297a3b0SGarrett D'Amore 			case 'B':
1224297a3b0SGarrett D'Amore 				pt = _add((t->tm_mon < 0 ||
1234297a3b0SGarrett D'Amore 				    t->tm_mon >= MONSPERYEAR) ?
1244297a3b0SGarrett D'Amore 				    "?" : (tptr->month)[t->tm_mon],
1254297a3b0SGarrett D'Amore 				    pt, ptlim);
1264297a3b0SGarrett D'Amore 				continue;
1274297a3b0SGarrett D'Amore 			case 'b':
1284297a3b0SGarrett D'Amore 			case 'h':
1294297a3b0SGarrett D'Amore 				pt = _add((t->tm_mon < 0 ||
1304297a3b0SGarrett D'Amore 				    t->tm_mon >= MONSPERYEAR) ?
1314297a3b0SGarrett D'Amore 				    "?" : tptr->mon[t->tm_mon],
1324297a3b0SGarrett D'Amore 				    pt, ptlim);
1334297a3b0SGarrett D'Amore 				continue;
1344297a3b0SGarrett D'Amore 			case 'C':
1354297a3b0SGarrett D'Amore 				/*
1364297a3b0SGarrett D'Amore 				 * %C used to do a...
1374297a3b0SGarrett D'Amore 				 *	_fmt("%a %b %e %X %Y", t);
1384297a3b0SGarrett D'Amore 				 * ...whereas now POSIX 1003.2 calls for
1394297a3b0SGarrett D'Amore 				 * something completely different.
1404297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
1414297a3b0SGarrett D'Amore 				 */
1424297a3b0SGarrett D'Amore 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
1434297a3b0SGarrett D'Amore 				    pt, ptlim);
1444297a3b0SGarrett D'Amore 				continue;
1454297a3b0SGarrett D'Amore 			case 'c':
146*62c3776aSGarrett D'Amore 				pt = _fmt(loc, tptr->c_fmt, t, pt, ptlim);
1474297a3b0SGarrett D'Amore 				continue;
1484297a3b0SGarrett D'Amore 			case 'D':
149*62c3776aSGarrett D'Amore 				pt = _fmt(loc, "%m/%d/%y", t, pt, ptlim);
1504297a3b0SGarrett D'Amore 				continue;
1514297a3b0SGarrett D'Amore 			case 'd':
1524297a3b0SGarrett D'Amore 				pt = _conv(t->tm_mday,
1534297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_DAYOFMONTH), pt, ptlim);
1544297a3b0SGarrett D'Amore 				continue;
1554297a3b0SGarrett D'Amore 			case 'E':
1564297a3b0SGarrett D'Amore 				if (Ealternative || Oalternative)
1574297a3b0SGarrett D'Amore 					break;
1584297a3b0SGarrett D'Amore 				Ealternative++;
1594297a3b0SGarrett D'Amore 				goto label;
1604297a3b0SGarrett D'Amore 			case 'O':
1614297a3b0SGarrett D'Amore 				/*
1624297a3b0SGarrett D'Amore 				 * C99 locale modifiers.
1634297a3b0SGarrett D'Amore 				 * The sequences
1644297a3b0SGarrett D'Amore 				 *	%Ec %EC %Ex %EX %Ey %EY
1654297a3b0SGarrett D'Amore 				 *	%Od %oe %OH %OI %Om %OM
1664297a3b0SGarrett D'Amore 				 *	%OS %Ou %OU %OV %Ow %OW %Oy
1674297a3b0SGarrett D'Amore 				 * are supposed to provide alternate
1684297a3b0SGarrett D'Amore 				 * representations.
1694297a3b0SGarrett D'Amore 				 */
1704297a3b0SGarrett D'Amore 				if (Ealternative || Oalternative)
1714297a3b0SGarrett D'Amore 					break;
1724297a3b0SGarrett D'Amore 				Oalternative++;
1734297a3b0SGarrett D'Amore 				goto label;
1744297a3b0SGarrett D'Amore 			case 'e':
1754297a3b0SGarrett D'Amore 				pt = _conv(t->tm_mday,
1764297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_SDAYOFMONTH), pt, ptlim);
1774297a3b0SGarrett D'Amore 				continue;
1784297a3b0SGarrett D'Amore 			case 'F':
179*62c3776aSGarrett D'Amore 				pt = _fmt(loc, "%Y-%m-%d", t, pt, ptlim);
1804297a3b0SGarrett D'Amore 				continue;
1814297a3b0SGarrett D'Amore 			case 'H':
1824297a3b0SGarrett D'Amore 				pt = _conv(t->tm_hour, PADDING(PAD_FMT_HMS),
1834297a3b0SGarrett D'Amore 				    pt, ptlim);
1844297a3b0SGarrett D'Amore 				continue;
1854297a3b0SGarrett D'Amore 			case 'I':
1864297a3b0SGarrett D'Amore 				pt = _conv((t->tm_hour % 12) ?
1874297a3b0SGarrett D'Amore 				    (t->tm_hour % 12) : 12,
1884297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_HMS), pt, ptlim);
1894297a3b0SGarrett D'Amore 				continue;
1904297a3b0SGarrett D'Amore 			case 'j':
1914297a3b0SGarrett D'Amore 				pt = _conv(t->tm_yday + 1,
1924297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_DAYOFYEAR), pt, ptlim);
1934297a3b0SGarrett D'Amore 				continue;
1944297a3b0SGarrett D'Amore 			case 'k':
1954297a3b0SGarrett D'Amore 				/*
1964297a3b0SGarrett D'Amore 				 * This used to be...
1974297a3b0SGarrett D'Amore 				 *	_conv(t->tm_hour % 12 ?
1984297a3b0SGarrett D'Amore 				 *		t->tm_hour % 12 : 12, 2, ' ');
1994297a3b0SGarrett D'Amore 				 * ...and has been changed to the below to
2004297a3b0SGarrett D'Amore 				 * match SunOS 4.1.1 and Arnold Robbins'
2014297a3b0SGarrett D'Amore 				 * strftime version 3.0. That is, "%k" and
2024297a3b0SGarrett D'Amore 				 * "%l" have been swapped.
2034297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
2044297a3b0SGarrett D'Amore 				 */
2054297a3b0SGarrett D'Amore 				pt = _conv(t->tm_hour,
2064297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_SHMS), pt, ptlim);
2074297a3b0SGarrett D'Amore 				continue;
2084297a3b0SGarrett D'Amore 			case 'l':
2094297a3b0SGarrett D'Amore 				/*
2104297a3b0SGarrett D'Amore 				 * This used to be...
2114297a3b0SGarrett D'Amore 				 *	_conv(t->tm_hour, 2, ' ');
2124297a3b0SGarrett D'Amore 				 * ...and has been changed to the below to
2134297a3b0SGarrett D'Amore 				 * match SunOS 4.1.1 and Arnold Robbin's
2144297a3b0SGarrett D'Amore 				 * strftime version 3.0. That is, "%k" and
2154297a3b0SGarrett D'Amore 				 * "%l" have been swapped.
2164297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
2174297a3b0SGarrett D'Amore 				 */
2184297a3b0SGarrett D'Amore 				pt = _conv((t->tm_hour % 12) ?
2194297a3b0SGarrett D'Amore 				    (t->tm_hour % 12) : 12,
2204297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_SHMS), pt, ptlim);
2214297a3b0SGarrett D'Amore 				continue;
2224297a3b0SGarrett D'Amore 			case 'M':
2234297a3b0SGarrett D'Amore 				pt = _conv(t->tm_min, PADDING(PAD_FMT_HMS),
2244297a3b0SGarrett D'Amore 				    pt, ptlim);
2254297a3b0SGarrett D'Amore 				continue;
2264297a3b0SGarrett D'Amore 			case 'm':
2274297a3b0SGarrett D'Amore 				pt = _conv(t->tm_mon + 1,
2284297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_MONTH),
2294297a3b0SGarrett D'Amore 				    pt, ptlim);
2304297a3b0SGarrett D'Amore 				continue;
2314297a3b0SGarrett D'Amore 			case 'n':
2324297a3b0SGarrett D'Amore 				pt = _add("\n", pt, ptlim);
2334297a3b0SGarrett D'Amore 				continue;
2344297a3b0SGarrett D'Amore 			case 'p':
2354297a3b0SGarrett D'Amore 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
2364297a3b0SGarrett D'Amore 				    tptr->pm : tptr->am, pt, ptlim);
2374297a3b0SGarrett D'Amore 				continue;
2384297a3b0SGarrett D'Amore 			case 'R':
239*62c3776aSGarrett D'Amore 				pt = _fmt(loc, "%H:%M", t, pt, ptlim);
2404297a3b0SGarrett D'Amore 				continue;
2414297a3b0SGarrett D'Amore 			case 'r':
242*62c3776aSGarrett D'Amore 				pt = _fmt(loc, tptr->ampm_fmt, t, pt, ptlim);
2434297a3b0SGarrett D'Amore 				continue;
2444297a3b0SGarrett D'Amore 			case 'S':
2454297a3b0SGarrett D'Amore 				pt = _conv(t->tm_sec, PADDING(PAD_FMT_HMS),
2464297a3b0SGarrett D'Amore 				    pt, ptlim);
2474297a3b0SGarrett D'Amore 				continue;
2484297a3b0SGarrett D'Amore 
2496eaad1d3SGarrett D'Amore 			case 's':
2506eaad1d3SGarrett D'Amore 			{
2516eaad1d3SGarrett D'Amore 				struct tm tm;
2526eaad1d3SGarrett D'Amore 				char *buf;
2536eaad1d3SGarrett D'Amore 
2546eaad1d3SGarrett D'Amore 				tm = *t;
2556eaad1d3SGarrett D'Amore 				(void) asprintf(&buf, "%ld", mktime(&tm));
2566eaad1d3SGarrett D'Amore 				pt = _add(buf, pt, ptlim);
2576eaad1d3SGarrett D'Amore 				continue;
2586eaad1d3SGarrett D'Amore 			}
2594297a3b0SGarrett D'Amore 
2604297a3b0SGarrett D'Amore 			case 'T':
261*62c3776aSGarrett D'Amore 				pt = _fmt(loc, "%H:%M:%S", t, pt, ptlim);
2624297a3b0SGarrett D'Amore 				continue;
2634297a3b0SGarrett D'Amore 			case 't':
2644297a3b0SGarrett D'Amore 				pt = _add("\t", pt, ptlim);
2654297a3b0SGarrett D'Amore 				continue;
2664297a3b0SGarrett D'Amore 			case 'U':
2674297a3b0SGarrett D'Amore 				pt = _conv((t->tm_yday + DAYSPERWEEK -
2684297a3b0SGarrett D'Amore 				    t->tm_wday) / DAYSPERWEEK,
2694297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_WEEKOFYEAR),
2704297a3b0SGarrett D'Amore 				    pt, ptlim);
2714297a3b0SGarrett D'Amore 				continue;
2724297a3b0SGarrett D'Amore 			case 'u':
2734297a3b0SGarrett D'Amore 				/*
2744297a3b0SGarrett D'Amore 				 * From Arnold Robbins' strftime version 3.0:
2754297a3b0SGarrett D'Amore 				 * "ISO 8601: Weekday as a decimal number
2764297a3b0SGarrett D'Amore 				 * [1 (Monday) - 7]"
2774297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
2784297a3b0SGarrett D'Amore 				 */
2794297a3b0SGarrett D'Amore 				pt = _conv((t->tm_wday == 0) ?
2804297a3b0SGarrett D'Amore 				    DAYSPERWEEK : t->tm_wday,
2814297a3b0SGarrett D'Amore 				    "%d", pt, ptlim);
2824297a3b0SGarrett D'Amore 				continue;
2834297a3b0SGarrett D'Amore 			case 'V':	/* ISO 8601 week number */
2844297a3b0SGarrett D'Amore 			case 'G':	/* ISO 8601 year (four digits) */
2854297a3b0SGarrett D'Amore 			case 'g':	/* ISO 8601 year (two digits) */
2864297a3b0SGarrett D'Amore /*
2874297a3b0SGarrett D'Amore  * From Arnold Robbins' strftime version 3.0: "the week number of the
2884297a3b0SGarrett D'Amore  * year (the first Monday as the first day of week 1) as a decimal number
2894297a3b0SGarrett D'Amore  * (01-53)."
2904297a3b0SGarrett D'Amore  * (ado, 1993-05-24)
2914297a3b0SGarrett D'Amore  *
2924297a3b0SGarrett D'Amore  * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
2934297a3b0SGarrett D'Amore  * "Week 01 of a year is per definition the first week which has the
2944297a3b0SGarrett D'Amore  * Thursday in this year, which is equivalent to the week which contains
2954297a3b0SGarrett D'Amore  * the fourth day of January. In other words, the first week of a new year
2964297a3b0SGarrett D'Amore  * is the week which has the majority of its days in the new year. Week 01
2974297a3b0SGarrett D'Amore  * might also contain days from the previous year and the week before week
2984297a3b0SGarrett D'Amore  * 01 of a year is the last week (52 or 53) of the previous year even if
2994297a3b0SGarrett D'Amore  * it contains days from the new year. A week starts with Monday (day 1)
3004297a3b0SGarrett D'Amore  * and ends with Sunday (day 7). For example, the first week of the year
3014297a3b0SGarrett D'Amore  * 1997 lasts from 1996-12-30 to 1997-01-05..."
3024297a3b0SGarrett D'Amore  * (ado, 1996-01-02)
3034297a3b0SGarrett D'Amore  */
3044297a3b0SGarrett D'Amore 			{
3054297a3b0SGarrett D'Amore 				int	year;
3064297a3b0SGarrett D'Amore 				int	base;
3074297a3b0SGarrett D'Amore 				int	yday;
3084297a3b0SGarrett D'Amore 				int	wday;
3094297a3b0SGarrett D'Amore 				int	w;
3104297a3b0SGarrett D'Amore 
3114297a3b0SGarrett D'Amore 				year = t->tm_year;
3124297a3b0SGarrett D'Amore 				base = TM_YEAR_BASE;
3134297a3b0SGarrett D'Amore 				yday = t->tm_yday;
3144297a3b0SGarrett D'Amore 				wday = t->tm_wday;
3154297a3b0SGarrett D'Amore 				for (;;) {
3164297a3b0SGarrett D'Amore 					int	len;
3174297a3b0SGarrett D'Amore 					int	bot;
3184297a3b0SGarrett D'Amore 					int	top;
3194297a3b0SGarrett D'Amore 
3204297a3b0SGarrett D'Amore 					len = isleap_sum(year, base) ?
3214297a3b0SGarrett D'Amore 					    DAYSPERLYEAR : DAYSPERNYEAR;
3224297a3b0SGarrett D'Amore 					/*
3234297a3b0SGarrett D'Amore 					 * What yday (-3 ... 3) does
3244297a3b0SGarrett D'Amore 					 * the ISO year begin on?
3254297a3b0SGarrett D'Amore 					 */
3264297a3b0SGarrett D'Amore 					bot = ((yday + 11 - wday) %
3274297a3b0SGarrett D'Amore 					    DAYSPERWEEK) - 3;
3284297a3b0SGarrett D'Amore 					/*
3294297a3b0SGarrett D'Amore 					 * What yday does the NEXT
3304297a3b0SGarrett D'Amore 					 * ISO year begin on?
3314297a3b0SGarrett D'Amore 					 */
3324297a3b0SGarrett D'Amore 					top = bot - (len % DAYSPERWEEK);
3334297a3b0SGarrett D'Amore 					if (top < -3)
3344297a3b0SGarrett D'Amore 						top += DAYSPERWEEK;
3354297a3b0SGarrett D'Amore 					top += len;
3364297a3b0SGarrett D'Amore 					if (yday >= top) {
3374297a3b0SGarrett D'Amore 						++base;
3384297a3b0SGarrett D'Amore 						w = 1;
3394297a3b0SGarrett D'Amore 						break;
3404297a3b0SGarrett D'Amore 					}
3414297a3b0SGarrett D'Amore 					if (yday >= bot) {
3424297a3b0SGarrett D'Amore 						w = 1 + ((yday - bot) /
3434297a3b0SGarrett D'Amore 						    DAYSPERWEEK);
3444297a3b0SGarrett D'Amore 						break;
3454297a3b0SGarrett D'Amore 					}
3464297a3b0SGarrett D'Amore 					--base;
3474297a3b0SGarrett D'Amore 					yday += isleap_sum(year, base) ?
3484297a3b0SGarrett D'Amore 					    DAYSPERLYEAR : DAYSPERNYEAR;
3494297a3b0SGarrett D'Amore 				}
3504297a3b0SGarrett D'Amore #ifdef XPG4_1994_04_09
3514297a3b0SGarrett D'Amore 				if ((w == 52 && t->tm_mon == TM_JANUARY) ||
3524297a3b0SGarrett D'Amore 				    (w == 1 && t->tm_mon == TM_DECEMBER))
3534297a3b0SGarrett D'Amore 					w = 53;
3544297a3b0SGarrett D'Amore #endif /* defined XPG4_1994_04_09 */
3554297a3b0SGarrett D'Amore 				if (*format == 'V')
3564297a3b0SGarrett D'Amore 					pt = _conv(w,
3574297a3b0SGarrett D'Amore 					    PADDING(PAD_FMT_WEEKOFYEAR),
3584297a3b0SGarrett D'Amore 					    pt, ptlim);
3594297a3b0SGarrett D'Amore 				else if (*format == 'g') {
3604297a3b0SGarrett D'Amore 					pt = _yconv(year, base, 0, 1,
3614297a3b0SGarrett D'Amore 					    pt, ptlim);
3624297a3b0SGarrett D'Amore 				} else
3634297a3b0SGarrett D'Amore 					pt = _yconv(year, base, 1, 1,
3644297a3b0SGarrett D'Amore 					    pt, ptlim);
3654297a3b0SGarrett D'Amore 			}
3664297a3b0SGarrett D'Amore 				continue;
3674297a3b0SGarrett D'Amore 			case 'v':
3684297a3b0SGarrett D'Amore 				/*
3694297a3b0SGarrett D'Amore 				 * From Arnold Robbins' strftime version 3.0:
3704297a3b0SGarrett D'Amore 				 * "date as dd-bbb-YYYY"
3714297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
3724297a3b0SGarrett D'Amore 				 */
373*62c3776aSGarrett D'Amore 				pt = _fmt(loc, "%e-%b-%Y", t, pt, ptlim);
3744297a3b0SGarrett D'Amore 				continue;
3754297a3b0SGarrett D'Amore 			case 'W':
3764297a3b0SGarrett D'Amore 				pt = _conv((t->tm_yday + DAYSPERWEEK -
3774297a3b0SGarrett D'Amore 				    (t->tm_wday ?
3784297a3b0SGarrett D'Amore 				    (t->tm_wday - 1) :
3794297a3b0SGarrett D'Amore 				    (DAYSPERWEEK - 1))) / DAYSPERWEEK,
3804297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_WEEKOFYEAR),
3814297a3b0SGarrett D'Amore 				    pt, ptlim);
3824297a3b0SGarrett D'Amore 				continue;
3834297a3b0SGarrett D'Amore 			case 'w':
3844297a3b0SGarrett D'Amore 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
3854297a3b0SGarrett D'Amore 				continue;
3864297a3b0SGarrett D'Amore 			case 'X':
387*62c3776aSGarrett D'Amore 				pt = _fmt(loc, tptr->X_fmt, t, pt, ptlim);
3884297a3b0SGarrett D'Amore 				continue;
3894297a3b0SGarrett D'Amore 			case 'x':
390*62c3776aSGarrett D'Amore 				pt = _fmt(loc, tptr->x_fmt, t, pt, ptlim);
3914297a3b0SGarrett D'Amore 				continue;
3924297a3b0SGarrett D'Amore 			case 'y':
3934297a3b0SGarrett D'Amore 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
3944297a3b0SGarrett D'Amore 				    pt, ptlim);
3954297a3b0SGarrett D'Amore 				continue;
3964297a3b0SGarrett D'Amore 			case 'Y':
3974297a3b0SGarrett D'Amore 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
3984297a3b0SGarrett D'Amore 				    pt, ptlim);
3994297a3b0SGarrett D'Amore 				continue;
4004297a3b0SGarrett D'Amore 			case 'Z':
4014297a3b0SGarrett D'Amore 				if (t->tm_isdst >= 0)
4024297a3b0SGarrett D'Amore 					pt = _add(tzname[t->tm_isdst != 0],
4034297a3b0SGarrett D'Amore 					    pt, ptlim);
4044297a3b0SGarrett D'Amore 				/*
4054297a3b0SGarrett D'Amore 				 * C99 says that %Z must be replaced by the
4064297a3b0SGarrett D'Amore 				 * empty string if the time zone is not
4074297a3b0SGarrett D'Amore 				 * determinable.
4084297a3b0SGarrett D'Amore 				 */
4094297a3b0SGarrett D'Amore 				continue;
4104297a3b0SGarrett D'Amore 			case 'z':
4114297a3b0SGarrett D'Amore 				{
4124297a3b0SGarrett D'Amore 				int		diff;
4134297a3b0SGarrett D'Amore 				char const *	sign;
4144297a3b0SGarrett D'Amore 
4154297a3b0SGarrett D'Amore 				if (t->tm_isdst < 0)
4164297a3b0SGarrett D'Amore 					continue;
4174297a3b0SGarrett D'Amore 				/*
4184297a3b0SGarrett D'Amore 				 * C99 says that the UTC offset must
4194297a3b0SGarrett D'Amore 				 * be computed by looking only at
4204297a3b0SGarrett D'Amore 				 * tm_isdst. This requirement is
4214297a3b0SGarrett D'Amore 				 * incorrect, since it means the code
4224297a3b0SGarrett D'Amore 				 * must rely on magic (in this case
4234297a3b0SGarrett D'Amore 				 * altzone and timezone), and the
4244297a3b0SGarrett D'Amore 				 * magic might not have the correct
4254297a3b0SGarrett D'Amore 				 * offset. Doing things correctly is
4264297a3b0SGarrett D'Amore 				 * tricky and requires disobeying C99;
4274297a3b0SGarrett D'Amore 				 * see GNU C strftime for details.
4284297a3b0SGarrett D'Amore 				 * For now, punt and conform to the
4294297a3b0SGarrett D'Amore 				 * standard, even though it's incorrect.
4304297a3b0SGarrett D'Amore 				 *
4314297a3b0SGarrett D'Amore 				 * C99 says that %z must be replaced by the
4324297a3b0SGarrett D'Amore 				 * empty string if the time zone is not
4334297a3b0SGarrett D'Amore 				 * determinable, so output nothing if the
4344297a3b0SGarrett D'Amore 				 * appropriate variables are not available.
4354297a3b0SGarrett D'Amore 				 */
4364297a3b0SGarrett D'Amore 				if (t->tm_isdst == 0)
4374297a3b0SGarrett D'Amore 					diff = -timezone;
4384297a3b0SGarrett D'Amore 				else
4394297a3b0SGarrett D'Amore 					diff = -altzone;
4404297a3b0SGarrett D'Amore 				if (diff < 0) {
4414297a3b0SGarrett D'Amore 					sign = "-";
4424297a3b0SGarrett D'Amore 					diff = -diff;
4434297a3b0SGarrett D'Amore 				} else
4444297a3b0SGarrett D'Amore 					sign = "+";
4454297a3b0SGarrett D'Amore 				pt = _add(sign, pt, ptlim);
4464297a3b0SGarrett D'Amore 				diff /= SECSPERMIN;
4474297a3b0SGarrett D'Amore 				diff = (diff / MINSPERHOUR) * 100 +
4484297a3b0SGarrett D'Amore 				    (diff % MINSPERHOUR);
4494297a3b0SGarrett D'Amore 				pt = _conv(diff, PADDING(PAD_FMT_YEAR),
4504297a3b0SGarrett D'Amore 				    pt, ptlim);
4514297a3b0SGarrett D'Amore 				}
4524297a3b0SGarrett D'Amore 				continue;
4534297a3b0SGarrett D'Amore 			case '+':
454*62c3776aSGarrett D'Amore 				pt = _fmt(loc, tptr->date_fmt, t, pt, ptlim);
4554297a3b0SGarrett D'Amore 				continue;
4564297a3b0SGarrett D'Amore 			case '-':
4574297a3b0SGarrett D'Amore 				if (PadIndex != PAD_DEFAULT)
4584297a3b0SGarrett D'Amore 					break;
4594297a3b0SGarrett D'Amore 				PadIndex = PAD_LESS;
4604297a3b0SGarrett D'Amore 				goto label;
4614297a3b0SGarrett D'Amore 			case '_':
4624297a3b0SGarrett D'Amore 				if (PadIndex != PAD_DEFAULT)
4634297a3b0SGarrett D'Amore 					break;
4644297a3b0SGarrett D'Amore 				PadIndex = PAD_SPACE;
4654297a3b0SGarrett D'Amore 				goto label;
4664297a3b0SGarrett D'Amore 			case '0':
4674297a3b0SGarrett D'Amore 				if (PadIndex != PAD_DEFAULT)
4684297a3b0SGarrett D'Amore 					break;
4694297a3b0SGarrett D'Amore 				PadIndex = PAD_ZERO;
4704297a3b0SGarrett D'Amore 				goto label;
4714297a3b0SGarrett D'Amore 			case '%':
4724297a3b0SGarrett D'Amore 			/*
4734297a3b0SGarrett D'Amore 			 * X311J/88-090 (4.12.3.5): if conversion char is
4744297a3b0SGarrett D'Amore 			 * undefined, behavior is undefined. Print out the
4754297a3b0SGarrett D'Amore 			 * character itself as printf(3) also does.
4764297a3b0SGarrett D'Amore 			 */
4774297a3b0SGarrett D'Amore 			default:
4784297a3b0SGarrett D'Amore 				break;
4794297a3b0SGarrett D'Amore 			}
4804297a3b0SGarrett D'Amore 		}
4814297a3b0SGarrett D'Amore 		if (pt == ptlim)
4824297a3b0SGarrett D'Amore 			break;
4834297a3b0SGarrett D'Amore 		*pt++ = *format;
4844297a3b0SGarrett D'Amore 	}
4854297a3b0SGarrett D'Amore 	return (pt);
4864297a3b0SGarrett D'Amore }
4874297a3b0SGarrett D'Amore 
4884297a3b0SGarrett D'Amore static char *
_conv(const int n,const char * format,char * const pt,const char * const ptlim)4894297a3b0SGarrett D'Amore _conv(const int n, const char *format, char *const pt,
4904297a3b0SGarrett D'Amore     const char *const ptlim)
4914297a3b0SGarrett D'Amore {
4924297a3b0SGarrett D'Amore 	char	buf[12];
4934297a3b0SGarrett D'Amore 
4944297a3b0SGarrett D'Amore 	(void) sprintf(buf, format, n);
4954297a3b0SGarrett D'Amore 	return (_add(buf, pt, ptlim));
4964297a3b0SGarrett D'Amore }
4974297a3b0SGarrett D'Amore 
4984297a3b0SGarrett D'Amore static char *
_add(const char * str,char * pt,const char * const ptlim)4994297a3b0SGarrett D'Amore _add(const char *str, char *pt, const char *const ptlim)
5004297a3b0SGarrett D'Amore {
5014297a3b0SGarrett D'Amore 	while (pt < ptlim && (*pt = *str++) != '\0')
5024297a3b0SGarrett D'Amore 		++pt;
5034297a3b0SGarrett D'Amore 	return (pt);
5044297a3b0SGarrett D'Amore }
5054297a3b0SGarrett D'Amore 
5064297a3b0SGarrett D'Amore /*
5074297a3b0SGarrett D'Amore  * POSIX and the C Standard are unclear or inconsistent about
5084297a3b0SGarrett D'Amore  * what %C and %y do if the year is negative or exceeds 9999.
5094297a3b0SGarrett D'Amore  * Use the convention that %C concatenated with %y yields the
5104297a3b0SGarrett D'Amore  * same output as %Y, and that %Y contains at least 4 bytes,
5114297a3b0SGarrett D'Amore  * with more only if necessary.
5124297a3b0SGarrett D'Amore  */
5134297a3b0SGarrett D'Amore 
5144297a3b0SGarrett D'Amore static char *
_yconv(const int a,const int b,const int convert_top,const int convert_yy,char * pt,const char * const ptlim)5154297a3b0SGarrett D'Amore _yconv(const int a, const int b, const int convert_top, const int convert_yy,
5164297a3b0SGarrett D'Amore     char *pt, const char * const ptlim)
5174297a3b0SGarrett D'Amore {
5184297a3b0SGarrett D'Amore 	register int	lead;
5194297a3b0SGarrett D'Amore 	register int	trail;
5204297a3b0SGarrett D'Amore 
5214297a3b0SGarrett D'Amore #define	DIVISOR	100
5224297a3b0SGarrett D'Amore 	trail = a % DIVISOR + b % DIVISOR;
5234297a3b0SGarrett D'Amore 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
5244297a3b0SGarrett D'Amore 	trail %= DIVISOR;
5254297a3b0SGarrett D'Amore 	if (trail < 0 && lead > 0) {
5264297a3b0SGarrett D'Amore 		trail += DIVISOR;
5274297a3b0SGarrett D'Amore 		--lead;
5284297a3b0SGarrett D'Amore 	} else if (lead < 0 && trail > 0) {
5294297a3b0SGarrett D'Amore 		trail -= DIVISOR;
5304297a3b0SGarrett D'Amore 		++lead;
5314297a3b0SGarrett D'Amore 	}
5324297a3b0SGarrett D'Amore 	if (convert_top) {
5334297a3b0SGarrett D'Amore 		if (lead == 0 && trail < 0)
5344297a3b0SGarrett D'Amore 			pt = _add("-0", pt, ptlim);
5354297a3b0SGarrett D'Amore 		else	pt = _conv(lead, "%02d", pt, ptlim);
5364297a3b0SGarrett D'Amore 	}
5374297a3b0SGarrett D'Amore 	if (convert_yy)
5384297a3b0SGarrett D'Amore 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
5394297a3b0SGarrett D'Amore 	return (pt);
5404297a3b0SGarrett D'Amore }
541