Home | History | Annotate | Line # | Download | only in time
strftime.c revision 1.14
      1  1.14    itojun /*	$NetBSD: strftime.c,v 1.14 2002/10/02 06:56:24 itojun Exp $	*/
      2   1.1       mrg 
      3   1.3  christos #include <sys/cdefs.h>
      4   1.1       mrg #if defined(LIBC_SCCS) && !defined(lint)
      5   1.3  christos #if 0
      6  1.13    kleink static char	elsieid[] = "@(#)strftime.c	7.64";
      7   1.3  christos #else
      8  1.14    itojun __RCSID("$NetBSD: strftime.c,v 1.14 2002/10/02 06:56:24 itojun Exp $");
      9   1.3  christos #endif
     10   1.1       mrg #endif /* LIBC_SCCS and not lint */
     11   1.1       mrg 
     12   1.4       jtc #include "namespace.h"
     13  1.12    kleink 
     14  1.12    kleink /*
     15  1.12    kleink ** Based on the UCB version with the ID appearing below.
     16  1.12    kleink ** This is ANSIish only when "multibyte character == plain character".
     17  1.12    kleink */
     18  1.12    kleink 
     19  1.11      taca #include "private.h"
     20  1.12    kleink 
     21  1.12    kleink /*
     22  1.14    itojun ** Copyright (c) 1989, 1993
     23  1.14    itojun **	The Regents of the University of California.  All rights reserved.
     24  1.12    kleink **
     25  1.14    itojun ** Redistribution and use in source and binary forms, with or without
     26  1.14    itojun ** modification, are permitted provided that the following conditions
     27  1.14    itojun ** are met:
     28  1.14    itojun ** 1. Redistributions of source code must retain the above copyright
     29  1.14    itojun **    notice, this list of conditions and the following disclaimer.
     30  1.14    itojun ** 2. Redistributions in binary form must reproduce the above copyright
     31  1.14    itojun **    notice, this list of conditions and the following disclaimer in the
     32  1.14    itojun **    documentation and/or other materials provided with the distribution.
     33  1.14    itojun ** 3. All advertising materials mentioning features or use of this software
     34  1.14    itojun **    must display the following acknowledgement:
     35  1.14    itojun **	This product includes software developed by the University of
     36  1.14    itojun **	California, Berkeley and its contributors.
     37  1.14    itojun ** 4. Neither the name of the University nor the names of its contributors
     38  1.14    itojun **    may be used to endorse or promote products derived from this software
     39  1.14    itojun **    without specific prior written permission.
     40  1.14    itojun **
     41  1.14    itojun ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     42  1.14    itojun ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     43  1.14    itojun ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     44  1.14    itojun ** ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     45  1.14    itojun ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     46  1.14    itojun ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     47  1.14    itojun ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48  1.14    itojun ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     49  1.14    itojun ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     50  1.14    itojun ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     51  1.14    itojun ** SUCH DAMAGE.
     52  1.12    kleink */
     53  1.12    kleink 
     54  1.12    kleink #ifndef LIBC_SCCS
     55  1.12    kleink #ifndef lint
     56  1.12    kleink static const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
     57  1.12    kleink #endif /* !defined lint */
     58  1.12    kleink #endif /* !defined LIBC_SCCS */
     59  1.12    kleink 
     60  1.12    kleink #include "tzfile.h"
     61  1.12    kleink #include "fcntl.h"
     62  1.12    kleink #include "locale.h"
     63  1.12    kleink 
     64  1.12    kleink #include "sys/localedef.h"
     65  1.12    kleink #define Locale	_CurrentTimeLocale
     66  1.12    kleink 
     67  1.12    kleink static char *	_add P((const char *, char *, const char *));
     68  1.12    kleink static char *	_conv P((int, const char *, char *, const char *));
     69  1.12    kleink static char *	_fmt P((const char *, const struct tm *, char *, const char *, int *));
     70  1.12    kleink 
     71  1.12    kleink #define NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
     72  1.12    kleink 
     73  1.12    kleink #ifndef YEAR_2000_NAME
     74  1.12    kleink #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
     75  1.12    kleink #endif /* !defined YEAR_2000_NAME */
     76  1.12    kleink 
     77  1.12    kleink 
     78  1.12    kleink #define IN_NONE	0
     79  1.12    kleink #define IN_SOME	1
     80  1.12    kleink #define IN_THIS	2
     81  1.12    kleink #define IN_ALL	3
     82   1.1       mrg 
     83   1.1       mrg size_t
     84   1.1       mrg strftime(s, maxsize, format, t)
     85  1.12    kleink char * const		s;
     86  1.12    kleink const size_t		maxsize;
     87  1.12    kleink const char * const	format;
     88  1.12    kleink const struct tm * const	t;
     89   1.1       mrg {
     90  1.12    kleink 	char *	p;
     91  1.12    kleink 	int	warn;
     92   1.5    kleink 
     93   1.1       mrg 	tzset();
     94  1.12    kleink 	warn = IN_NONE;
     95  1.12    kleink 	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
     96  1.12    kleink #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
     97  1.12    kleink 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
     98  1.12    kleink 		(void) fprintf(stderr, "\n");
     99  1.12    kleink 		if (format == NULL)
    100  1.12    kleink 			(void) fprintf(stderr, "NULL strftime format ");
    101  1.12    kleink 		else	(void) fprintf(stderr, "strftime format \"%s\" ",
    102  1.12    kleink 				format);
    103  1.12    kleink 		(void) fprintf(stderr, "yields only two digits of years in ");
    104  1.12    kleink 		if (warn == IN_SOME)
    105  1.12    kleink 			(void) fprintf(stderr, "some locales");
    106  1.12    kleink 		else if (warn == IN_THIS)
    107  1.12    kleink 			(void) fprintf(stderr, "the current locale");
    108  1.12    kleink 		else	(void) fprintf(stderr, "all locales");
    109  1.12    kleink 		(void) fprintf(stderr, "\n");
    110  1.12    kleink 	}
    111  1.12    kleink #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
    112  1.12    kleink 	if (p == s + maxsize)
    113  1.12    kleink 		return 0;
    114  1.12    kleink 	*p = '\0';
    115  1.12    kleink 	return p - s;
    116   1.1       mrg }
    117   1.1       mrg 
    118  1.12    kleink static char *
    119  1.12    kleink _fmt(format, t, pt, ptlim, warnp)
    120  1.12    kleink const char *		format;
    121  1.12    kleink const struct tm * const	t;
    122  1.12    kleink char *			pt;
    123  1.12    kleink const char * const	ptlim;
    124  1.12    kleink int *			warnp;
    125   1.1       mrg {
    126  1.12    kleink 	for ( ; *format; ++format) {
    127   1.1       mrg 		if (*format == '%') {
    128  1.12    kleink label:
    129  1.12    kleink 			switch (*++format) {
    130   1.1       mrg 			case '\0':
    131   1.1       mrg 				--format;
    132   1.1       mrg 				break;
    133   1.1       mrg 			case 'A':
    134  1.12    kleink 				pt = _add((t->tm_wday < 0 ||
    135  1.12    kleink 					t->tm_wday >= DAYSPERWEEK) ?
    136  1.12    kleink 					"?" : Locale->day[t->tm_wday],
    137  1.12    kleink 					pt, ptlim);
    138   1.1       mrg 				continue;
    139   1.1       mrg 			case 'a':
    140  1.12    kleink 				pt = _add((t->tm_wday < 0 ||
    141  1.12    kleink 					t->tm_wday >= DAYSPERWEEK) ?
    142  1.12    kleink 					"?" : Locale->abday[t->tm_wday],
    143  1.12    kleink 					pt, ptlim);
    144   1.1       mrg 				continue;
    145   1.1       mrg 			case 'B':
    146  1.12    kleink 				pt = _add((t->tm_mon < 0 ||
    147  1.12    kleink 					t->tm_mon >= MONSPERYEAR) ?
    148  1.12    kleink 					"?" : Locale->mon[t->tm_mon],
    149  1.12    kleink 					pt, ptlim);
    150   1.1       mrg 				continue;
    151   1.1       mrg 			case 'b':
    152   1.1       mrg 			case 'h':
    153  1.12    kleink 				pt = _add((t->tm_mon < 0 ||
    154  1.12    kleink 					t->tm_mon >= MONSPERYEAR) ?
    155  1.12    kleink 					"?" : Locale->abmon[t->tm_mon],
    156  1.12    kleink 					pt, ptlim);
    157   1.1       mrg 				continue;
    158   1.1       mrg 			case 'C':
    159  1.12    kleink 				/*
    160  1.12    kleink 				** %C used to do a...
    161  1.12    kleink 				**	_fmt("%a %b %e %X %Y", t);
    162  1.12    kleink 				** ...whereas now POSIX 1003.2 calls for
    163  1.12    kleink 				** something completely different.
    164  1.12    kleink 				** (ado, 1993-05-24)
    165  1.12    kleink 				*/
    166  1.12    kleink 				pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
    167  1.12    kleink 					"%02d", pt, ptlim);
    168   1.1       mrg 				continue;
    169   1.1       mrg 			case 'c':
    170  1.12    kleink 				{
    171  1.12    kleink 				int warn2 = IN_SOME;
    172  1.12    kleink 
    173  1.12    kleink 				pt = _fmt(Locale->d_t_fmt, t, pt, ptlim, warnp);
    174  1.12    kleink 				if (warn2 == IN_ALL)
    175  1.12    kleink 					warn2 = IN_THIS;
    176  1.12    kleink 				if (warn2 > *warnp)
    177  1.12    kleink 					*warnp = warn2;
    178  1.12    kleink 				}
    179   1.1       mrg 				continue;
    180   1.1       mrg 			case 'D':
    181  1.12    kleink 				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
    182   1.1       mrg 				continue;
    183   1.1       mrg 			case 'd':
    184  1.12    kleink 				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
    185   1.1       mrg 				continue;
    186  1.12    kleink 			case 'E':
    187  1.12    kleink 			case 'O':
    188  1.12    kleink 				/*
    189  1.12    kleink 				** C99 locale modifiers.
    190  1.12    kleink 				** The sequences
    191  1.12    kleink 				**	%Ec %EC %Ex %EX %Ey %EY
    192  1.12    kleink 				**	%Od %oe %OH %OI %Om %OM
    193  1.12    kleink 				**	%OS %Ou %OU %OV %Ow %OW %Oy
    194  1.12    kleink 				** are supposed to provide alternate
    195  1.12    kleink 				** representations.
    196  1.12    kleink 				*/
    197  1.12    kleink 				goto label;
    198   1.1       mrg 			case 'e':
    199  1.12    kleink 				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
    200  1.10    kleink 				continue;
    201  1.10    kleink 			case 'F':
    202  1.12    kleink 				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
    203   1.1       mrg 				continue;
    204   1.1       mrg 			case 'H':
    205  1.12    kleink 				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
    206   1.1       mrg 				continue;
    207   1.1       mrg 			case 'I':
    208  1.12    kleink 				pt = _conv((t->tm_hour % 12) ?
    209  1.12    kleink 					(t->tm_hour % 12) : 12,
    210  1.12    kleink 					"%02d", pt, ptlim);
    211   1.1       mrg 				continue;
    212   1.1       mrg 			case 'j':
    213  1.12    kleink 				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
    214   1.1       mrg 				continue;
    215   1.1       mrg 			case 'k':
    216  1.12    kleink 				/*
    217  1.12    kleink 				** This used to be...
    218  1.12    kleink 				**	_conv(t->tm_hour % 12 ?
    219  1.12    kleink 				**		t->tm_hour % 12 : 12, 2, ' ');
    220  1.12    kleink 				** ...and has been changed to the below to
    221  1.12    kleink 				** match SunOS 4.1.1 and Arnold Robbins'
    222  1.12    kleink 				** strftime version 3.0.  That is, "%k" and
    223  1.12    kleink 				** "%l" have been swapped.
    224  1.12    kleink 				** (ado, 1993-05-24)
    225  1.12    kleink 				*/
    226  1.12    kleink 				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
    227  1.12    kleink 				continue;
    228  1.12    kleink #ifdef KITCHEN_SINK
    229  1.12    kleink 			case 'K':
    230  1.12    kleink 				/*
    231  1.12    kleink 				** After all this time, still unclaimed!
    232  1.12    kleink 				*/
    233  1.12    kleink 				pt = _add("kitchen sink", pt, ptlim);
    234   1.1       mrg 				continue;
    235  1.12    kleink #endif /* defined KITCHEN_SINK */
    236   1.1       mrg 			case 'l':
    237  1.12    kleink 				/*
    238  1.12    kleink 				** This used to be...
    239  1.12    kleink 				**	_conv(t->tm_hour, 2, ' ');
    240  1.12    kleink 				** ...and has been changed to the below to
    241  1.12    kleink 				** match SunOS 4.1.1 and Arnold Robbin's
    242  1.12    kleink 				** strftime version 3.0.  That is, "%k" and
    243  1.12    kleink 				** "%l" have been swapped.
    244  1.12    kleink 				** (ado, 1993-05-24)
    245  1.12    kleink 				*/
    246  1.12    kleink 				pt = _conv((t->tm_hour % 12) ?
    247  1.12    kleink 					(t->tm_hour % 12) : 12,
    248  1.12    kleink 					"%2d", pt, ptlim);
    249   1.1       mrg 				continue;
    250   1.1       mrg 			case 'M':
    251  1.12    kleink 				pt = _conv(t->tm_min, "%02d", pt, ptlim);
    252   1.1       mrg 				continue;
    253   1.1       mrg 			case 'm':
    254  1.12    kleink 				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
    255   1.1       mrg 				continue;
    256   1.1       mrg 			case 'n':
    257  1.12    kleink 				pt = _add("\n", pt, ptlim);
    258   1.1       mrg 				continue;
    259   1.1       mrg 			case 'p':
    260  1.12    kleink 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
    261  1.12    kleink 					Locale->am_pm[1] :
    262  1.12    kleink 					Locale->am_pm[0],
    263  1.12    kleink 					pt, ptlim);
    264   1.1       mrg 				continue;
    265   1.1       mrg 			case 'R':
    266  1.12    kleink 				pt = _fmt("%H:%M", t, pt, ptlim, warnp);
    267   1.1       mrg 				continue;
    268   1.1       mrg 			case 'r':
    269  1.12    kleink 				pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
    270   1.1       mrg 				continue;
    271   1.1       mrg 			case 'S':
    272  1.12    kleink 				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
    273   1.1       mrg 				continue;
    274   1.1       mrg 			case 's':
    275  1.12    kleink 				{
    276  1.12    kleink 					struct tm	tm;
    277  1.12    kleink 					char		buf[INT_STRLEN_MAXIMUM(
    278  1.12    kleink 								time_t) + 1];
    279  1.12    kleink 					time_t		mkt;
    280  1.12    kleink 
    281  1.12    kleink 					tm = *t;
    282  1.12    kleink 					mkt = mktime(&tm);
    283  1.12    kleink 					/* CONSTCOND */
    284  1.12    kleink 					if (TYPE_SIGNED(time_t))
    285  1.12    kleink 						(void) sprintf(buf, "%ld",
    286  1.12    kleink 							(long) mkt);
    287  1.12    kleink 					else	(void) sprintf(buf, "%lu",
    288  1.12    kleink 							(unsigned long) mkt);
    289  1.12    kleink 					pt = _add(buf, pt, ptlim);
    290  1.12    kleink 				}
    291   1.1       mrg 				continue;
    292   1.1       mrg 			case 'T':
    293  1.12    kleink 				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
    294   1.1       mrg 				continue;
    295   1.1       mrg 			case 't':
    296  1.12    kleink 				pt = _add("\t", pt, ptlim);
    297   1.1       mrg 				continue;
    298   1.1       mrg 			case 'U':
    299  1.12    kleink 				pt = _conv((t->tm_yday + DAYSPERWEEK -
    300  1.12    kleink 					t->tm_wday) / DAYSPERWEEK,
    301  1.12    kleink 					"%02d", pt, ptlim);
    302   1.1       mrg 				continue;
    303   1.1       mrg 			case 'u':
    304  1.12    kleink 				/*
    305  1.12    kleink 				** From Arnold Robbins' strftime version 3.0:
    306  1.12    kleink 				** "ISO 8601: Weekday as a decimal number
    307  1.12    kleink 				** [1 (Monday) - 7]"
    308  1.12    kleink 				** (ado, 1993-05-24)
    309  1.12    kleink 				*/
    310  1.12    kleink 				pt = _conv((t->tm_wday == 0) ?
    311  1.12    kleink 					DAYSPERWEEK : t->tm_wday,
    312  1.12    kleink 					"%d", pt, ptlim);
    313   1.1       mrg 				continue;
    314   1.8  augustss 			case 'V':	/* ISO 8601 week number */
    315   1.8  augustss 			case 'G':	/* ISO 8601 year (four digits) */
    316   1.8  augustss 			case 'g':	/* ISO 8601 year (two digits) */
    317   1.8  augustss /*
    318   1.8  augustss ** From Arnold Robbins' strftime version 3.0:  "the week number of the
    319   1.8  augustss ** year (the first Monday as the first day of week 1) as a decimal number
    320   1.8  augustss ** (01-53)."
    321   1.8  augustss ** (ado, 1993-05-24)
    322   1.8  augustss **
    323   1.8  augustss ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
    324   1.8  augustss ** "Week 01 of a year is per definition the first week which has the
    325   1.8  augustss ** Thursday in this year, which is equivalent to the week which contains
    326   1.8  augustss ** the fourth day of January. In other words, the first week of a new year
    327   1.8  augustss ** is the week which has the majority of its days in the new year. Week 01
    328   1.8  augustss ** might also contain days from the previous year and the week before week
    329   1.8  augustss ** 01 of a year is the last week (52 or 53) of the previous year even if
    330   1.8  augustss ** it contains days from the new year. A week starts with Monday (day 1)
    331   1.8  augustss ** and ends with Sunday (day 7).  For example, the first week of the year
    332   1.8  augustss ** 1997 lasts from 1996-12-30 to 1997-01-05..."
    333   1.8  augustss ** (ado, 1996-01-02)
    334   1.8  augustss */
    335   1.1       mrg 				{
    336   1.8  augustss 					int	year;
    337   1.8  augustss 					int	yday;
    338   1.8  augustss 					int	wday;
    339   1.8  augustss 					int	w;
    340   1.8  augustss 
    341   1.8  augustss 					year = t->tm_year + TM_YEAR_BASE;
    342   1.8  augustss 					yday = t->tm_yday;
    343   1.8  augustss 					wday = t->tm_wday;
    344   1.8  augustss 					for ( ; ; ) {
    345   1.8  augustss 						int	len;
    346   1.8  augustss 						int	bot;
    347   1.8  augustss 						int	top;
    348   1.8  augustss 
    349   1.8  augustss 						len = isleap(year) ?
    350   1.8  augustss 							DAYSPERLYEAR :
    351   1.8  augustss 							DAYSPERNYEAR;
    352   1.8  augustss 						/*
    353   1.8  augustss 						** What yday (-3 ... 3) does
    354   1.8  augustss 						** the ISO year begin on?
    355   1.8  augustss 						*/
    356   1.8  augustss 						bot = ((yday + 11 - wday) %
    357   1.8  augustss 							DAYSPERWEEK) - 3;
    358   1.8  augustss 						/*
    359   1.8  augustss 						** What yday does the NEXT
    360   1.8  augustss 						** ISO year begin on?
    361   1.8  augustss 						*/
    362   1.8  augustss 						top = bot -
    363   1.8  augustss 							(len % DAYSPERWEEK);
    364   1.8  augustss 						if (top < -3)
    365   1.8  augustss 							top += DAYSPERWEEK;
    366   1.8  augustss 						top += len;
    367   1.8  augustss 						if (yday >= top) {
    368   1.8  augustss 							++year;
    369   1.8  augustss 							w = 1;
    370   1.8  augustss 							break;
    371   1.8  augustss 						}
    372   1.8  augustss 						if (yday >= bot) {
    373   1.8  augustss 							w = 1 + ((yday - bot) /
    374   1.8  augustss 								DAYSPERWEEK);
    375   1.8  augustss 							break;
    376   1.8  augustss 						}
    377   1.8  augustss 						--year;
    378   1.8  augustss 						yday += isleap(year) ?
    379   1.8  augustss 							DAYSPERLYEAR :
    380   1.8  augustss 							DAYSPERNYEAR;
    381   1.8  augustss 					}
    382   1.8  augustss #ifdef XPG4_1994_04_09
    383   1.8  augustss 					if ((w == 52
    384   1.8  augustss 					     && t->tm_mon == TM_JANUARY)
    385   1.8  augustss 					    || (w == 1
    386   1.8  augustss 						&& t->tm_mon == TM_DECEMBER))
    387   1.8  augustss 						w = 53;
    388   1.8  augustss #endif /* defined XPG4_1994_04_09 */
    389  1.12    kleink 					if (*format == 'V')
    390  1.12    kleink 						pt = _conv(w, "%02d",
    391  1.12    kleink 							pt, ptlim);
    392  1.12    kleink 					else if (*format == 'g') {
    393  1.12    kleink 						*warnp = IN_ALL;
    394  1.12    kleink 						pt = _conv(year % 100, "%02d",
    395  1.12    kleink 							pt, ptlim);
    396  1.12    kleink 					} else	pt = _conv(year, "%04d",
    397  1.12    kleink 							pt, ptlim);
    398   1.1       mrg 				}
    399   1.1       mrg 				continue;
    400  1.12    kleink 			case 'v':
    401  1.12    kleink 				/*
    402  1.12    kleink 				** From Arnold Robbins' strftime version 3.0:
    403  1.12    kleink 				** "date as dd-bbb-YYYY"
    404  1.12    kleink 				** (ado, 1993-05-24)
    405  1.12    kleink 				*/
    406  1.12    kleink 				pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
    407  1.12    kleink 				continue;
    408   1.1       mrg 			case 'W':
    409  1.12    kleink 				pt = _conv((t->tm_yday + DAYSPERWEEK -
    410  1.12    kleink 					(t->tm_wday ?
    411  1.12    kleink 					(t->tm_wday - 1) :
    412  1.12    kleink 					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
    413  1.12    kleink 					"%02d", pt, ptlim);
    414   1.1       mrg 				continue;
    415   1.1       mrg 			case 'w':
    416  1.12    kleink 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
    417  1.12    kleink 				continue;
    418  1.12    kleink 			case 'X':
    419  1.12    kleink 				pt = _fmt(Locale->t_fmt, t, pt, ptlim, warnp);
    420   1.1       mrg 				continue;
    421   1.1       mrg 			case 'x':
    422  1.12    kleink 				{
    423  1.12    kleink 				int	warn2 = IN_SOME;
    424  1.12    kleink 
    425  1.12    kleink 				pt = _fmt(Locale->d_fmt, t, pt, ptlim, &warn2);
    426  1.12    kleink 				if (warn2 == IN_ALL)
    427  1.12    kleink 					warn2 = IN_THIS;
    428  1.12    kleink 				if (warn2 > *warnp)
    429  1.12    kleink 					*warnp = warn2;
    430  1.12    kleink 				}
    431   1.1       mrg 				continue;
    432   1.1       mrg 			case 'y':
    433  1.12    kleink 				*warnp = IN_ALL;
    434  1.12    kleink 				pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
    435  1.12    kleink 					"%02d", pt, ptlim);
    436   1.1       mrg 				continue;
    437   1.1       mrg 			case 'Y':
    438  1.12    kleink 				pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
    439  1.12    kleink 					pt, ptlim);
    440   1.1       mrg 				continue;
    441   1.1       mrg 			case 'Z':
    442  1.11      taca #ifdef TM_ZONE
    443  1.12    kleink 				if (t->TM_ZONE != NULL)
    444  1.12    kleink 					pt = _add(t->TM_ZONE, pt, ptlim);
    445  1.12    kleink 				else
    446  1.12    kleink #endif /* defined TM_ZONE */
    447  1.12    kleink 				if (t->tm_isdst >= 0)
    448  1.12    kleink 					pt = _add(tzname[t->tm_isdst != 0],
    449  1.12    kleink 						pt, ptlim);
    450  1.12    kleink 				/*
    451  1.12    kleink 				** C99 says that %Z must be replaced by the
    452  1.12    kleink 				** empty string if the time zone is not
    453  1.12    kleink 				** determinable.
    454  1.12    kleink 				*/
    455  1.12    kleink 				continue;
    456  1.12    kleink 			case 'z':
    457  1.12    kleink 				{
    458  1.12    kleink 				int		diff;
    459  1.12    kleink 				char const *	sign;
    460  1.12    kleink 
    461  1.12    kleink 				if (t->tm_isdst < 0)
    462  1.12    kleink 					continue;
    463  1.12    kleink #ifdef TM_GMTOFF
    464  1.12    kleink 				diff = (int)t->TM_GMTOFF;
    465  1.12    kleink #else /* !defined TM_GMTOFF */
    466  1.12    kleink 				/*
    467  1.12    kleink 				** C99 says that the UTC offset must
    468  1.12    kleink 				** be computed by looking only at
    469  1.12    kleink 				** tm_isdst.  This requirement is
    470  1.12    kleink 				** incorrect, since it means the code
    471  1.12    kleink 				** must rely on magic (in this case
    472  1.12    kleink 				** altzone and timezone), and the
    473  1.12    kleink 				** magic might not have the correct
    474  1.12    kleink 				** offset.  Doing things correctly is
    475  1.12    kleink 				** tricky and requires disobeying C99;
    476  1.12    kleink 				** see GNU C strftime for details.
    477  1.12    kleink 				** For now, punt and conform to the
    478  1.12    kleink 				** standard, even though it's incorrect.
    479  1.12    kleink 				**
    480  1.12    kleink 				** C99 says that %z must be replaced by the
    481  1.12    kleink 				** empty string if the time zone is not
    482  1.12    kleink 				** determinable, so output nothing if the
    483  1.12    kleink 				** appropriate variables are not available.
    484  1.12    kleink 				*/
    485  1.12    kleink 				if (t->tm_isdst == 0)
    486  1.12    kleink #ifdef USG_COMPAT
    487  1.12    kleink 					diff = -timezone;
    488  1.13    kleink #else /* !defined USG_COMPAT */
    489  1.12    kleink 					continue;
    490  1.12    kleink #endif /* !defined USG_COMPAT */
    491  1.12    kleink 				else
    492  1.12    kleink #ifdef ALTZONE
    493  1.12    kleink 					diff = -altzone;
    494  1.12    kleink #else /* !defined ALTZONE */
    495  1.12    kleink 					continue;
    496  1.12    kleink #endif /* !defined ALTZONE */
    497  1.12    kleink #endif /* !defined TM_GMTOFF */
    498  1.12    kleink 				if (diff < 0) {
    499  1.12    kleink 					sign = "-";
    500  1.12    kleink 					diff = -diff;
    501  1.12    kleink 				} else	sign = "+";
    502  1.12    kleink 				pt = _add(sign, pt, ptlim);
    503  1.12    kleink 				diff /= 60;
    504  1.12    kleink 				pt = _conv((diff/60)*100 + diff%60,
    505  1.12    kleink 					"%04d", pt, ptlim);
    506  1.12    kleink 				}
    507   1.1       mrg 				continue;
    508  1.12    kleink #if 0
    509  1.12    kleink 			case '+':
    510  1.12    kleink 				pt = _fmt(Locale->date_fmt, t, pt, ptlim,
    511  1.12    kleink 					warnp);
    512  1.12    kleink 				continue;
    513  1.12    kleink #endif
    514   1.1       mrg 			case '%':
    515   1.1       mrg 			/*
    516  1.12    kleink 			** X311J/88-090 (4.12.3.5): if conversion char is
    517  1.12    kleink 			** undefined, behavior is undefined.  Print out the
    518  1.12    kleink 			** character itself as printf(3) also does.
    519  1.12    kleink 			*/
    520   1.1       mrg 			default:
    521   1.1       mrg 				break;
    522   1.1       mrg 			}
    523   1.1       mrg 		}
    524  1.12    kleink 		if (pt == ptlim)
    525  1.12    kleink 			break;
    526  1.12    kleink 		*pt++ = *format;
    527   1.1       mrg 	}
    528  1.12    kleink 	return pt;
    529   1.1       mrg }
    530   1.1       mrg 
    531  1.12    kleink static char *
    532  1.12    kleink _conv(n, format, pt, ptlim)
    533  1.12    kleink const int		n;
    534  1.12    kleink const char * const	format;
    535  1.12    kleink char * const		pt;
    536  1.12    kleink const char * const	ptlim;
    537   1.1       mrg {
    538  1.12    kleink 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
    539   1.1       mrg 
    540  1.12    kleink 	(void) sprintf(buf, format, n);
    541  1.12    kleink 	return _add(buf, pt, ptlim);
    542   1.1       mrg }
    543   1.1       mrg 
    544  1.12    kleink static char *
    545   1.5    kleink _add(str, pt, ptlim)
    546  1.12    kleink const char *		str;
    547  1.12    kleink char *			pt;
    548  1.12    kleink const char * const	ptlim;
    549   1.1       mrg {
    550  1.12    kleink 	while (pt < ptlim && (*pt = *str++) != '\0')
    551  1.12    kleink 		++pt;
    552  1.12    kleink 	return pt;
    553   1.1       mrg }
    554