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