xref: /illumos-gate/usr/src/lib/libc/port/locale/strftime.c (revision 72af5a458c3acde8fe8b432dc826b8b100d005d1)
14297a3b0SGarrett D'Amore /*
22d08521bSGarrett 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>
23*72af5a45SBill Sommerfeld #include <stdint.h>
244297a3b0SGarrett D'Amore #include <sys/stat.h>
254297a3b0SGarrett D'Amore #include <string.h>
264297a3b0SGarrett D'Amore #include <stdio.h>
272d08521bSGarrett D'Amore #include <locale.h>
284297a3b0SGarrett D'Amore #include "timelocal.h"
292d08521bSGarrett D'Amore #include "localeimpl.h"
304297a3b0SGarrett D'Amore 
314297a3b0SGarrett D'Amore static char *_add(const char *, char *, const char *);
324297a3b0SGarrett D'Amore static char *_conv(int, const char *, char *, const char *);
332d08521bSGarrett D'Amore static char *_fmt(locale_t, const char *, const struct tm *, char *,
342d08521bSGarrett D'Amore     const char * const);
354297a3b0SGarrett D'Amore static char *_yconv(int, int, int, int, char *, const char *);
364297a3b0SGarrett D'Amore 
374297a3b0SGarrett D'Amore extern char *tzname[];
384297a3b0SGarrett D'Amore 
394297a3b0SGarrett D'Amore #define	IN_NONE	0
404297a3b0SGarrett D'Amore #define	IN_SOME	1
414297a3b0SGarrett D'Amore #define	IN_THIS	2
424297a3b0SGarrett D'Amore #define	IN_ALL	3
434297a3b0SGarrett D'Amore 
444297a3b0SGarrett D'Amore #define	PAD_DEFAULT	0
454297a3b0SGarrett D'Amore #define	PAD_LESS	1
464297a3b0SGarrett D'Amore #define	PAD_SPACE	2
474297a3b0SGarrett D'Amore #define	PAD_ZERO	3
484297a3b0SGarrett D'Amore 
494297a3b0SGarrett D'Amore static const char *fmt_padding[][4] = {
504297a3b0SGarrett D'Amore 	/* DEFAULT,	LESS,	SPACE,	ZERO */
514297a3b0SGarrett D'Amore #define	PAD_FMT_MONTHDAY	0
524297a3b0SGarrett D'Amore #define	PAD_FMT_HMS		0
534297a3b0SGarrett D'Amore #define	PAD_FMT_CENTURY		0
544297a3b0SGarrett D'Amore #define	PAD_FMT_SHORTYEAR	0
554297a3b0SGarrett D'Amore #define	PAD_FMT_MONTH		0
564297a3b0SGarrett D'Amore #define	PAD_FMT_WEEKOFYEAR	0
574297a3b0SGarrett D'Amore #define	PAD_FMT_DAYOFMONTH	0
584297a3b0SGarrett D'Amore 	{ "%02d",	"%d",	"%2d",	"%02d" },
594297a3b0SGarrett D'Amore #define	PAD_FMT_SDAYOFMONTH	1
604297a3b0SGarrett D'Amore #define	PAD_FMT_SHMS		1
614297a3b0SGarrett D'Amore 	{ "%2d",	"%d",	"%2d",	"%02d" },
624297a3b0SGarrett D'Amore #define	PAD_FMT_DAYOFYEAR	2
634297a3b0SGarrett D'Amore 	{ "%03d",	"%d",	"%3d",	"%03d" },
644297a3b0SGarrett D'Amore #define	PAD_FMT_YEAR		3
654297a3b0SGarrett D'Amore 	{ "%04d",	"%d",	"%4d",	"%04d" }
664297a3b0SGarrett D'Amore };
674297a3b0SGarrett D'Amore 
68*72af5a45SBill Sommerfeld /*
69*72af5a45SBill Sommerfeld  * If a caller (such as ascftime) passes in a very large maximum size,
70*72af5a45SBill Sommerfeld  * the pointer addition here may overflow, wrap around the end of
71*72af5a45SBill Sommerfeld  * memory, and, if used as-is, would cause subsequent range checks to fail
72*72af5a45SBill Sommerfeld  * spuriously, resulting in corrupted output even if there is sufficent
73*72af5a45SBill Sommerfeld  * room in the buffer for the requested format.
74*72af5a45SBill Sommerfeld  *
75*72af5a45SBill Sommerfeld  * To prevent this, we replace an overflowed result with the largest
76*72af5a45SBill Sommerfeld  * possible pointer value.
77*72af5a45SBill Sommerfeld  */
78*72af5a45SBill Sommerfeld static char *
_ptlim(char * s,size_t maxsize)79*72af5a45SBill Sommerfeld _ptlim(char *s, size_t maxsize)
80*72af5a45SBill Sommerfeld {
81*72af5a45SBill Sommerfeld 	char *e = s + maxsize;
82*72af5a45SBill Sommerfeld 
83*72af5a45SBill Sommerfeld 	if (e < s)
84*72af5a45SBill Sommerfeld 		e = (char *)(-sizeof (*e));
85*72af5a45SBill Sommerfeld 
86*72af5a45SBill Sommerfeld 	return (e);
87*72af5a45SBill Sommerfeld }
884297a3b0SGarrett D'Amore 
894297a3b0SGarrett 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)902d08521bSGarrett D'Amore strftime_l(char *_RESTRICT_KYWD s, size_t maxsize,
912d08521bSGarrett D'Amore     const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t,
922d08521bSGarrett D'Amore     locale_t loc)
934297a3b0SGarrett D'Amore {
944297a3b0SGarrett D'Amore 	char *p;
95*72af5a45SBill Sommerfeld 	char *e = _ptlim(s, maxsize);
964297a3b0SGarrett D'Amore 
974297a3b0SGarrett D'Amore 	tzset();
98*72af5a45SBill Sommerfeld 	p = _fmt(loc, ((format == NULL) ? "%c" : format), t, s, e);
99*72af5a45SBill Sommerfeld 	if (p == e)
1004297a3b0SGarrett D'Amore 		return (0);
1014297a3b0SGarrett D'Amore 	*p = '\0';
1024297a3b0SGarrett D'Amore 	return (p - s);
1034297a3b0SGarrett D'Amore }
1044297a3b0SGarrett D'Amore 
1052d08521bSGarrett D'Amore size_t
strftime(char * _RESTRICT_KYWD s,size_t maxsize,const char * _RESTRICT_KYWD format,const struct tm * _RESTRICT_KYWD t)1062d08521bSGarrett D'Amore strftime(char *_RESTRICT_KYWD s, size_t maxsize,
1072d08521bSGarrett D'Amore     const char *_RESTRICT_KYWD format, const struct tm *_RESTRICT_KYWD t)
1082d08521bSGarrett D'Amore {
1092d08521bSGarrett D'Amore 	return (strftime_l(s, maxsize, format, t, uselocale(NULL)));
1102d08521bSGarrett D'Amore }
1112d08521bSGarrett D'Amore 
1124297a3b0SGarrett D'Amore static char *
_fmt(locale_t loc,const char * format,const struct tm * t,char * pt,const char * const ptlim)1132d08521bSGarrett D'Amore _fmt(locale_t loc, const char *format, const struct tm *t, char *pt,
1142d08521bSGarrett D'Amore     const char * const ptlim)
1154297a3b0SGarrett D'Amore {
1164297a3b0SGarrett D'Amore 	int Ealternative, Oalternative, PadIndex;
1172d08521bSGarrett D'Amore 	const struct lc_time *tptr = loc->time;
1184297a3b0SGarrett D'Amore 
1194297a3b0SGarrett D'Amore #define	PADDING(x)	fmt_padding[x][PadIndex]
1204297a3b0SGarrett D'Amore 
1214297a3b0SGarrett D'Amore 	for (; *format; ++format) {
1224297a3b0SGarrett D'Amore 		if (*format == '%') {
1234297a3b0SGarrett D'Amore 			Ealternative = 0;
1244297a3b0SGarrett D'Amore 			Oalternative = 0;
1254297a3b0SGarrett D'Amore 			PadIndex	 = PAD_DEFAULT;
1264297a3b0SGarrett D'Amore label:
1274297a3b0SGarrett D'Amore 			switch (*++format) {
1284297a3b0SGarrett D'Amore 			case '\0':
1294297a3b0SGarrett D'Amore 				--format;
1304297a3b0SGarrett D'Amore 				break;
1314297a3b0SGarrett D'Amore 			case 'A':
1324297a3b0SGarrett D'Amore 				pt = _add((t->tm_wday < 0 ||
1334297a3b0SGarrett D'Amore 				    t->tm_wday >= DAYSPERWEEK) ?
1344297a3b0SGarrett D'Amore 				    "?" : tptr->weekday[t->tm_wday],
1354297a3b0SGarrett D'Amore 				    pt, ptlim);
1364297a3b0SGarrett D'Amore 				continue;
1374297a3b0SGarrett D'Amore 			case 'a':
1384297a3b0SGarrett D'Amore 				pt = _add((t->tm_wday < 0 ||
1394297a3b0SGarrett D'Amore 				    t->tm_wday >= DAYSPERWEEK) ?
1404297a3b0SGarrett D'Amore 				    "?" : tptr->wday[t->tm_wday],
1414297a3b0SGarrett D'Amore 				    pt, ptlim);
1424297a3b0SGarrett D'Amore 				continue;
1434297a3b0SGarrett D'Amore 			case 'B':
1444297a3b0SGarrett D'Amore 				pt = _add((t->tm_mon < 0 ||
1454297a3b0SGarrett D'Amore 				    t->tm_mon >= MONSPERYEAR) ?
1464297a3b0SGarrett D'Amore 				    "?" : (tptr->month)[t->tm_mon],
1474297a3b0SGarrett D'Amore 				    pt, ptlim);
1484297a3b0SGarrett D'Amore 				continue;
1494297a3b0SGarrett D'Amore 			case 'b':
1504297a3b0SGarrett D'Amore 			case 'h':
1514297a3b0SGarrett D'Amore 				pt = _add((t->tm_mon < 0 ||
1524297a3b0SGarrett D'Amore 				    t->tm_mon >= MONSPERYEAR) ?
1534297a3b0SGarrett D'Amore 				    "?" : tptr->mon[t->tm_mon],
1544297a3b0SGarrett D'Amore 				    pt, ptlim);
1554297a3b0SGarrett D'Amore 				continue;
1564297a3b0SGarrett D'Amore 			case 'C':
1574297a3b0SGarrett D'Amore 				/*
1584297a3b0SGarrett D'Amore 				 * %C used to do a...
1594297a3b0SGarrett D'Amore 				 *	_fmt("%a %b %e %X %Y", t);
1604297a3b0SGarrett D'Amore 				 * ...whereas now POSIX 1003.2 calls for
1614297a3b0SGarrett D'Amore 				 * something completely different.
1624297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
1634297a3b0SGarrett D'Amore 				 */
1644297a3b0SGarrett D'Amore 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
1654297a3b0SGarrett D'Amore 				    pt, ptlim);
1664297a3b0SGarrett D'Amore 				continue;
1674297a3b0SGarrett D'Amore 			case 'c':
1682d08521bSGarrett D'Amore 				pt = _fmt(loc, tptr->c_fmt, t, pt, ptlim);
1694297a3b0SGarrett D'Amore 				continue;
1704297a3b0SGarrett D'Amore 			case 'D':
1712d08521bSGarrett D'Amore 				pt = _fmt(loc, "%m/%d/%y", t, pt, ptlim);
1724297a3b0SGarrett D'Amore 				continue;
1734297a3b0SGarrett D'Amore 			case 'd':
1744297a3b0SGarrett D'Amore 				pt = _conv(t->tm_mday,
1754297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_DAYOFMONTH), pt, ptlim);
1764297a3b0SGarrett D'Amore 				continue;
1774297a3b0SGarrett D'Amore 			case 'E':
1784297a3b0SGarrett D'Amore 				if (Ealternative || Oalternative)
1794297a3b0SGarrett D'Amore 					break;
1804297a3b0SGarrett D'Amore 				Ealternative++;
1814297a3b0SGarrett D'Amore 				goto label;
1824297a3b0SGarrett D'Amore 			case 'O':
1834297a3b0SGarrett D'Amore 				/*
1844297a3b0SGarrett D'Amore 				 * C99 locale modifiers.
1854297a3b0SGarrett D'Amore 				 * The sequences
1864297a3b0SGarrett D'Amore 				 *	%Ec %EC %Ex %EX %Ey %EY
1874297a3b0SGarrett D'Amore 				 *	%Od %oe %OH %OI %Om %OM
1884297a3b0SGarrett D'Amore 				 *	%OS %Ou %OU %OV %Ow %OW %Oy
1894297a3b0SGarrett D'Amore 				 * are supposed to provide alternate
1904297a3b0SGarrett D'Amore 				 * representations.
1914297a3b0SGarrett D'Amore 				 */
1924297a3b0SGarrett D'Amore 				if (Ealternative || Oalternative)
1934297a3b0SGarrett D'Amore 					break;
1944297a3b0SGarrett D'Amore 				Oalternative++;
1954297a3b0SGarrett D'Amore 				goto label;
1964297a3b0SGarrett D'Amore 			case 'e':
1974297a3b0SGarrett D'Amore 				pt = _conv(t->tm_mday,
1984297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_SDAYOFMONTH), pt, ptlim);
1994297a3b0SGarrett D'Amore 				continue;
2004297a3b0SGarrett D'Amore 			case 'F':
2012d08521bSGarrett D'Amore 				pt = _fmt(loc, "%Y-%m-%d", t, pt, ptlim);
2024297a3b0SGarrett D'Amore 				continue;
2034297a3b0SGarrett D'Amore 			case 'H':
2044297a3b0SGarrett D'Amore 				pt = _conv(t->tm_hour, PADDING(PAD_FMT_HMS),
2054297a3b0SGarrett D'Amore 				    pt, ptlim);
2064297a3b0SGarrett D'Amore 				continue;
2074297a3b0SGarrett D'Amore 			case 'I':
2084297a3b0SGarrett D'Amore 				pt = _conv((t->tm_hour % 12) ?
2094297a3b0SGarrett D'Amore 				    (t->tm_hour % 12) : 12,
2104297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_HMS), pt, ptlim);
2114297a3b0SGarrett D'Amore 				continue;
2124297a3b0SGarrett D'Amore 			case 'j':
2134297a3b0SGarrett D'Amore 				pt = _conv(t->tm_yday + 1,
2144297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_DAYOFYEAR), pt, ptlim);
2154297a3b0SGarrett D'Amore 				continue;
2164297a3b0SGarrett D'Amore 			case 'k':
2174297a3b0SGarrett D'Amore 				/*
2184297a3b0SGarrett D'Amore 				 * This used to be...
2194297a3b0SGarrett D'Amore 				 *	_conv(t->tm_hour % 12 ?
2204297a3b0SGarrett D'Amore 				 *		t->tm_hour % 12 : 12, 2, ' ');
2214297a3b0SGarrett D'Amore 				 * ...and has been changed to the below to
2224297a3b0SGarrett D'Amore 				 * match SunOS 4.1.1 and Arnold Robbins'
2234297a3b0SGarrett D'Amore 				 * strftime version 3.0. That is, "%k" and
2244297a3b0SGarrett D'Amore 				 * "%l" have been swapped.
2254297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
2264297a3b0SGarrett D'Amore 				 */
2274297a3b0SGarrett D'Amore 				pt = _conv(t->tm_hour,
2284297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_SHMS), pt, ptlim);
2294297a3b0SGarrett D'Amore 				continue;
2304297a3b0SGarrett D'Amore 			case 'l':
2314297a3b0SGarrett D'Amore 				/*
2324297a3b0SGarrett D'Amore 				 * This used to be...
2334297a3b0SGarrett D'Amore 				 *	_conv(t->tm_hour, 2, ' ');
2344297a3b0SGarrett D'Amore 				 * ...and has been changed to the below to
2354297a3b0SGarrett D'Amore 				 * match SunOS 4.1.1 and Arnold Robbin's
2364297a3b0SGarrett D'Amore 				 * strftime version 3.0. That is, "%k" and
2374297a3b0SGarrett D'Amore 				 * "%l" have been swapped.
2384297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
2394297a3b0SGarrett D'Amore 				 */
2404297a3b0SGarrett D'Amore 				pt = _conv((t->tm_hour % 12) ?
2414297a3b0SGarrett D'Amore 				    (t->tm_hour % 12) : 12,
2424297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_SHMS), pt, ptlim);
2434297a3b0SGarrett D'Amore 				continue;
2444297a3b0SGarrett D'Amore 			case 'M':
2454297a3b0SGarrett D'Amore 				pt = _conv(t->tm_min, PADDING(PAD_FMT_HMS),
2464297a3b0SGarrett D'Amore 				    pt, ptlim);
2474297a3b0SGarrett D'Amore 				continue;
2484297a3b0SGarrett D'Amore 			case 'm':
2494297a3b0SGarrett D'Amore 				pt = _conv(t->tm_mon + 1,
2504297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_MONTH),
2514297a3b0SGarrett D'Amore 				    pt, ptlim);
2524297a3b0SGarrett D'Amore 				continue;
2534297a3b0SGarrett D'Amore 			case 'n':
2544297a3b0SGarrett D'Amore 				pt = _add("\n", pt, ptlim);
2554297a3b0SGarrett D'Amore 				continue;
2564297a3b0SGarrett D'Amore 			case 'p':
2574297a3b0SGarrett D'Amore 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
2584297a3b0SGarrett D'Amore 				    tptr->pm : tptr->am, pt, ptlim);
2594297a3b0SGarrett D'Amore 				continue;
2604297a3b0SGarrett D'Amore 			case 'R':
2612d08521bSGarrett D'Amore 				pt = _fmt(loc, "%H:%M", t, pt, ptlim);
2624297a3b0SGarrett D'Amore 				continue;
2634297a3b0SGarrett D'Amore 			case 'r':
2642d08521bSGarrett D'Amore 				pt = _fmt(loc, tptr->ampm_fmt, t, pt, ptlim);
2654297a3b0SGarrett D'Amore 				continue;
2664297a3b0SGarrett D'Amore 			case 'S':
2674297a3b0SGarrett D'Amore 				pt = _conv(t->tm_sec, PADDING(PAD_FMT_HMS),
2684297a3b0SGarrett D'Amore 				    pt, ptlim);
2694297a3b0SGarrett D'Amore 				continue;
2704297a3b0SGarrett D'Amore 
2716eaad1d3SGarrett D'Amore 			case 's':
2726eaad1d3SGarrett D'Amore 			{
2736eaad1d3SGarrett D'Amore 				struct tm tm;
2746eaad1d3SGarrett D'Amore 				char *buf;
2756eaad1d3SGarrett D'Amore 
2766eaad1d3SGarrett D'Amore 				tm = *t;
2776eaad1d3SGarrett D'Amore 				(void) asprintf(&buf, "%ld", mktime(&tm));
2786eaad1d3SGarrett D'Amore 				pt = _add(buf, pt, ptlim);
2796eaad1d3SGarrett D'Amore 				continue;
2806eaad1d3SGarrett D'Amore 			}
2814297a3b0SGarrett D'Amore 
2824297a3b0SGarrett D'Amore 			case 'T':
2832d08521bSGarrett D'Amore 				pt = _fmt(loc, "%H:%M:%S", t, pt, ptlim);
2844297a3b0SGarrett D'Amore 				continue;
2854297a3b0SGarrett D'Amore 			case 't':
2864297a3b0SGarrett D'Amore 				pt = _add("\t", pt, ptlim);
2874297a3b0SGarrett D'Amore 				continue;
2884297a3b0SGarrett D'Amore 			case 'U':
2894297a3b0SGarrett D'Amore 				pt = _conv((t->tm_yday + DAYSPERWEEK -
2904297a3b0SGarrett D'Amore 				    t->tm_wday) / DAYSPERWEEK,
2914297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_WEEKOFYEAR),
2924297a3b0SGarrett D'Amore 				    pt, ptlim);
2934297a3b0SGarrett D'Amore 				continue;
2944297a3b0SGarrett D'Amore 			case 'u':
2954297a3b0SGarrett D'Amore 				/*
2964297a3b0SGarrett D'Amore 				 * From Arnold Robbins' strftime version 3.0:
2974297a3b0SGarrett D'Amore 				 * "ISO 8601: Weekday as a decimal number
2984297a3b0SGarrett D'Amore 				 * [1 (Monday) - 7]"
2994297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
3004297a3b0SGarrett D'Amore 				 */
3014297a3b0SGarrett D'Amore 				pt = _conv((t->tm_wday == 0) ?
3024297a3b0SGarrett D'Amore 				    DAYSPERWEEK : t->tm_wday,
3034297a3b0SGarrett D'Amore 				    "%d", pt, ptlim);
3044297a3b0SGarrett D'Amore 				continue;
3054297a3b0SGarrett D'Amore 			case 'V':	/* ISO 8601 week number */
3064297a3b0SGarrett D'Amore 			case 'G':	/* ISO 8601 year (four digits) */
3074297a3b0SGarrett D'Amore 			case 'g':	/* ISO 8601 year (two digits) */
3084297a3b0SGarrett D'Amore /*
3094297a3b0SGarrett D'Amore  * From Arnold Robbins' strftime version 3.0: "the week number of the
3104297a3b0SGarrett D'Amore  * year (the first Monday as the first day of week 1) as a decimal number
3114297a3b0SGarrett D'Amore  * (01-53)."
3124297a3b0SGarrett D'Amore  * (ado, 1993-05-24)
3134297a3b0SGarrett D'Amore  *
3144297a3b0SGarrett D'Amore  * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
3154297a3b0SGarrett D'Amore  * "Week 01 of a year is per definition the first week which has the
3164297a3b0SGarrett D'Amore  * Thursday in this year, which is equivalent to the week which contains
3174297a3b0SGarrett D'Amore  * the fourth day of January. In other words, the first week of a new year
3184297a3b0SGarrett D'Amore  * is the week which has the majority of its days in the new year. Week 01
3194297a3b0SGarrett D'Amore  * might also contain days from the previous year and the week before week
3204297a3b0SGarrett D'Amore  * 01 of a year is the last week (52 or 53) of the previous year even if
3214297a3b0SGarrett D'Amore  * it contains days from the new year. A week starts with Monday (day 1)
3224297a3b0SGarrett D'Amore  * and ends with Sunday (day 7). For example, the first week of the year
3234297a3b0SGarrett D'Amore  * 1997 lasts from 1996-12-30 to 1997-01-05..."
3244297a3b0SGarrett D'Amore  * (ado, 1996-01-02)
3254297a3b0SGarrett D'Amore  */
3264297a3b0SGarrett D'Amore 			{
3274297a3b0SGarrett D'Amore 				int	year;
3284297a3b0SGarrett D'Amore 				int	base;
3294297a3b0SGarrett D'Amore 				int	yday;
3304297a3b0SGarrett D'Amore 				int	wday;
3314297a3b0SGarrett D'Amore 				int	w;
3324297a3b0SGarrett D'Amore 
3334297a3b0SGarrett D'Amore 				year = t->tm_year;
3344297a3b0SGarrett D'Amore 				base = TM_YEAR_BASE;
3354297a3b0SGarrett D'Amore 				yday = t->tm_yday;
3364297a3b0SGarrett D'Amore 				wday = t->tm_wday;
3374297a3b0SGarrett D'Amore 				for (;;) {
3384297a3b0SGarrett D'Amore 					int	len;
3394297a3b0SGarrett D'Amore 					int	bot;
3404297a3b0SGarrett D'Amore 					int	top;
3414297a3b0SGarrett D'Amore 
3424297a3b0SGarrett D'Amore 					len = isleap_sum(year, base) ?
3434297a3b0SGarrett D'Amore 					    DAYSPERLYEAR : DAYSPERNYEAR;
3444297a3b0SGarrett D'Amore 					/*
3454297a3b0SGarrett D'Amore 					 * What yday (-3 ... 3) does
3464297a3b0SGarrett D'Amore 					 * the ISO year begin on?
3474297a3b0SGarrett D'Amore 					 */
3484297a3b0SGarrett D'Amore 					bot = ((yday + 11 - wday) %
3494297a3b0SGarrett D'Amore 					    DAYSPERWEEK) - 3;
3504297a3b0SGarrett D'Amore 					/*
3514297a3b0SGarrett D'Amore 					 * What yday does the NEXT
3524297a3b0SGarrett D'Amore 					 * ISO year begin on?
3534297a3b0SGarrett D'Amore 					 */
3544297a3b0SGarrett D'Amore 					top = bot - (len % DAYSPERWEEK);
3554297a3b0SGarrett D'Amore 					if (top < -3)
3564297a3b0SGarrett D'Amore 						top += DAYSPERWEEK;
3574297a3b0SGarrett D'Amore 					top += len;
3584297a3b0SGarrett D'Amore 					if (yday >= top) {
3594297a3b0SGarrett D'Amore 						++base;
3604297a3b0SGarrett D'Amore 						w = 1;
3614297a3b0SGarrett D'Amore 						break;
3624297a3b0SGarrett D'Amore 					}
3634297a3b0SGarrett D'Amore 					if (yday >= bot) {
3644297a3b0SGarrett D'Amore 						w = 1 + ((yday - bot) /
3654297a3b0SGarrett D'Amore 						    DAYSPERWEEK);
3664297a3b0SGarrett D'Amore 						break;
3674297a3b0SGarrett D'Amore 					}
3684297a3b0SGarrett D'Amore 					--base;
3694297a3b0SGarrett D'Amore 					yday += isleap_sum(year, base) ?
3704297a3b0SGarrett D'Amore 					    DAYSPERLYEAR : DAYSPERNYEAR;
3714297a3b0SGarrett D'Amore 				}
3724297a3b0SGarrett D'Amore #ifdef XPG4_1994_04_09
3734297a3b0SGarrett D'Amore 				if ((w == 52 && t->tm_mon == TM_JANUARY) ||
3744297a3b0SGarrett D'Amore 				    (w == 1 && t->tm_mon == TM_DECEMBER))
3754297a3b0SGarrett D'Amore 					w = 53;
3764297a3b0SGarrett D'Amore #endif /* defined XPG4_1994_04_09 */
3774297a3b0SGarrett D'Amore 				if (*format == 'V')
3784297a3b0SGarrett D'Amore 					pt = _conv(w,
3794297a3b0SGarrett D'Amore 					    PADDING(PAD_FMT_WEEKOFYEAR),
3804297a3b0SGarrett D'Amore 					    pt, ptlim);
3814297a3b0SGarrett D'Amore 				else if (*format == 'g') {
3824297a3b0SGarrett D'Amore 					pt = _yconv(year, base, 0, 1,
3834297a3b0SGarrett D'Amore 					    pt, ptlim);
3844297a3b0SGarrett D'Amore 				} else
3854297a3b0SGarrett D'Amore 					pt = _yconv(year, base, 1, 1,
3864297a3b0SGarrett D'Amore 					    pt, ptlim);
3874297a3b0SGarrett D'Amore 			}
3884297a3b0SGarrett D'Amore 				continue;
3894297a3b0SGarrett D'Amore 			case 'v':
3904297a3b0SGarrett D'Amore 				/*
3914297a3b0SGarrett D'Amore 				 * From Arnold Robbins' strftime version 3.0:
3924297a3b0SGarrett D'Amore 				 * "date as dd-bbb-YYYY"
3934297a3b0SGarrett D'Amore 				 * (ado, 1993-05-24)
3944297a3b0SGarrett D'Amore 				 */
3952d08521bSGarrett D'Amore 				pt = _fmt(loc, "%e-%b-%Y", t, pt, ptlim);
3964297a3b0SGarrett D'Amore 				continue;
3974297a3b0SGarrett D'Amore 			case 'W':
3984297a3b0SGarrett D'Amore 				pt = _conv((t->tm_yday + DAYSPERWEEK -
3994297a3b0SGarrett D'Amore 				    (t->tm_wday ?
4004297a3b0SGarrett D'Amore 				    (t->tm_wday - 1) :
4014297a3b0SGarrett D'Amore 				    (DAYSPERWEEK - 1))) / DAYSPERWEEK,
4024297a3b0SGarrett D'Amore 				    PADDING(PAD_FMT_WEEKOFYEAR),
4034297a3b0SGarrett D'Amore 				    pt, ptlim);
4044297a3b0SGarrett D'Amore 				continue;
4054297a3b0SGarrett D'Amore 			case 'w':
4064297a3b0SGarrett D'Amore 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
4074297a3b0SGarrett D'Amore 				continue;
4084297a3b0SGarrett D'Amore 			case 'X':
4092d08521bSGarrett D'Amore 				pt = _fmt(loc, tptr->X_fmt, t, pt, ptlim);
4104297a3b0SGarrett D'Amore 				continue;
4114297a3b0SGarrett D'Amore 			case 'x':
4122d08521bSGarrett D'Amore 				pt = _fmt(loc, tptr->x_fmt, t, pt, ptlim);
4134297a3b0SGarrett D'Amore 				continue;
4144297a3b0SGarrett D'Amore 			case 'y':
4154297a3b0SGarrett D'Amore 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
4164297a3b0SGarrett D'Amore 				    pt, ptlim);
4174297a3b0SGarrett D'Amore 				continue;
4184297a3b0SGarrett D'Amore 			case 'Y':
4194297a3b0SGarrett D'Amore 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
4204297a3b0SGarrett D'Amore 				    pt, ptlim);
4214297a3b0SGarrett D'Amore 				continue;
4224297a3b0SGarrett D'Amore 			case 'Z':
4234297a3b0SGarrett D'Amore 				if (t->tm_isdst >= 0)
4244297a3b0SGarrett D'Amore 					pt = _add(tzname[t->tm_isdst != 0],
4254297a3b0SGarrett D'Amore 					    pt, ptlim);
4264297a3b0SGarrett D'Amore 				/*
4274297a3b0SGarrett D'Amore 				 * C99 says that %Z must be replaced by the
4284297a3b0SGarrett D'Amore 				 * empty string if the time zone is not
4294297a3b0SGarrett D'Amore 				 * determinable.
4304297a3b0SGarrett D'Amore 				 */
4314297a3b0SGarrett D'Amore 				continue;
4324297a3b0SGarrett D'Amore 			case 'z':
4334297a3b0SGarrett D'Amore 				{
4344297a3b0SGarrett D'Amore 				int		diff;
4354297a3b0SGarrett D'Amore 				char const *	sign;
4364297a3b0SGarrett D'Amore 
4374297a3b0SGarrett D'Amore 				if (t->tm_isdst < 0)
4384297a3b0SGarrett D'Amore 					continue;
4394297a3b0SGarrett D'Amore 				/*
4404297a3b0SGarrett D'Amore 				 * C99 says that the UTC offset must
4414297a3b0SGarrett D'Amore 				 * be computed by looking only at
4424297a3b0SGarrett D'Amore 				 * tm_isdst. This requirement is
4434297a3b0SGarrett D'Amore 				 * incorrect, since it means the code
4444297a3b0SGarrett D'Amore 				 * must rely on magic (in this case
4454297a3b0SGarrett D'Amore 				 * altzone and timezone), and the
4464297a3b0SGarrett D'Amore 				 * magic might not have the correct
4474297a3b0SGarrett D'Amore 				 * offset. Doing things correctly is
4484297a3b0SGarrett D'Amore 				 * tricky and requires disobeying C99;
4494297a3b0SGarrett D'Amore 				 * see GNU C strftime for details.
4504297a3b0SGarrett D'Amore 				 * For now, punt and conform to the
4514297a3b0SGarrett D'Amore 				 * standard, even though it's incorrect.
4524297a3b0SGarrett D'Amore 				 *
4534297a3b0SGarrett D'Amore 				 * C99 says that %z must be replaced by the
4544297a3b0SGarrett D'Amore 				 * empty string if the time zone is not
4554297a3b0SGarrett D'Amore 				 * determinable, so output nothing if the
4564297a3b0SGarrett D'Amore 				 * appropriate variables are not available.
4574297a3b0SGarrett D'Amore 				 */
4584297a3b0SGarrett D'Amore 				if (t->tm_isdst == 0)
4594297a3b0SGarrett D'Amore 					diff = -timezone;
4604297a3b0SGarrett D'Amore 				else
4614297a3b0SGarrett D'Amore 					diff = -altzone;
4624297a3b0SGarrett D'Amore 				if (diff < 0) {
4634297a3b0SGarrett D'Amore 					sign = "-";
4644297a3b0SGarrett D'Amore 					diff = -diff;
4654297a3b0SGarrett D'Amore 				} else
4664297a3b0SGarrett D'Amore 					sign = "+";
4674297a3b0SGarrett D'Amore 				pt = _add(sign, pt, ptlim);
4684297a3b0SGarrett D'Amore 				diff /= SECSPERMIN;
4694297a3b0SGarrett D'Amore 				diff = (diff / MINSPERHOUR) * 100 +
4704297a3b0SGarrett D'Amore 				    (diff % MINSPERHOUR);
4714297a3b0SGarrett D'Amore 				pt = _conv(diff, PADDING(PAD_FMT_YEAR),
4724297a3b0SGarrett D'Amore 				    pt, ptlim);
4734297a3b0SGarrett D'Amore 				}
4744297a3b0SGarrett D'Amore 				continue;
4754297a3b0SGarrett D'Amore 			case '+':
4762d08521bSGarrett D'Amore 				pt = _fmt(loc, tptr->date_fmt, t, pt, ptlim);
4774297a3b0SGarrett D'Amore 				continue;
4784297a3b0SGarrett D'Amore 			case '-':
4794297a3b0SGarrett D'Amore 				if (PadIndex != PAD_DEFAULT)
4804297a3b0SGarrett D'Amore 					break;
4814297a3b0SGarrett D'Amore 				PadIndex = PAD_LESS;
4824297a3b0SGarrett D'Amore 				goto label;
4834297a3b0SGarrett D'Amore 			case '_':
4844297a3b0SGarrett D'Amore 				if (PadIndex != PAD_DEFAULT)
4854297a3b0SGarrett D'Amore 					break;
4864297a3b0SGarrett D'Amore 				PadIndex = PAD_SPACE;
4874297a3b0SGarrett D'Amore 				goto label;
4884297a3b0SGarrett D'Amore 			case '0':
4894297a3b0SGarrett D'Amore 				if (PadIndex != PAD_DEFAULT)
4904297a3b0SGarrett D'Amore 					break;
4914297a3b0SGarrett D'Amore 				PadIndex = PAD_ZERO;
4924297a3b0SGarrett D'Amore 				goto label;
4934297a3b0SGarrett D'Amore 			case '%':
4944297a3b0SGarrett D'Amore 			/*
4954297a3b0SGarrett D'Amore 			 * X311J/88-090 (4.12.3.5): if conversion char is
4964297a3b0SGarrett D'Amore 			 * undefined, behavior is undefined. Print out the
4974297a3b0SGarrett D'Amore 			 * character itself as printf(3) also does.
4984297a3b0SGarrett D'Amore 			 */
4994297a3b0SGarrett D'Amore 			default:
5004297a3b0SGarrett D'Amore 				break;
5014297a3b0SGarrett D'Amore 			}
5024297a3b0SGarrett D'Amore 		}
5034297a3b0SGarrett D'Amore 		if (pt == ptlim)
5044297a3b0SGarrett D'Amore 			break;
5054297a3b0SGarrett D'Amore 		*pt++ = *format;
5064297a3b0SGarrett D'Amore 	}
5074297a3b0SGarrett D'Amore 	return (pt);
5084297a3b0SGarrett D'Amore }
5094297a3b0SGarrett D'Amore 
5104297a3b0SGarrett D'Amore static char *
_conv(const int n,const char * format,char * const pt,const char * const ptlim)5114297a3b0SGarrett D'Amore _conv(const int n, const char *format, char *const pt,
5124297a3b0SGarrett D'Amore     const char *const ptlim)
5134297a3b0SGarrett D'Amore {
5144297a3b0SGarrett D'Amore 	char	buf[12];
5154297a3b0SGarrett D'Amore 
5164297a3b0SGarrett D'Amore 	(void) sprintf(buf, format, n);
5174297a3b0SGarrett D'Amore 	return (_add(buf, pt, ptlim));
5184297a3b0SGarrett D'Amore }
5194297a3b0SGarrett D'Amore 
5204297a3b0SGarrett D'Amore static char *
_add(const char * str,char * pt,const char * const ptlim)5214297a3b0SGarrett D'Amore _add(const char *str, char *pt, const char *const ptlim)
5224297a3b0SGarrett D'Amore {
5234297a3b0SGarrett D'Amore 	while (pt < ptlim && (*pt = *str++) != '\0')
5244297a3b0SGarrett D'Amore 		++pt;
5254297a3b0SGarrett D'Amore 	return (pt);
5264297a3b0SGarrett D'Amore }
5274297a3b0SGarrett D'Amore 
5284297a3b0SGarrett D'Amore /*
5294297a3b0SGarrett D'Amore  * POSIX and the C Standard are unclear or inconsistent about
5304297a3b0SGarrett D'Amore  * what %C and %y do if the year is negative or exceeds 9999.
5314297a3b0SGarrett D'Amore  * Use the convention that %C concatenated with %y yields the
5324297a3b0SGarrett D'Amore  * same output as %Y, and that %Y contains at least 4 bytes,
5334297a3b0SGarrett D'Amore  * with more only if necessary.
5344297a3b0SGarrett D'Amore  */
5354297a3b0SGarrett D'Amore 
5364297a3b0SGarrett 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)5374297a3b0SGarrett D'Amore _yconv(const int a, const int b, const int convert_top, const int convert_yy,
5384297a3b0SGarrett D'Amore     char *pt, const char * const ptlim)
5394297a3b0SGarrett D'Amore {
5404297a3b0SGarrett D'Amore 	register int	lead;
5414297a3b0SGarrett D'Amore 	register int	trail;
5424297a3b0SGarrett D'Amore 
5434297a3b0SGarrett D'Amore #define	DIVISOR	100
5444297a3b0SGarrett D'Amore 	trail = a % DIVISOR + b % DIVISOR;
5454297a3b0SGarrett D'Amore 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
5464297a3b0SGarrett D'Amore 	trail %= DIVISOR;
5474297a3b0SGarrett D'Amore 	if (trail < 0 && lead > 0) {
5484297a3b0SGarrett D'Amore 		trail += DIVISOR;
5494297a3b0SGarrett D'Amore 		--lead;
5504297a3b0SGarrett D'Amore 	} else if (lead < 0 && trail > 0) {
5514297a3b0SGarrett D'Amore 		trail -= DIVISOR;
5524297a3b0SGarrett D'Amore 		++lead;
5534297a3b0SGarrett D'Amore 	}
5544297a3b0SGarrett D'Amore 	if (convert_top) {
5554297a3b0SGarrett D'Amore 		if (lead == 0 && trail < 0)
5564297a3b0SGarrett D'Amore 			pt = _add("-0", pt, ptlim);
5574297a3b0SGarrett D'Amore 		else	pt = _conv(lead, "%02d", pt, ptlim);
5584297a3b0SGarrett D'Amore 	}
5594297a3b0SGarrett D'Amore 	if (convert_yy)
5604297a3b0SGarrett D'Amore 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
5614297a3b0SGarrett D'Amore 	return (pt);
5624297a3b0SGarrett D'Amore }
563