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