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