xref: /freebsd/lib/libc/stdtime/strftime.c (revision 46c599340f187db577b9212ab18022f3c7380c68)
148d96b17SGarrett Wollman /*
248d96b17SGarrett Wollman  * Copyright (c) 1989 The Regents of the University of California.
348d96b17SGarrett Wollman  * All rights reserved.
448d96b17SGarrett Wollman  *
53c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
65b5fa75aSEd Maste  *
73c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
83c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
93c87aa1dSDavid Chisnall  *
1048d96b17SGarrett Wollman  * Redistribution and use in source and binary forms are permitted
1148d96b17SGarrett Wollman  * provided that the above copyright notice and this paragraph are
1248d96b17SGarrett Wollman  * duplicated in all such forms and that any documentation,
1348d96b17SGarrett Wollman  * advertising materials, and other materials related to such
1448d96b17SGarrett Wollman  * distribution and use acknowledge that the software was developed
1548d96b17SGarrett Wollman  * by the University of California, Berkeley. The name of the
1648d96b17SGarrett Wollman  * University may not be used to endorse or promote products derived
1748d96b17SGarrett Wollman  * from this software without specific prior written permission.
1848d96b17SGarrett Wollman  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1948d96b17SGarrett Wollman  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
2048d96b17SGarrett Wollman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2148d96b17SGarrett Wollman  */
2248d96b17SGarrett Wollman 
23d201fe46SDaniel Eischen #include "namespace.h"
24c28fbb7bSGarrett Wollman #include "private.h"
25c28fbb7bSGarrett Wollman 
2648d96b17SGarrett Wollman #include "tzfile.h"
27c28fbb7bSGarrett Wollman #include <fcntl.h>
28c28fbb7bSGarrett Wollman #include <sys/stat.h>
29bc421551SDag-Erling Smørgrav #include <stdio.h>
30d201fe46SDaniel Eischen #include "un-namespace.h"
3137486f03SJoerg Wunsch #include "timelocal.h"
3248d96b17SGarrett Wollman 
331d145de8SAlfred Perlstein static char *	_add(const char *, char *, const char *);
34e66c50c7SPedro F. Giffuni static char *	_conv(int, const char *, char *, const char *, locale_t);
35dfc79e89SEdwin Groothuis static char *	_fmt(const char *, const struct tm *, char *, const char *,
363c87aa1dSDavid Chisnall 			int *, locale_t);
37e66c50c7SPedro F. Giffuni static char *	_yconv(int, int, int, int, char *, const char *, locale_t);
3848d96b17SGarrett Wollman 
3948d96b17SGarrett Wollman extern char *	tzname[];
4048d96b17SGarrett Wollman 
41cdff05faSStefan Farfeleder #ifndef YEAR_2000_NAME
42cdff05faSStefan Farfeleder #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
43cdff05faSStefan Farfeleder #endif /* !defined YEAR_2000_NAME */
44cdff05faSStefan Farfeleder 
45cdff05faSStefan Farfeleder #define	IN_NONE	0
46cdff05faSStefan Farfeleder #define	IN_SOME	1
47cdff05faSStefan Farfeleder #define	IN_THIS	2
48cdff05faSStefan Farfeleder #define	IN_ALL	3
49cdff05faSStefan Farfeleder 
50d9506686SXin LI #define	PAD_DEFAULT	0
51d9506686SXin LI #define	PAD_LESS	1
52d9506686SXin LI #define	PAD_SPACE	2
53d9506686SXin LI #define	PAD_ZERO	3
54d9506686SXin LI 
55770284d8SJilles Tjoelker static const char fmt_padding[][4][5] = {
56d9506686SXin LI 	/* DEFAULT,	LESS,	SPACE,	ZERO */
57d9506686SXin LI #define	PAD_FMT_MONTHDAY	0
58d9506686SXin LI #define	PAD_FMT_HMS		0
59d9506686SXin LI #define	PAD_FMT_CENTURY		0
60d9506686SXin LI #define	PAD_FMT_SHORTYEAR	0
61d9506686SXin LI #define	PAD_FMT_MONTH		0
62d9506686SXin LI #define	PAD_FMT_WEEKOFYEAR	0
63d9506686SXin LI #define	PAD_FMT_DAYOFMONTH	0
64d9506686SXin LI 	{ "%02d",	"%d",	"%2d",	"%02d" },
65d9506686SXin LI #define	PAD_FMT_SDAYOFMONTH	1
66d9506686SXin LI #define	PAD_FMT_SHMS		1
67d9506686SXin LI 	{ "%2d",	"%d",	"%2d",	"%02d" },
68d9506686SXin LI #define	PAD_FMT_DAYOFYEAR	2
69d9506686SXin LI 	{ "%03d",	"%d",	"%3d",	"%03d" },
70d9506686SXin LI #define	PAD_FMT_YEAR		3
71d9506686SXin LI 	{ "%04d",	"%d",	"%4d",	"%04d" }
72d9506686SXin LI };
73d9506686SXin LI 
7448d96b17SGarrett Wollman size_t
strftime_l(char * __restrict s,size_t maxsize,const char * __restrict format,const struct tm * __restrict t,locale_t loc)753c87aa1dSDavid Chisnall strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
763c87aa1dSDavid Chisnall     const struct tm * __restrict t, locale_t loc)
7748d96b17SGarrett Wollman {
7848d96b17SGarrett Wollman 	char *	p;
79cdff05faSStefan Farfeleder 	int	warn;
803c87aa1dSDavid Chisnall 	FIX_LOCALE(loc);
8148d96b17SGarrett Wollman 
82c28fbb7bSGarrett Wollman 	tzset();
83cdff05faSStefan Farfeleder 	warn = IN_NONE;
843c87aa1dSDavid Chisnall 	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn, loc);
85cdff05faSStefan Farfeleder #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
86cdff05faSStefan Farfeleder 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
873c87aa1dSDavid Chisnall 		(void) fprintf_l(stderr, loc, "\n");
88cdff05faSStefan Farfeleder 		if (format == NULL)
89e66c50c7SPedro F. Giffuni 			(void) fputs("NULL strftime format ", stderr);
903c87aa1dSDavid Chisnall 		else	(void) fprintf_l(stderr, loc, "strftime format \"%s\" ",
91cdff05faSStefan Farfeleder 				format);
92e66c50c7SPedro F. Giffuni 		(void) fputs("yields only two digits of years in ", stderr);
93cdff05faSStefan Farfeleder 		if (warn == IN_SOME)
94e66c50c7SPedro F. Giffuni 			(void) fputs("some locales", stderr);
95cdff05faSStefan Farfeleder 		else if (warn == IN_THIS)
96e66c50c7SPedro F. Giffuni 			(void) fputs("the current locale", stderr);
97e66c50c7SPedro F. Giffuni 		else	(void) fputs("all locales", stderr);
98e66c50c7SPedro F. Giffuni 		(void) fputs("\n", stderr);
99cdff05faSStefan Farfeleder 	}
100cdff05faSStefan Farfeleder #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
10148d96b17SGarrett Wollman 	if (p == s + maxsize)
1023624c752SPedro F. Giffuni 		return (0);
10348d96b17SGarrett Wollman 	*p = '\0';
10448d96b17SGarrett Wollman 	return p - s;
10548d96b17SGarrett Wollman }
10648d96b17SGarrett Wollman 
1073c87aa1dSDavid Chisnall size_t
strftime(char * __restrict s,size_t maxsize,const char * __restrict format,const struct tm * __restrict t)1083c87aa1dSDavid Chisnall strftime(char * __restrict s, size_t maxsize, const char * __restrict format,
1093c87aa1dSDavid Chisnall     const struct tm * __restrict t)
1103c87aa1dSDavid Chisnall {
1113c87aa1dSDavid Chisnall 	return strftime_l(s, maxsize, format, t, __get_locale());
1123c87aa1dSDavid Chisnall }
1133c87aa1dSDavid Chisnall 
11448d96b17SGarrett Wollman static char *
_fmt(const char * format,const struct tm * const t,char * pt,const char * const ptlim,int * warnp,locale_t loc)11585cef632SCraig Rodrigues _fmt(const char *format, const struct tm * const t, char *pt,
11685cef632SCraig Rodrigues     const char * const ptlim, int *warnp, locale_t loc)
11748d96b17SGarrett Wollman {
118d9506686SXin LI 	int Ealternative, Oalternative, PadIndex;
1193c87aa1dSDavid Chisnall 	struct lc_time_T *tptr = __get_current_time_locale(loc);
12011cd0d32SAndrey A. Chernov 
12148d96b17SGarrett Wollman 	for ( ; *format; ++format) {
12248d96b17SGarrett Wollman 		if (*format == '%') {
12311cd0d32SAndrey A. Chernov 			Ealternative = 0;
12411cd0d32SAndrey A. Chernov 			Oalternative = 0;
125d9506686SXin LI 			PadIndex	 = PAD_DEFAULT;
12648d96b17SGarrett Wollman label:
12748d96b17SGarrett Wollman 			switch (*++format) {
12848d96b17SGarrett Wollman 			case '\0':
12948d96b17SGarrett Wollman 				--format;
13048d96b17SGarrett Wollman 				break;
13148d96b17SGarrett Wollman 			case 'A':
132cdff05faSStefan Farfeleder 				pt = _add((t->tm_wday < 0 ||
133cdff05faSStefan Farfeleder 					t->tm_wday >= DAYSPERWEEK) ?
134930cd711SAlexey Zelkin 					"?" : tptr->weekday[t->tm_wday],
135c28fbb7bSGarrett Wollman 					pt, ptlim);
13648d96b17SGarrett Wollman 				continue;
13748d96b17SGarrett Wollman 			case 'a':
138cdff05faSStefan Farfeleder 				pt = _add((t->tm_wday < 0 ||
139cdff05faSStefan Farfeleder 					t->tm_wday >= DAYSPERWEEK) ?
140930cd711SAlexey Zelkin 					"?" : tptr->wday[t->tm_wday],
141c28fbb7bSGarrett Wollman 					pt, ptlim);
14248d96b17SGarrett Wollman 				continue;
14348d96b17SGarrett Wollman 			case 'B':
144cdff05faSStefan Farfeleder 				pt = _add((t->tm_mon < 0 ||
145cdff05faSStefan Farfeleder 					t->tm_mon >= MONSPERYEAR) ?
146930cd711SAlexey Zelkin 					"?" : (Oalternative ? tptr->alt_month :
147930cd711SAlexey Zelkin 					tptr->month)[t->tm_mon],
148c28fbb7bSGarrett Wollman 					pt, ptlim);
14948d96b17SGarrett Wollman 				continue;
15048d96b17SGarrett Wollman 			case 'b':
15148d96b17SGarrett Wollman 			case 'h':
152cdff05faSStefan Farfeleder 				pt = _add((t->tm_mon < 0 ||
153cdff05faSStefan Farfeleder 					t->tm_mon >= MONSPERYEAR) ?
154930cd711SAlexey Zelkin 					"?" : tptr->mon[t->tm_mon],
155c28fbb7bSGarrett Wollman 					pt, ptlim);
15648d96b17SGarrett Wollman 				continue;
15748d96b17SGarrett Wollman 			case 'C':
15848d96b17SGarrett Wollman 				/*
1593624c752SPedro F. Giffuni 				 * %C used to do a...
1603624c752SPedro F. Giffuni 				 *	_fmt("%a %b %e %X %Y", t);
1613624c752SPedro F. Giffuni 				 * ...whereas now POSIX 1003.2 calls for
1623624c752SPedro F. Giffuni 				 * something completely different.
1633624c752SPedro F. Giffuni 				 * (ado, 1993-05-24)
16448d96b17SGarrett Wollman 				 */
165dfc79e89SEdwin Groothuis 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
166e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
16748d96b17SGarrett Wollman 				continue;
168c28fbb7bSGarrett Wollman 			case 'c':
169cdff05faSStefan Farfeleder 				{
170cdff05faSStefan Farfeleder 				int warn2 = IN_SOME;
171cdff05faSStefan Farfeleder 
1723c87aa1dSDavid Chisnall 				pt = _fmt(tptr->c_fmt, t, pt, ptlim, &warn2, loc);
173cdff05faSStefan Farfeleder 				if (warn2 == IN_ALL)
174cdff05faSStefan Farfeleder 					warn2 = IN_THIS;
175cdff05faSStefan Farfeleder 				if (warn2 > *warnp)
176cdff05faSStefan Farfeleder 					*warnp = warn2;
177cdff05faSStefan Farfeleder 				}
17848d96b17SGarrett Wollman 				continue;
179c28fbb7bSGarrett Wollman 			case 'D':
1803c87aa1dSDavid Chisnall 				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp, loc);
18148d96b17SGarrett Wollman 				continue;
18248d96b17SGarrett Wollman 			case 'd':
183e66c50c7SPedro F. Giffuni 				pt = _conv(t->tm_mday,
184e66c50c7SPedro F. Giffuni 					fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
185e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
18648d96b17SGarrett Wollman 				continue;
18748d96b17SGarrett Wollman 			case 'E':
188c63a4303SAndrey A. Chernov 				if (Ealternative || Oalternative)
189c63a4303SAndrey A. Chernov 					break;
19011cd0d32SAndrey A. Chernov 				Ealternative++;
19111cd0d32SAndrey A. Chernov 				goto label;
19248d96b17SGarrett Wollman 			case 'O':
19348d96b17SGarrett Wollman 				/*
1943624c752SPedro F. Giffuni 				 * C99 locale modifiers.
1953624c752SPedro F. Giffuni 				 * The sequences
1963624c752SPedro F. Giffuni 				 *	%Ec %EC %Ex %EX %Ey %EY
1973624c752SPedro F. Giffuni 				 *	%Od %oe %OH %OI %Om %OM
1983624c752SPedro F. Giffuni 				 *	%OS %Ou %OU %OV %Ow %OW %Oy
1993624c752SPedro F. Giffuni 				 * are supposed to provide alternate
2003624c752SPedro F. Giffuni 				 * representations.
2013624c752SPedro F. Giffuni 				 *
2023624c752SPedro F. Giffuni 				 * FreeBSD extension
2033624c752SPedro F. Giffuni 				 *      %OB
20448d96b17SGarrett Wollman 				 */
205c63a4303SAndrey A. Chernov 				if (Ealternative || Oalternative)
206c63a4303SAndrey A. Chernov 					break;
20711cd0d32SAndrey A. Chernov 				Oalternative++;
20848d96b17SGarrett Wollman 				goto label;
20948d96b17SGarrett Wollman 			case 'e':
210d9506686SXin LI 				pt = _conv(t->tm_mday,
211e66c50c7SPedro F. Giffuni 					fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
212e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
21348d96b17SGarrett Wollman 				continue;
214c63a4303SAndrey A. Chernov 			case 'F':
2153c87aa1dSDavid Chisnall 				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp, loc);
216c63a4303SAndrey A. Chernov 				continue;
21748d96b17SGarrett Wollman 			case 'H':
218d9506686SXin LI 				pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_HMS][PadIndex],
219e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
22048d96b17SGarrett Wollman 				continue;
22148d96b17SGarrett Wollman 			case 'I':
22248d96b17SGarrett Wollman 				pt = _conv((t->tm_hour % 12) ?
22348d96b17SGarrett Wollman 					(t->tm_hour % 12) : 12,
2243624c752SPedro F. Giffuni 					fmt_padding[PAD_FMT_HMS][PadIndex],
225e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
22648d96b17SGarrett Wollman 				continue;
22748d96b17SGarrett Wollman 			case 'j':
228d9506686SXin LI 				pt = _conv(t->tm_yday + 1,
229e66c50c7SPedro F. Giffuni 					fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
230e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
23148d96b17SGarrett Wollman 				continue;
23248d96b17SGarrett Wollman 			case 'k':
23348d96b17SGarrett Wollman 				/*
2343624c752SPedro F. Giffuni 				 * This used to be...
2353624c752SPedro F. Giffuni 				 *	_conv(t->tm_hour % 12 ?
2363624c752SPedro F. Giffuni 				 *		t->tm_hour % 12 : 12, 2, ' ');
2373624c752SPedro F. Giffuni 				 * ...and has been changed to the below to
2383624c752SPedro F. Giffuni 				 * match SunOS 4.1.1 and Arnold Robbins'
2393624c752SPedro F. Giffuni 				 * strftime version 3.0. That is, "%k" and
2403624c752SPedro F. Giffuni 				 * "%l" have been swapped.
2413624c752SPedro F. Giffuni 				 * (ado, 1993-05-24)
24248d96b17SGarrett Wollman 				 */
243d9506686SXin LI 				pt = _conv(t->tm_hour, fmt_padding[PAD_FMT_SHMS][PadIndex],
244e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
24548d96b17SGarrett Wollman 				continue;
24648d96b17SGarrett Wollman #ifdef KITCHEN_SINK
24748d96b17SGarrett Wollman 			case 'K':
24848d96b17SGarrett Wollman 				/*
24948d96b17SGarrett Wollman 				** After all this time, still unclaimed!
25048d96b17SGarrett Wollman 				*/
25148d96b17SGarrett Wollman 				pt = _add("kitchen sink", pt, ptlim);
25248d96b17SGarrett Wollman 				continue;
25348d96b17SGarrett Wollman #endif /* defined KITCHEN_SINK */
25448d96b17SGarrett Wollman 			case 'l':
25548d96b17SGarrett Wollman 				/*
2563624c752SPedro F. Giffuni 				 * This used to be...
2573624c752SPedro F. Giffuni 				 *	_conv(t->tm_hour, 2, ' ');
2583624c752SPedro F. Giffuni 				 * ...and has been changed to the below to
2593624c752SPedro F. Giffuni 				 * match SunOS 4.1.1 and Arnold Robbin's
2603624c752SPedro F. Giffuni 				 * strftime version 3.0. That is, "%k" and
2613624c752SPedro F. Giffuni 				 * "%l" have been swapped.
2623624c752SPedro F. Giffuni 				 * (ado, 1993-05-24)
26348d96b17SGarrett Wollman 				 */
26448d96b17SGarrett Wollman 				pt = _conv((t->tm_hour % 12) ?
26548d96b17SGarrett Wollman 					(t->tm_hour % 12) : 12,
2663624c752SPedro F. Giffuni 					fmt_padding[PAD_FMT_SHMS][PadIndex],
267e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
26848d96b17SGarrett Wollman 				continue;
26948d96b17SGarrett Wollman 			case 'M':
270d9506686SXin LI 				pt = _conv(t->tm_min, fmt_padding[PAD_FMT_HMS][PadIndex],
271e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
27248d96b17SGarrett Wollman 				continue;
27348d96b17SGarrett Wollman 			case 'm':
274d9506686SXin LI 				pt = _conv(t->tm_mon + 1,
2753624c752SPedro F. Giffuni 					fmt_padding[PAD_FMT_MONTH][PadIndex],
276e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
27748d96b17SGarrett Wollman 				continue;
27848d96b17SGarrett Wollman 			case 'n':
27948d96b17SGarrett Wollman 				pt = _add("\n", pt, ptlim);
28048d96b17SGarrett Wollman 				continue;
28148d96b17SGarrett Wollman 			case 'p':
282cdff05faSStefan Farfeleder 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
2833624c752SPedro F. Giffuni 					tptr->pm : tptr->am,
28448d96b17SGarrett Wollman 					pt, ptlim);
28548d96b17SGarrett Wollman 				continue;
28648d96b17SGarrett Wollman 			case 'R':
2873c87aa1dSDavid Chisnall 				pt = _fmt("%H:%M", t, pt, ptlim, warnp, loc);
28848d96b17SGarrett Wollman 				continue;
28948d96b17SGarrett Wollman 			case 'r':
290cdff05faSStefan Farfeleder 				pt = _fmt(tptr->ampm_fmt, t, pt, ptlim,
2913c87aa1dSDavid Chisnall 					warnp, loc);
29248d96b17SGarrett Wollman 				continue;
29348d96b17SGarrett Wollman 			case 'S':
294d9506686SXin LI 				pt = _conv(t->tm_sec, fmt_padding[PAD_FMT_HMS][PadIndex],
295e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
29648d96b17SGarrett Wollman 				continue;
2975ad178d8SAndrey A. Chernov 			case 's':
298c6966b0cSBruce Evans 				{
299c6966b0cSBruce Evans 					struct tm	tm;
300c6966b0cSBruce Evans 					char		buf[INT_STRLEN_MAXIMUM(
301c6966b0cSBruce Evans 								time_t) + 1];
302c6966b0cSBruce Evans 					time_t		mkt;
303c6966b0cSBruce Evans 
304c6966b0cSBruce Evans 					tm = *t;
305*46c59934SDag-Erling Smørgrav 					mkt = timeoff(&tm, t->tm_gmtoff);
306c6966b0cSBruce Evans 					if (TYPE_SIGNED(time_t))
307e66c50c7SPedro F. Giffuni 						(void) sprintf_l(buf, loc, "%ld",
308c6966b0cSBruce Evans 							(long) mkt);
309e66c50c7SPedro F. Giffuni 					else	(void) sprintf_l(buf, loc, "%lu",
310c6966b0cSBruce Evans 							(unsigned long) mkt);
311c6966b0cSBruce Evans 					pt = _add(buf, pt, ptlim);
312c6966b0cSBruce Evans 				}
3135ad178d8SAndrey A. Chernov 				continue;
31448d96b17SGarrett Wollman 			case 'T':
3153c87aa1dSDavid Chisnall 				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp, loc);
31648d96b17SGarrett Wollman 				continue;
31748d96b17SGarrett Wollman 			case 't':
31848d96b17SGarrett Wollman 				pt = _add("\t", pt, ptlim);
31948d96b17SGarrett Wollman 				continue;
32048d96b17SGarrett Wollman 			case 'U':
321cdff05faSStefan Farfeleder 				pt = _conv((t->tm_yday + DAYSPERWEEK -
322cdff05faSStefan Farfeleder 					t->tm_wday) / DAYSPERWEEK,
323e66c50c7SPedro F. Giffuni 					fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
324e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
32548d96b17SGarrett Wollman 				continue;
32648d96b17SGarrett Wollman 			case 'u':
32748d96b17SGarrett Wollman 				/*
3283624c752SPedro F. Giffuni 				 * From Arnold Robbins' strftime version 3.0:
3293624c752SPedro F. Giffuni 				 * "ISO 8601: Weekday as a decimal number
3303624c752SPedro F. Giffuni 				 * [1 (Monday) - 7]"
3313624c752SPedro F. Giffuni 				 * (ado, 1993-05-24)
33248d96b17SGarrett Wollman 				 */
333cdff05faSStefan Farfeleder 				pt = _conv((t->tm_wday == 0) ?
334cdff05faSStefan Farfeleder 					DAYSPERWEEK : t->tm_wday,
335e66c50c7SPedro F. Giffuni 					"%d", pt, ptlim, loc);
33648d96b17SGarrett Wollman 				continue;
337b9b51fb7SWolfgang Helbig 			case 'V':	/* ISO 8601 week number */
338b9b51fb7SWolfgang Helbig 			case 'G':	/* ISO 8601 year (four digits) */
339b9b51fb7SWolfgang Helbig 			case 'g':	/* ISO 8601 year (two digits) */
34048d96b17SGarrett Wollman /*
3413624c752SPedro F. Giffuni  * From Arnold Robbins' strftime version 3.0: "the week number of the
3423624c752SPedro F. Giffuni  * year (the first Monday as the first day of week 1) as a decimal number
3433624c752SPedro F. Giffuni  * (01-53)."
3443624c752SPedro F. Giffuni  * (ado, 1993-05-24)
3453624c752SPedro F. Giffuni  *
3463624c752SPedro F. Giffuni  * From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
3473624c752SPedro F. Giffuni  * "Week 01 of a year is per definition the first week which has the
3483624c752SPedro F. Giffuni  * Thursday in this year, which is equivalent to the week which contains
3493624c752SPedro F. Giffuni  * the fourth day of January. In other words, the first week of a new year
3503624c752SPedro F. Giffuni  * is the week which has the majority of its days in the new year. Week 01
3513624c752SPedro F. Giffuni  * might also contain days from the previous year and the week before week
3523624c752SPedro F. Giffuni  * 01 of a year is the last week (52 or 53) of the previous year even if
3533624c752SPedro F. Giffuni  * it contains days from the new year. A week starts with Monday (day 1)
3543624c752SPedro F. Giffuni  * and ends with Sunday (day 7). For example, the first week of the year
3553624c752SPedro F. Giffuni  * 1997 lasts from 1996-12-30 to 1997-01-05..."
3563624c752SPedro F. Giffuni  * (ado, 1996-01-02)
35748d96b17SGarrett Wollman  */
35848d96b17SGarrett Wollman 				{
359b9b51fb7SWolfgang Helbig 					int	year;
360dfc79e89SEdwin Groothuis 					int	base;
361b9b51fb7SWolfgang Helbig 					int	yday;
362b9b51fb7SWolfgang Helbig 					int	wday;
363b9b51fb7SWolfgang Helbig 					int	w;
36448d96b17SGarrett Wollman 
365dfc79e89SEdwin Groothuis 					year = t->tm_year;
366dfc79e89SEdwin Groothuis 					base = TM_YEAR_BASE;
367b9b51fb7SWolfgang Helbig 					yday = t->tm_yday;
368b9b51fb7SWolfgang Helbig 					wday = t->tm_wday;
369b9b51fb7SWolfgang Helbig 					for ( ; ; ) {
370b9b51fb7SWolfgang Helbig 						int	len;
371b9b51fb7SWolfgang Helbig 						int	bot;
372b9b51fb7SWolfgang Helbig 						int	top;
373b9b51fb7SWolfgang Helbig 
374dfc79e89SEdwin Groothuis 						len = isleap_sum(year, base) ?
375b9b51fb7SWolfgang Helbig 							DAYSPERLYEAR :
376b9b51fb7SWolfgang Helbig 							DAYSPERNYEAR;
37748d96b17SGarrett Wollman 						/*
3783624c752SPedro F. Giffuni 						 * What yday (-3 ... 3) does
3793624c752SPedro F. Giffuni 						 * the ISO year begin on?
38048d96b17SGarrett Wollman 						 */
381b9b51fb7SWolfgang Helbig 						bot = ((yday + 11 - wday) %
382b9b51fb7SWolfgang Helbig 							DAYSPERWEEK) - 3;
38348d96b17SGarrett Wollman 						/*
3843624c752SPedro F. Giffuni 						 * What yday does the NEXT
3853624c752SPedro F. Giffuni 						 * ISO year begin on?
38648d96b17SGarrett Wollman 						 */
387b9b51fb7SWolfgang Helbig 						top = bot -
388b9b51fb7SWolfgang Helbig 							(len % DAYSPERWEEK);
389b9b51fb7SWolfgang Helbig 						if (top < -3)
390b9b51fb7SWolfgang Helbig 							top += DAYSPERWEEK;
391b9b51fb7SWolfgang Helbig 						top += len;
392b9b51fb7SWolfgang Helbig 						if (yday >= top) {
393dfc79e89SEdwin Groothuis 							++base;
394b9b51fb7SWolfgang Helbig 							w = 1;
395b9b51fb7SWolfgang Helbig 							break;
396fe463a8fSGarrett Wollman 						}
397b9b51fb7SWolfgang Helbig 						if (yday >= bot) {
398b9b51fb7SWolfgang Helbig 							w = 1 + ((yday - bot) /
399b9b51fb7SWolfgang Helbig 								DAYSPERWEEK);
400b9b51fb7SWolfgang Helbig 							break;
401b9b51fb7SWolfgang Helbig 						}
402dfc79e89SEdwin Groothuis 						--base;
403dfc79e89SEdwin Groothuis 						yday += isleap_sum(year, base) ?
404b9b51fb7SWolfgang Helbig 							DAYSPERLYEAR :
405b9b51fb7SWolfgang Helbig 							DAYSPERNYEAR;
406b9b51fb7SWolfgang Helbig 					}
407b9b51fb7SWolfgang Helbig #ifdef XPG4_1994_04_09
408dfc79e89SEdwin Groothuis 					if ((w == 52 &&
409dfc79e89SEdwin Groothuis 						t->tm_mon == TM_JANUARY) ||
410dfc79e89SEdwin Groothuis 						(w == 1 &&
411dfc79e89SEdwin Groothuis 						t->tm_mon == TM_DECEMBER))
412b9b51fb7SWolfgang Helbig 							w = 53;
413b9b51fb7SWolfgang Helbig #endif /* defined XPG4_1994_04_09 */
414b9b51fb7SWolfgang Helbig 					if (*format == 'V')
415d9506686SXin LI 						pt = _conv(w, fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
416e66c50c7SPedro F. Giffuni 							pt, ptlim, loc);
417b9b51fb7SWolfgang Helbig 					else if (*format == 'g') {
418cdff05faSStefan Farfeleder 						*warnp = IN_ALL;
419dfc79e89SEdwin Groothuis 						pt = _yconv(year, base, 0, 1,
420e66c50c7SPedro F. Giffuni 							pt, ptlim, loc);
421dfc79e89SEdwin Groothuis 					} else	pt = _yconv(year, base, 1, 1,
422e66c50c7SPedro F. Giffuni 							pt, ptlim, loc);
42348d96b17SGarrett Wollman 				}
42448d96b17SGarrett Wollman 				continue;
42548d96b17SGarrett Wollman 			case 'v':
42648d96b17SGarrett Wollman 				/*
4273624c752SPedro F. Giffuni 				 * From Arnold Robbins' strftime version 3.0:
4283624c752SPedro F. Giffuni 				 * "date as dd-bbb-YYYY"
4293624c752SPedro F. Giffuni 				 * (ado, 1993-05-24)
43048d96b17SGarrett Wollman 				 */
4313c87aa1dSDavid Chisnall 				pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp, loc);
43248d96b17SGarrett Wollman 				continue;
43348d96b17SGarrett Wollman 			case 'W':
434cdff05faSStefan Farfeleder 				pt = _conv((t->tm_yday + DAYSPERWEEK -
43548d96b17SGarrett Wollman 					(t->tm_wday ?
436cdff05faSStefan Farfeleder 					(t->tm_wday - 1) :
437cdff05faSStefan Farfeleder 					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
438e66c50c7SPedro F. Giffuni 					fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
439e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
44048d96b17SGarrett Wollman 				continue;
44148d96b17SGarrett Wollman 			case 'w':
442e66c50c7SPedro F. Giffuni 				pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
44348d96b17SGarrett Wollman 				continue;
444c28fbb7bSGarrett Wollman 			case 'X':
4453c87aa1dSDavid Chisnall 				pt = _fmt(tptr->X_fmt, t, pt, ptlim, warnp, loc);
446c28fbb7bSGarrett Wollman 				continue;
447c28fbb7bSGarrett Wollman 			case 'x':
448cdff05faSStefan Farfeleder 				{
449cdff05faSStefan Farfeleder 				int	warn2 = IN_SOME;
450cdff05faSStefan Farfeleder 
4513c87aa1dSDavid Chisnall 				pt = _fmt(tptr->x_fmt, t, pt, ptlim, &warn2, loc);
452cdff05faSStefan Farfeleder 				if (warn2 == IN_ALL)
453cdff05faSStefan Farfeleder 					warn2 = IN_THIS;
454cdff05faSStefan Farfeleder 				if (warn2 > *warnp)
455cdff05faSStefan Farfeleder 					*warnp = warn2;
456cdff05faSStefan Farfeleder 				}
457c28fbb7bSGarrett Wollman 				continue;
45848d96b17SGarrett Wollman 			case 'y':
459cdff05faSStefan Farfeleder 				*warnp = IN_ALL;
460dfc79e89SEdwin Groothuis 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
461e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
46248d96b17SGarrett Wollman 				continue;
46348d96b17SGarrett Wollman 			case 'Y':
464dfc79e89SEdwin Groothuis 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
465e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
46648d96b17SGarrett Wollman 				continue;
46748d96b17SGarrett Wollman 			case 'Z':
468cdff05faSStefan Farfeleder #ifdef TM_ZONE
469cdff05faSStefan Farfeleder 				if (t->TM_ZONE != NULL)
470cdff05faSStefan Farfeleder 					pt = _add(t->TM_ZONE, pt, ptlim);
47148d96b17SGarrett Wollman 				else
472cdff05faSStefan Farfeleder #endif /* defined TM_ZONE */
473cdff05faSStefan Farfeleder 				if (t->tm_isdst >= 0)
474cdff05faSStefan Farfeleder 					pt = _add(tzname[t->tm_isdst != 0],
47548d96b17SGarrett Wollman 						pt, ptlim);
476cdff05faSStefan Farfeleder 				/*
4773624c752SPedro F. Giffuni 				 * C99 says that %Z must be replaced by the
4783624c752SPedro F. Giffuni 				 * empty string if the time zone is not
4793624c752SPedro F. Giffuni 				 * determinable.
480cdff05faSStefan Farfeleder 				 */
48148d96b17SGarrett Wollman 				continue;
482208b5822SJoerg Wunsch 			case 'z':
483208b5822SJoerg Wunsch 				{
484cdff05faSStefan Farfeleder 				int		diff;
485cdff05faSStefan Farfeleder 				char const *	sign;
486cdff05faSStefan Farfeleder 
487cdff05faSStefan Farfeleder 				if (t->tm_isdst < 0)
488cdff05faSStefan Farfeleder 					continue;
489cdff05faSStefan Farfeleder #ifdef TM_GMTOFF
490cdff05faSStefan Farfeleder 				diff = t->TM_GMTOFF;
491cdff05faSStefan Farfeleder #else /* !defined TM_GMTOFF */
492cdff05faSStefan Farfeleder 				/*
4933624c752SPedro F. Giffuni 				 * C99 says that the UTC offset must
4943624c752SPedro F. Giffuni 				 * be computed by looking only at
4953624c752SPedro F. Giffuni 				 * tm_isdst. This requirement is
4963624c752SPedro F. Giffuni 				 * incorrect, since it means the code
4973624c752SPedro F. Giffuni 				 * must rely on magic (in this case
4983624c752SPedro F. Giffuni 				 * altzone and timezone), and the
4993624c752SPedro F. Giffuni 				 * magic might not have the correct
5003624c752SPedro F. Giffuni 				 * offset. Doing things correctly is
5013624c752SPedro F. Giffuni 				 * tricky and requires disobeying C99;
5023624c752SPedro F. Giffuni 				 * see GNU C strftime for details.
5033624c752SPedro F. Giffuni 				 * For now, punt and conform to the
5043624c752SPedro F. Giffuni 				 * standard, even though it's incorrect.
5053624c752SPedro F. Giffuni 				 *
5063624c752SPedro F. Giffuni 				 * C99 says that %z must be replaced by the
5073624c752SPedro F. Giffuni 				 * empty string if the time zone is not
5083624c752SPedro F. Giffuni 				 * determinable, so output nothing if the
5093624c752SPedro F. Giffuni 				 * appropriate variables are not available.
510cdff05faSStefan Farfeleder 				 */
511cdff05faSStefan Farfeleder 				if (t->tm_isdst == 0)
512cdff05faSStefan Farfeleder #ifdef USG_COMPAT
513cdff05faSStefan Farfeleder 					diff = -timezone;
514cdff05faSStefan Farfeleder #else /* !defined USG_COMPAT */
515cdff05faSStefan Farfeleder 					continue;
516cdff05faSStefan Farfeleder #endif /* !defined USG_COMPAT */
517cdff05faSStefan Farfeleder 				else
518cdff05faSStefan Farfeleder #ifdef ALTZONE
519cdff05faSStefan Farfeleder 					diff = -altzone;
520cdff05faSStefan Farfeleder #else /* !defined ALTZONE */
521cdff05faSStefan Farfeleder 					continue;
522cdff05faSStefan Farfeleder #endif /* !defined ALTZONE */
523cdff05faSStefan Farfeleder #endif /* !defined TM_GMTOFF */
524cdff05faSStefan Farfeleder 				if (diff < 0) {
525cdff05faSStefan Farfeleder 					sign = "-";
526cdff05faSStefan Farfeleder 					diff = -diff;
5273624c752SPedro F. Giffuni 				} else
5283624c752SPedro F. Giffuni 					sign = "+";
529cdff05faSStefan Farfeleder 				pt = _add(sign, pt, ptlim);
530dfc79e89SEdwin Groothuis 				diff /= SECSPERMIN;
531dfc79e89SEdwin Groothuis 				diff = (diff / MINSPERHOUR) * 100 +
532dfc79e89SEdwin Groothuis 					(diff % MINSPERHOUR);
533dfc79e89SEdwin Groothuis 				pt = _conv(diff,
534e66c50c7SPedro F. Giffuni 					fmt_padding[PAD_FMT_YEAR][PadIndex],
535e66c50c7SPedro F. Giffuni 					pt, ptlim, loc);
536208b5822SJoerg Wunsch 				}
537208b5822SJoerg Wunsch 				continue;
538c28fbb7bSGarrett Wollman 			case '+':
539cdff05faSStefan Farfeleder 				pt = _fmt(tptr->date_fmt, t, pt, ptlim,
5403c87aa1dSDavid Chisnall 					warnp, loc);
541c28fbb7bSGarrett Wollman 				continue;
542d9506686SXin LI 			case '-':
543d9506686SXin LI 				if (PadIndex != PAD_DEFAULT)
544d9506686SXin LI 					break;
545d9506686SXin LI 				PadIndex = PAD_LESS;
546d9506686SXin LI 				goto label;
547d9506686SXin LI 			case '_':
548d9506686SXin LI 				if (PadIndex != PAD_DEFAULT)
549d9506686SXin LI 					break;
550d9506686SXin LI 				PadIndex = PAD_SPACE;
551d9506686SXin LI 				goto label;
552d9506686SXin LI 			case '0':
553d9506686SXin LI 				if (PadIndex != PAD_DEFAULT)
554d9506686SXin LI 					break;
555d9506686SXin LI 				PadIndex = PAD_ZERO;
556d9506686SXin LI 				goto label;
55748d96b17SGarrett Wollman 			case '%':
55848d96b17SGarrett Wollman 			/*
5593624c752SPedro F. Giffuni 			 * X311J/88-090 (4.12.3.5): if conversion char is
5603624c752SPedro F. Giffuni 			 * undefined, behavior is undefined. Print out the
5613624c752SPedro F. Giffuni 			 * character itself as printf(3) also does.
56248d96b17SGarrett Wollman 			 */
56348d96b17SGarrett Wollman 			default:
56448d96b17SGarrett Wollman 				break;
56548d96b17SGarrett Wollman 			}
56648d96b17SGarrett Wollman 		}
56748d96b17SGarrett Wollman 		if (pt == ptlim)
56848d96b17SGarrett Wollman 			break;
56948d96b17SGarrett Wollman 		*pt++ = *format;
57048d96b17SGarrett Wollman 	}
5713624c752SPedro F. Giffuni 	return (pt);
57248d96b17SGarrett Wollman }
57348d96b17SGarrett Wollman 
57448d96b17SGarrett Wollman static char *
_conv(const int n,const char * const format,char * const pt,const char * const ptlim,locale_t loc)57585cef632SCraig Rodrigues _conv(const int n, const char * const format, char * const pt,
57685cef632SCraig Rodrigues     const char * const ptlim, locale_t  loc)
57748d96b17SGarrett Wollman {
57848d96b17SGarrett Wollman 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
57948d96b17SGarrett Wollman 
580e66c50c7SPedro F. Giffuni 	(void) sprintf_l(buf, loc, format, n);
58148d96b17SGarrett Wollman 	return _add(buf, pt, ptlim);
58248d96b17SGarrett Wollman }
58348d96b17SGarrett Wollman 
58448d96b17SGarrett Wollman static char *
_add(const char * str,char * pt,const char * const ptlim)58585cef632SCraig Rodrigues _add(const char *str, char *pt, const char * const ptlim)
58648d96b17SGarrett Wollman {
58748d96b17SGarrett Wollman 	while (pt < ptlim && (*pt = *str++) != '\0')
58848d96b17SGarrett Wollman 		++pt;
5893624c752SPedro F. Giffuni 	return (pt);
59048d96b17SGarrett Wollman }
591dfc79e89SEdwin Groothuis 
592dfc79e89SEdwin Groothuis /*
5933624c752SPedro F. Giffuni  * POSIX and the C Standard are unclear or inconsistent about
5943624c752SPedro F. Giffuni  * what %C and %y do if the year is negative or exceeds 9999.
5953624c752SPedro F. Giffuni  * Use the convention that %C concatenated with %y yields the
5963624c752SPedro F. Giffuni  * same output as %Y, and that %Y contains at least 4 bytes,
5973624c752SPedro F. Giffuni  * with more only if necessary.
598dfc79e89SEdwin Groothuis  */
599dfc79e89SEdwin Groothuis 
600dfc79e89SEdwin Groothuis static char *
_yconv(const int a,const int b,const int convert_top,const int convert_yy,char * pt,const char * const ptlim,locale_t loc)60185cef632SCraig Rodrigues _yconv(const int a, const int b, const int convert_top, const int convert_yy,
60285cef632SCraig Rodrigues     char *pt, const char * const ptlim, locale_t  loc)
603dfc79e89SEdwin Groothuis {
604dfc79e89SEdwin Groothuis 	register int	lead;
605dfc79e89SEdwin Groothuis 	register int	trail;
606dfc79e89SEdwin Groothuis 
607dfc79e89SEdwin Groothuis #define	DIVISOR	100
608dfc79e89SEdwin Groothuis 	trail = a % DIVISOR + b % DIVISOR;
609dfc79e89SEdwin Groothuis 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
610dfc79e89SEdwin Groothuis 	trail %= DIVISOR;
611dfc79e89SEdwin Groothuis 	if (trail < 0 && lead > 0) {
612dfc79e89SEdwin Groothuis 		trail += DIVISOR;
613dfc79e89SEdwin Groothuis 		--lead;
614dfc79e89SEdwin Groothuis 	} else if (lead < 0 && trail > 0) {
615dfc79e89SEdwin Groothuis 		trail -= DIVISOR;
616dfc79e89SEdwin Groothuis 		++lead;
617dfc79e89SEdwin Groothuis 	}
618dfc79e89SEdwin Groothuis 	if (convert_top) {
619dfc79e89SEdwin Groothuis 		if (lead == 0 && trail < 0)
620dfc79e89SEdwin Groothuis 			pt = _add("-0", pt, ptlim);
621e66c50c7SPedro F. Giffuni 		else	pt = _conv(lead, "%02d", pt, ptlim, loc);
622dfc79e89SEdwin Groothuis 	}
623dfc79e89SEdwin Groothuis 	if (convert_yy)
624e66c50c7SPedro F. Giffuni 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt,
625e66c50c7SPedro F. Giffuni 		     ptlim, loc);
6263624c752SPedro F. Giffuni 	return (pt);
627dfc79e89SEdwin Groothuis }
628