Home | History | Annotate | Line # | Download | only in time
strftime.c revision 1.25
      1  1.25     joerg /*	$NetBSD: strftime.c,v 1.25 2013/04/21 17:45:46 joerg 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.20   mlelstv static char	elsieid[] = "@(#)strftime.c	8.3";
      8   1.3  christos #else
      9  1.25     joerg __RCSID("$NetBSD: strftime.c,v 1.25 2013/04/21 17:45:46 joerg Exp $");
     10   1.3  christos #endif
     11   1.1       mrg #endif /* LIBC_SCCS and not lint */
     12   1.1       mrg 
     13   1.4       jtc #include "namespace.h"
     14  1.12    kleink 
     15  1.25     joerg #include <stddef.h>
     16  1.25     joerg #include <locale.h>
     17  1.25     joerg #include "setlocale_local.h"
     18  1.25     joerg 
     19  1.12    kleink /*
     20  1.24  christos ** Based on the UCB version with the copyright notice and sccsid
     21  1.24  christos ** appearing below.
     22  1.24  christos **
     23  1.12    kleink ** This is ANSIish only when "multibyte character == plain character".
     24  1.12    kleink */
     25  1.12    kleink 
     26  1.11      taca #include "private.h"
     27  1.12    kleink 
     28  1.16    kleink /*
     29  1.16    kleink ** We don't use these extensions in strftime operation even when
     30  1.16    kleink ** supported by the local tzcode configuration.  A strictly
     31  1.16    kleink ** conforming C application may leave them in undefined state.
     32  1.16    kleink */
     33  1.16    kleink 
     34  1.15    kleink #ifdef _LIBC
     35  1.15    kleink #undef TM_ZONE
     36  1.16    kleink #undef TM_GMTOFF
     37  1.15    kleink #endif
     38  1.15    kleink 
     39  1.12    kleink /*
     40  1.14    itojun ** Copyright (c) 1989, 1993
     41  1.14    itojun **	The Regents of the University of California.  All rights reserved.
     42  1.12    kleink **
     43  1.14    itojun ** Redistribution and use in source and binary forms, with or without
     44  1.14    itojun ** modification, are permitted provided that the following conditions
     45  1.14    itojun ** are met:
     46  1.14    itojun ** 1. Redistributions of source code must retain the above copyright
     47  1.14    itojun **    notice, this list of conditions and the following disclaimer.
     48  1.14    itojun ** 2. Redistributions in binary form must reproduce the above copyright
     49  1.14    itojun **    notice, this list of conditions and the following disclaimer in the
     50  1.14    itojun **    documentation and/or other materials provided with the distribution.
     51  1.14    itojun ** 3. All advertising materials mentioning features or use of this software
     52  1.14    itojun **    must display the following acknowledgement:
     53  1.14    itojun **	This product includes software developed by the University of
     54  1.14    itojun **	California, Berkeley and its contributors.
     55  1.14    itojun ** 4. Neither the name of the University nor the names of its contributors
     56  1.14    itojun **    may be used to endorse or promote products derived from this software
     57  1.14    itojun **    without specific prior written permission.
     58  1.14    itojun **
     59  1.14    itojun ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  1.14    itojun ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  1.14    itojun ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  1.14    itojun ** ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  1.14    itojun ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  1.14    itojun ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  1.14    itojun ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  1.14    itojun ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  1.14    itojun ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  1.14    itojun ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  1.14    itojun ** SUCH DAMAGE.
     70  1.12    kleink */
     71  1.12    kleink 
     72  1.12    kleink #ifndef LIBC_SCCS
     73  1.12    kleink #ifndef lint
     74  1.12    kleink static const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
     75  1.12    kleink #endif /* !defined lint */
     76  1.12    kleink #endif /* !defined LIBC_SCCS */
     77  1.12    kleink 
     78  1.12    kleink #include "tzfile.h"
     79  1.12    kleink #include "fcntl.h"
     80  1.12    kleink #include "locale.h"
     81  1.12    kleink 
     82  1.21  christos #ifdef __weak_alias
     83  1.25     joerg __weak_alias(strftime_l, _strftime_l)
     84  1.25     joerg __weak_alias(strftime_lz, _strftime_lz)
     85  1.21  christos __weak_alias(strftime_z, _strftime_z)
     86  1.21  christos #endif
     87  1.21  christos 
     88  1.12    kleink #include "sys/localedef.h"
     89  1.25     joerg #define _TIME_LOCALE(loc) \
     90  1.25     joerg     ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
     91  1.20   mlelstv #define c_fmt   d_t_fmt
     92  1.12    kleink 
     93  1.20   mlelstv static char *	_add(const char *, char *, const char *);
     94  1.20   mlelstv static char *	_conv(int, const char *, char *, const char *);
     95  1.21  christos static char *	_fmt(const timezone_t, const char *, const struct tm *, char *,
     96  1.25     joerg 			const char *, int *, locale_t);
     97  1.20   mlelstv static char *	_yconv(int, int, int, int, char *, const char *);
     98  1.12    kleink 
     99  1.20   mlelstv extern char *	tzname[];
    100  1.12    kleink 
    101  1.12    kleink #ifndef YEAR_2000_NAME
    102  1.12    kleink #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
    103  1.12    kleink #endif /* !defined YEAR_2000_NAME */
    104  1.12    kleink 
    105  1.12    kleink #define IN_NONE	0
    106  1.12    kleink #define IN_SOME	1
    107  1.12    kleink #define IN_THIS	2
    108  1.12    kleink #define IN_ALL	3
    109   1.1       mrg 
    110   1.1       mrg size_t
    111  1.25     joerg strftime_z(const timezone_t sp, char * __restrict s, size_t maxsize,
    112  1.25     joerg     const char * __restrict format, const struct tm * __restrict t)
    113  1.25     joerg {
    114  1.25     joerg 	return strftime_lz(sp, s, maxsize, format, t, *_current_locale());
    115  1.25     joerg }
    116  1.25     joerg 
    117  1.25     joerg size_t
    118  1.25     joerg strftime_lz(const timezone_t sp, char *const s, const size_t maxsize,
    119  1.25     joerg     const char *const format, const struct tm *const t, locale_t loc)
    120   1.1       mrg {
    121  1.12    kleink 	char *	p;
    122  1.12    kleink 	int	warn;
    123   1.5    kleink 
    124  1.25     joerg 	if (loc == NULL)
    125  1.25     joerg 		loc = _C_locale;
    126  1.25     joerg 
    127  1.12    kleink 	warn = IN_NONE;
    128  1.21  christos 	p = _fmt(sp, ((format == NULL) ? "%c" : format), t, s, s + maxsize,
    129  1.25     joerg 	    &warn, loc);
    130  1.12    kleink #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
    131  1.12    kleink 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
    132  1.12    kleink 		(void) fprintf(stderr, "\n");
    133  1.12    kleink 		if (format == NULL)
    134  1.12    kleink 			(void) fprintf(stderr, "NULL strftime format ");
    135  1.12    kleink 		else	(void) fprintf(stderr, "strftime format \"%s\" ",
    136  1.12    kleink 				format);
    137  1.12    kleink 		(void) fprintf(stderr, "yields only two digits of years in ");
    138  1.12    kleink 		if (warn == IN_SOME)
    139  1.12    kleink 			(void) fprintf(stderr, "some locales");
    140  1.12    kleink 		else if (warn == IN_THIS)
    141  1.12    kleink 			(void) fprintf(stderr, "the current locale");
    142  1.12    kleink 		else	(void) fprintf(stderr, "all locales");
    143  1.12    kleink 		(void) fprintf(stderr, "\n");
    144  1.12    kleink 	}
    145  1.12    kleink #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
    146  1.12    kleink 	if (p == s + maxsize)
    147  1.12    kleink 		return 0;
    148  1.12    kleink 	*p = '\0';
    149  1.12    kleink 	return p - s;
    150   1.1       mrg }
    151   1.1       mrg 
    152  1.12    kleink static char *
    153  1.23  christos _fmt(const timezone_t sp, const char *format, const struct tm *const t,
    154  1.25     joerg 	char *pt, const char *const ptlim, int *warnp, locale_t loc)
    155   1.1       mrg {
    156  1.12    kleink 	for ( ; *format; ++format) {
    157   1.1       mrg 		if (*format == '%') {
    158  1.12    kleink label:
    159  1.12    kleink 			switch (*++format) {
    160   1.1       mrg 			case '\0':
    161   1.1       mrg 				--format;
    162   1.1       mrg 				break;
    163   1.1       mrg 			case 'A':
    164  1.12    kleink 				pt = _add((t->tm_wday < 0 ||
    165  1.12    kleink 					t->tm_wday >= DAYSPERWEEK) ?
    166  1.25     joerg 					"?" : _TIME_LOCALE(loc)->day[t->tm_wday],
    167  1.12    kleink 					pt, ptlim);
    168   1.1       mrg 				continue;
    169   1.1       mrg 			case 'a':
    170  1.12    kleink 				pt = _add((t->tm_wday < 0 ||
    171  1.12    kleink 					t->tm_wday >= DAYSPERWEEK) ?
    172  1.25     joerg 					"?" : _TIME_LOCALE(loc)->abday[t->tm_wday],
    173  1.12    kleink 					pt, ptlim);
    174   1.1       mrg 				continue;
    175   1.1       mrg 			case 'B':
    176  1.12    kleink 				pt = _add((t->tm_mon < 0 ||
    177  1.12    kleink 					t->tm_mon >= MONSPERYEAR) ?
    178  1.25     joerg 					"?" : _TIME_LOCALE(loc)->mon[t->tm_mon],
    179  1.12    kleink 					pt, ptlim);
    180   1.1       mrg 				continue;
    181   1.1       mrg 			case 'b':
    182   1.1       mrg 			case 'h':
    183  1.12    kleink 				pt = _add((t->tm_mon < 0 ||
    184  1.12    kleink 					t->tm_mon >= MONSPERYEAR) ?
    185  1.25     joerg 					"?" : _TIME_LOCALE(loc)->abmon[t->tm_mon],
    186  1.12    kleink 					pt, ptlim);
    187   1.1       mrg 				continue;
    188   1.1       mrg 			case 'C':
    189  1.12    kleink 				/*
    190  1.12    kleink 				** %C used to do a...
    191  1.12    kleink 				**	_fmt("%a %b %e %X %Y", t);
    192  1.12    kleink 				** ...whereas now POSIX 1003.2 calls for
    193  1.12    kleink 				** something completely different.
    194  1.12    kleink 				** (ado, 1993-05-24)
    195  1.12    kleink 				*/
    196  1.20   mlelstv 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
    197  1.20   mlelstv 					pt, ptlim);
    198   1.1       mrg 				continue;
    199   1.1       mrg 			case 'c':
    200  1.12    kleink 				{
    201  1.12    kleink 				int warn2 = IN_SOME;
    202  1.12    kleink 
    203  1.25     joerg 				pt = _fmt(sp, _TIME_LOCALE(loc)->c_fmt, t, pt,
    204  1.25     joerg 				    ptlim, &warn2, loc);
    205  1.12    kleink 				if (warn2 == IN_ALL)
    206  1.12    kleink 					warn2 = IN_THIS;
    207  1.12    kleink 				if (warn2 > *warnp)
    208  1.12    kleink 					*warnp = warn2;
    209  1.12    kleink 				}
    210   1.1       mrg 				continue;
    211   1.1       mrg 			case 'D':
    212  1.25     joerg 				pt = _fmt(sp, "%m/%d/%y", t, pt, ptlim, warnp,
    213  1.25     joerg 				    loc);
    214   1.1       mrg 				continue;
    215   1.1       mrg 			case 'd':
    216  1.12    kleink 				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
    217   1.1       mrg 				continue;
    218  1.12    kleink 			case 'E':
    219  1.12    kleink 			case 'O':
    220  1.12    kleink 				/*
    221  1.12    kleink 				** C99 locale modifiers.
    222  1.12    kleink 				** The sequences
    223  1.12    kleink 				**	%Ec %EC %Ex %EX %Ey %EY
    224  1.12    kleink 				**	%Od %oe %OH %OI %Om %OM
    225  1.12    kleink 				**	%OS %Ou %OU %OV %Ow %OW %Oy
    226  1.12    kleink 				** are supposed to provide alternate
    227  1.12    kleink 				** representations.
    228  1.12    kleink 				*/
    229  1.12    kleink 				goto label;
    230   1.1       mrg 			case 'e':
    231  1.12    kleink 				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
    232  1.10    kleink 				continue;
    233  1.10    kleink 			case 'F':
    234  1.25     joerg 				pt = _fmt(sp, "%Y-%m-%d", t, pt, ptlim, warnp,
    235  1.25     joerg 				    loc);
    236   1.1       mrg 				continue;
    237   1.1       mrg 			case 'H':
    238  1.12    kleink 				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
    239   1.1       mrg 				continue;
    240   1.1       mrg 			case 'I':
    241  1.12    kleink 				pt = _conv((t->tm_hour % 12) ?
    242  1.12    kleink 					(t->tm_hour % 12) : 12,
    243  1.12    kleink 					"%02d", pt, ptlim);
    244   1.1       mrg 				continue;
    245   1.1       mrg 			case 'j':
    246  1.12    kleink 				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
    247   1.1       mrg 				continue;
    248   1.1       mrg 			case 'k':
    249  1.12    kleink 				/*
    250  1.12    kleink 				** This used to be...
    251  1.12    kleink 				**	_conv(t->tm_hour % 12 ?
    252  1.12    kleink 				**		t->tm_hour % 12 : 12, 2, ' ');
    253  1.12    kleink 				** ...and has been changed to the below to
    254  1.12    kleink 				** match SunOS 4.1.1 and Arnold Robbins'
    255  1.20   mlelstv 				** strftime version 3.0. That is, "%k" and
    256  1.12    kleink 				** "%l" have been swapped.
    257  1.12    kleink 				** (ado, 1993-05-24)
    258  1.12    kleink 				*/
    259  1.12    kleink 				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
    260  1.12    kleink 				continue;
    261  1.12    kleink #ifdef KITCHEN_SINK
    262  1.12    kleink 			case 'K':
    263  1.12    kleink 				/*
    264  1.12    kleink 				** After all this time, still unclaimed!
    265  1.12    kleink 				*/
    266  1.12    kleink 				pt = _add("kitchen sink", pt, ptlim);
    267   1.1       mrg 				continue;
    268  1.12    kleink #endif /* defined KITCHEN_SINK */
    269   1.1       mrg 			case 'l':
    270  1.12    kleink 				/*
    271  1.12    kleink 				** This used to be...
    272  1.12    kleink 				**	_conv(t->tm_hour, 2, ' ');
    273  1.12    kleink 				** ...and has been changed to the below to
    274  1.12    kleink 				** match SunOS 4.1.1 and Arnold Robbin's
    275  1.20   mlelstv 				** strftime version 3.0. That is, "%k" and
    276  1.12    kleink 				** "%l" have been swapped.
    277  1.12    kleink 				** (ado, 1993-05-24)
    278  1.12    kleink 				*/
    279  1.12    kleink 				pt = _conv((t->tm_hour % 12) ?
    280  1.12    kleink 					(t->tm_hour % 12) : 12,
    281  1.12    kleink 					"%2d", pt, ptlim);
    282   1.1       mrg 				continue;
    283   1.1       mrg 			case 'M':
    284  1.12    kleink 				pt = _conv(t->tm_min, "%02d", pt, ptlim);
    285   1.1       mrg 				continue;
    286   1.1       mrg 			case 'm':
    287  1.12    kleink 				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
    288   1.1       mrg 				continue;
    289   1.1       mrg 			case 'n':
    290  1.12    kleink 				pt = _add("\n", pt, ptlim);
    291   1.1       mrg 				continue;
    292   1.1       mrg 			case 'p':
    293  1.12    kleink 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
    294  1.25     joerg 					_TIME_LOCALE(loc)->am_pm[1] :
    295  1.25     joerg 					_TIME_LOCALE(loc)->am_pm[0],
    296  1.12    kleink 					pt, ptlim);
    297   1.1       mrg 				continue;
    298   1.1       mrg 			case 'R':
    299  1.25     joerg 				pt = _fmt(sp, "%H:%M", t, pt, ptlim, warnp,
    300  1.25     joerg 				    loc);
    301   1.1       mrg 				continue;
    302   1.1       mrg 			case 'r':
    303  1.25     joerg 				pt = _fmt(sp, _TIME_LOCALE(loc)->t_fmt_ampm, t,
    304  1.25     joerg 				    pt, ptlim, warnp, loc);
    305   1.1       mrg 				continue;
    306   1.1       mrg 			case 'S':
    307  1.12    kleink 				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
    308   1.1       mrg 				continue;
    309   1.1       mrg 			case 's':
    310  1.12    kleink 				{
    311  1.12    kleink 					struct tm	tm;
    312  1.12    kleink 					char		buf[INT_STRLEN_MAXIMUM(
    313  1.12    kleink 								time_t) + 1];
    314  1.12    kleink 					time_t		mkt;
    315  1.12    kleink 
    316  1.12    kleink 					tm = *t;
    317  1.12    kleink 					mkt = mktime(&tm);
    318  1.12    kleink 					/* CONSTCOND */
    319  1.12    kleink 					if (TYPE_SIGNED(time_t))
    320  1.19  christos 						(void) snprintf(buf, sizeof(buf),
    321  1.19  christos 						    "%lld", (long long) mkt);
    322  1.19  christos 					else	(void) snprintf(buf, sizeof(buf),
    323  1.19  christos 						    "%llu", (unsigned long long)
    324  1.19  christos 						    mkt);
    325  1.12    kleink 					pt = _add(buf, pt, ptlim);
    326  1.12    kleink 				}
    327   1.1       mrg 				continue;
    328   1.1       mrg 			case 'T':
    329  1.25     joerg 				pt = _fmt(sp, "%H:%M:%S", t, pt, ptlim, warnp,
    330  1.25     joerg 				    loc);
    331   1.1       mrg 				continue;
    332   1.1       mrg 			case 't':
    333  1.12    kleink 				pt = _add("\t", pt, ptlim);
    334   1.1       mrg 				continue;
    335   1.1       mrg 			case 'U':
    336  1.12    kleink 				pt = _conv((t->tm_yday + DAYSPERWEEK -
    337  1.12    kleink 					t->tm_wday) / DAYSPERWEEK,
    338  1.12    kleink 					"%02d", pt, ptlim);
    339   1.1       mrg 				continue;
    340   1.1       mrg 			case 'u':
    341  1.12    kleink 				/*
    342  1.12    kleink 				** From Arnold Robbins' strftime version 3.0:
    343  1.12    kleink 				** "ISO 8601: Weekday as a decimal number
    344  1.12    kleink 				** [1 (Monday) - 7]"
    345  1.12    kleink 				** (ado, 1993-05-24)
    346  1.12    kleink 				*/
    347  1.12    kleink 				pt = _conv((t->tm_wday == 0) ?
    348  1.12    kleink 					DAYSPERWEEK : t->tm_wday,
    349  1.12    kleink 					"%d", pt, ptlim);
    350   1.1       mrg 				continue;
    351   1.8  augustss 			case 'V':	/* ISO 8601 week number */
    352   1.8  augustss 			case 'G':	/* ISO 8601 year (four digits) */
    353   1.8  augustss 			case 'g':	/* ISO 8601 year (two digits) */
    354   1.8  augustss /*
    355  1.20   mlelstv ** From Arnold Robbins' strftime version 3.0: "the week number of the
    356   1.8  augustss ** year (the first Monday as the first day of week 1) as a decimal number
    357   1.8  augustss ** (01-53)."
    358   1.8  augustss ** (ado, 1993-05-24)
    359   1.8  augustss **
    360   1.8  augustss ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
    361   1.8  augustss ** "Week 01 of a year is per definition the first week which has the
    362   1.8  augustss ** Thursday in this year, which is equivalent to the week which contains
    363   1.8  augustss ** the fourth day of January. In other words, the first week of a new year
    364   1.8  augustss ** is the week which has the majority of its days in the new year. Week 01
    365   1.8  augustss ** might also contain days from the previous year and the week before week
    366   1.8  augustss ** 01 of a year is the last week (52 or 53) of the previous year even if
    367   1.8  augustss ** it contains days from the new year. A week starts with Monday (day 1)
    368  1.20   mlelstv ** and ends with Sunday (day 7). For example, the first week of the year
    369   1.8  augustss ** 1997 lasts from 1996-12-30 to 1997-01-05..."
    370   1.8  augustss ** (ado, 1996-01-02)
    371   1.8  augustss */
    372   1.1       mrg 				{
    373   1.8  augustss 					int	year;
    374  1.20   mlelstv 					int	base;
    375   1.8  augustss 					int	yday;
    376   1.8  augustss 					int	wday;
    377   1.8  augustss 					int	w;
    378   1.8  augustss 
    379  1.20   mlelstv 					year = t->tm_year;
    380  1.20   mlelstv 					base = TM_YEAR_BASE;
    381   1.8  augustss 					yday = t->tm_yday;
    382   1.8  augustss 					wday = t->tm_wday;
    383   1.8  augustss 					for ( ; ; ) {
    384   1.8  augustss 						int	len;
    385   1.8  augustss 						int	bot;
    386   1.8  augustss 						int	top;
    387   1.8  augustss 
    388  1.20   mlelstv 						len = isleap_sum(year, base) ?
    389   1.8  augustss 							DAYSPERLYEAR :
    390   1.8  augustss 							DAYSPERNYEAR;
    391   1.8  augustss 						/*
    392   1.8  augustss 						** What yday (-3 ... 3) does
    393   1.8  augustss 						** the ISO year begin on?
    394   1.8  augustss 						*/
    395   1.8  augustss 						bot = ((yday + 11 - wday) %
    396   1.8  augustss 							DAYSPERWEEK) - 3;
    397   1.8  augustss 						/*
    398   1.8  augustss 						** What yday does the NEXT
    399   1.8  augustss 						** ISO year begin on?
    400   1.8  augustss 						*/
    401   1.8  augustss 						top = bot -
    402   1.8  augustss 							(len % DAYSPERWEEK);
    403   1.8  augustss 						if (top < -3)
    404   1.8  augustss 							top += DAYSPERWEEK;
    405   1.8  augustss 						top += len;
    406   1.8  augustss 						if (yday >= top) {
    407  1.20   mlelstv 							++base;
    408   1.8  augustss 							w = 1;
    409   1.8  augustss 							break;
    410   1.8  augustss 						}
    411   1.8  augustss 						if (yday >= bot) {
    412   1.8  augustss 							w = 1 + ((yday - bot) /
    413   1.8  augustss 								DAYSPERWEEK);
    414   1.8  augustss 							break;
    415   1.8  augustss 						}
    416  1.20   mlelstv 						--base;
    417  1.20   mlelstv 						yday += isleap_sum(year, base) ?
    418   1.8  augustss 							DAYSPERLYEAR :
    419   1.8  augustss 							DAYSPERNYEAR;
    420   1.8  augustss 					}
    421   1.8  augustss #ifdef XPG4_1994_04_09
    422  1.20   mlelstv 					if ((w == 52 &&
    423  1.20   mlelstv 						t->tm_mon == TM_JANUARY) ||
    424  1.20   mlelstv 						(w == 1 &&
    425  1.20   mlelstv 						t->tm_mon == TM_DECEMBER))
    426  1.20   mlelstv 							w = 53;
    427   1.8  augustss #endif /* defined XPG4_1994_04_09 */
    428  1.12    kleink 					if (*format == 'V')
    429  1.12    kleink 						pt = _conv(w, "%02d",
    430  1.12    kleink 							pt, ptlim);
    431  1.12    kleink 					else if (*format == 'g') {
    432  1.12    kleink 						*warnp = IN_ALL;
    433  1.20   mlelstv 						pt = _yconv(year, base, 0, 1,
    434  1.12    kleink 							pt, ptlim);
    435  1.20   mlelstv 					} else	pt = _yconv(year, base, 1, 1,
    436  1.12    kleink 							pt, ptlim);
    437   1.1       mrg 				}
    438   1.1       mrg 				continue;
    439  1.12    kleink 			case 'v':
    440  1.12    kleink 				/*
    441  1.12    kleink 				** From Arnold Robbins' strftime version 3.0:
    442  1.12    kleink 				** "date as dd-bbb-YYYY"
    443  1.12    kleink 				** (ado, 1993-05-24)
    444  1.12    kleink 				*/
    445  1.25     joerg 				pt = _fmt(sp, "%e-%b-%Y", t, pt, ptlim, warnp,
    446  1.25     joerg 				    loc);
    447  1.12    kleink 				continue;
    448   1.1       mrg 			case 'W':
    449  1.12    kleink 				pt = _conv((t->tm_yday + DAYSPERWEEK -
    450  1.12    kleink 					(t->tm_wday ?
    451  1.12    kleink 					(t->tm_wday - 1) :
    452  1.12    kleink 					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
    453  1.12    kleink 					"%02d", pt, ptlim);
    454   1.1       mrg 				continue;
    455   1.1       mrg 			case 'w':
    456  1.12    kleink 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
    457  1.12    kleink 				continue;
    458  1.12    kleink 			case 'X':
    459  1.25     joerg 				pt = _fmt(sp, _TIME_LOCALE(loc)->t_fmt, t, pt,
    460  1.25     joerg 				    ptlim, warnp, loc);
    461   1.1       mrg 				continue;
    462   1.1       mrg 			case 'x':
    463  1.12    kleink 				{
    464  1.12    kleink 				int	warn2 = IN_SOME;
    465  1.12    kleink 
    466  1.25     joerg 				pt = _fmt(sp, _TIME_LOCALE(loc)->d_fmt, t, pt,
    467  1.25     joerg 				    ptlim, &warn2, loc);
    468  1.12    kleink 				if (warn2 == IN_ALL)
    469  1.12    kleink 					warn2 = IN_THIS;
    470  1.12    kleink 				if (warn2 > *warnp)
    471  1.12    kleink 					*warnp = warn2;
    472  1.12    kleink 				}
    473   1.1       mrg 				continue;
    474   1.1       mrg 			case 'y':
    475  1.12    kleink 				*warnp = IN_ALL;
    476  1.20   mlelstv 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
    477  1.20   mlelstv 					pt, ptlim);
    478   1.1       mrg 				continue;
    479   1.1       mrg 			case 'Y':
    480  1.20   mlelstv 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
    481  1.12    kleink 					pt, ptlim);
    482   1.1       mrg 				continue;
    483   1.1       mrg 			case 'Z':
    484  1.11      taca #ifdef TM_ZONE
    485  1.12    kleink 				if (t->TM_ZONE != NULL)
    486  1.12    kleink 					pt = _add(t->TM_ZONE, pt, ptlim);
    487  1.12    kleink 				else
    488  1.12    kleink #endif /* defined TM_ZONE */
    489  1.12    kleink 				if (t->tm_isdst >= 0)
    490  1.21  christos 					pt = _add((sp ?
    491  1.21  christos 					    tzgetname(sp, t->tm_isdst) :
    492  1.21  christos 					    tzname[t->tm_isdst != 0]),
    493  1.21  christos 					    pt, ptlim);
    494  1.12    kleink 				/*
    495  1.12    kleink 				** C99 says that %Z must be replaced by the
    496  1.12    kleink 				** empty string if the time zone is not
    497  1.12    kleink 				** determinable.
    498  1.12    kleink 				*/
    499  1.12    kleink 				continue;
    500  1.12    kleink 			case 'z':
    501  1.12    kleink 				{
    502  1.12    kleink 				int		diff;
    503  1.12    kleink 				char const *	sign;
    504  1.12    kleink 
    505  1.12    kleink 				if (t->tm_isdst < 0)
    506  1.12    kleink 					continue;
    507  1.12    kleink #ifdef TM_GMTOFF
    508  1.12    kleink 				diff = (int)t->TM_GMTOFF;
    509  1.12    kleink #else /* !defined TM_GMTOFF */
    510  1.12    kleink 				/*
    511  1.12    kleink 				** C99 says that the UTC offset must
    512  1.12    kleink 				** be computed by looking only at
    513  1.20   mlelstv 				** tm_isdst. This requirement is
    514  1.12    kleink 				** incorrect, since it means the code
    515  1.12    kleink 				** must rely on magic (in this case
    516  1.12    kleink 				** altzone and timezone), and the
    517  1.12    kleink 				** magic might not have the correct
    518  1.20   mlelstv 				** offset. Doing things correctly is
    519  1.12    kleink 				** tricky and requires disobeying C99;
    520  1.12    kleink 				** see GNU C strftime for details.
    521  1.12    kleink 				** For now, punt and conform to the
    522  1.12    kleink 				** standard, even though it's incorrect.
    523  1.12    kleink 				**
    524  1.12    kleink 				** C99 says that %z must be replaced by the
    525  1.12    kleink 				** empty string if the time zone is not
    526  1.12    kleink 				** determinable, so output nothing if the
    527  1.12    kleink 				** appropriate variables are not available.
    528  1.12    kleink 				*/
    529  1.16    kleink #ifndef STD_INSPIRED
    530  1.12    kleink 				if (t->tm_isdst == 0)
    531  1.12    kleink #ifdef USG_COMPAT
    532  1.12    kleink 					diff = -timezone;
    533  1.13    kleink #else /* !defined USG_COMPAT */
    534  1.12    kleink 					continue;
    535  1.12    kleink #endif /* !defined USG_COMPAT */
    536  1.12    kleink 				else
    537  1.12    kleink #ifdef ALTZONE
    538  1.12    kleink 					diff = -altzone;
    539  1.12    kleink #else /* !defined ALTZONE */
    540  1.12    kleink 					continue;
    541  1.12    kleink #endif /* !defined ALTZONE */
    542  1.16    kleink #else /* defined STD_INSPIRED */
    543  1.16    kleink 				{
    544  1.16    kleink 					struct tm tmp;
    545  1.16    kleink 					time_t lct, gct;
    546  1.16    kleink 
    547  1.16    kleink 					/*
    548  1.16    kleink 					** Get calendar time from t
    549  1.16    kleink 					** being treated as local.
    550  1.16    kleink 					*/
    551  1.16    kleink 					tmp = *t; /* mktime discards const */
    552  1.16    kleink 					lct = mktime(&tmp);
    553  1.16    kleink 
    554  1.16    kleink 					if (lct == (time_t)-1)
    555  1.16    kleink 						continue;
    556  1.16    kleink 
    557  1.16    kleink 					/*
    558  1.16    kleink 					** Get calendar time from t
    559  1.16    kleink 					** being treated as GMT.
    560  1.16    kleink 					**/
    561  1.16    kleink 					tmp = *t; /* mktime discards const */
    562  1.16    kleink 					gct = timegm(&tmp);
    563  1.16    kleink 
    564  1.16    kleink 					if (gct == (time_t)-1)
    565  1.16    kleink 						continue;
    566  1.16    kleink 
    567  1.16    kleink 					/* LINTED difference will fit int */
    568  1.16    kleink 					diff = (intmax_t)gct - (intmax_t)lct;
    569  1.16    kleink 				}
    570  1.16    kleink #endif /* defined STD_INSPIRED */
    571  1.12    kleink #endif /* !defined TM_GMTOFF */
    572  1.12    kleink 				if (diff < 0) {
    573  1.12    kleink 					sign = "-";
    574  1.12    kleink 					diff = -diff;
    575  1.12    kleink 				} else	sign = "+";
    576  1.12    kleink 				pt = _add(sign, pt, ptlim);
    577  1.20   mlelstv 				diff /= SECSPERMIN;
    578  1.20   mlelstv 				diff = (diff / MINSPERHOUR) * 100 +
    579  1.20   mlelstv 					(diff % MINSPERHOUR);
    580  1.20   mlelstv 				pt = _conv(diff, "%04d", pt, ptlim);
    581  1.12    kleink 				}
    582   1.1       mrg 				continue;
    583  1.12    kleink #if 0
    584  1.12    kleink 			case '+':
    585  1.25     joerg 				pt = _fmt(sp, _TIME_LOCALE(loc)->date_fmt, t,
    586  1.25     joerg 				    pt, ptlim, warnp, loc);
    587  1.12    kleink 				continue;
    588  1.12    kleink #endif
    589   1.1       mrg 			case '%':
    590   1.1       mrg 			/*
    591  1.12    kleink 			** X311J/88-090 (4.12.3.5): if conversion char is
    592  1.20   mlelstv 			** undefined, behavior is undefined. Print out the
    593  1.12    kleink 			** character itself as printf(3) also does.
    594  1.12    kleink 			*/
    595   1.1       mrg 			default:
    596   1.1       mrg 				break;
    597   1.1       mrg 			}
    598   1.1       mrg 		}
    599  1.12    kleink 		if (pt == ptlim)
    600  1.12    kleink 			break;
    601  1.12    kleink 		*pt++ = *format;
    602   1.1       mrg 	}
    603  1.12    kleink 	return pt;
    604   1.1       mrg }
    605   1.1       mrg 
    606  1.21  christos size_t
    607  1.21  christos strftime(char * const s, const size_t maxsize,
    608  1.21  christos     const char * const format, const struct tm * const	t)
    609  1.21  christos {
    610  1.21  christos 	tzset();
    611  1.21  christos 	return strftime_z(NULL, s, maxsize, format, t);
    612  1.21  christos }
    613  1.21  christos 
    614  1.25     joerg size_t
    615  1.25     joerg strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
    616  1.25     joerg     const struct tm * __restrict t, locale_t loc)
    617  1.25     joerg {
    618  1.25     joerg 	tzset();
    619  1.25     joerg 	return strftime_lz(NULL, s, maxsize, format, t, loc);
    620  1.25     joerg }
    621  1.25     joerg 
    622  1.12    kleink static char *
    623  1.23  christos _conv(const int	n, const char *const format, char *const pt,
    624  1.23  christos     const char *const ptlim)
    625   1.1       mrg {
    626  1.12    kleink 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
    627   1.1       mrg 
    628  1.19  christos 	(void) snprintf(buf, sizeof(buf), format, n);
    629  1.12    kleink 	return _add(buf, pt, ptlim);
    630   1.1       mrg }
    631   1.1       mrg 
    632  1.12    kleink static char *
    633  1.23  christos _add(const char *str, char *pt, const char *const ptlim)
    634   1.1       mrg {
    635  1.12    kleink 	while (pt < ptlim && (*pt = *str++) != '\0')
    636  1.12    kleink 		++pt;
    637  1.12    kleink 	return pt;
    638   1.1       mrg }
    639  1.20   mlelstv 
    640  1.20   mlelstv /*
    641  1.20   mlelstv ** POSIX and the C Standard are unclear or inconsistent about
    642  1.20   mlelstv ** what %C and %y do if the year is negative or exceeds 9999.
    643  1.20   mlelstv ** Use the convention that %C concatenated with %y yields the
    644  1.20   mlelstv ** same output as %Y, and that %Y contains at least 4 bytes,
    645  1.20   mlelstv ** with more only if necessary.
    646  1.20   mlelstv */
    647  1.20   mlelstv 
    648  1.20   mlelstv static char *
    649  1.22      matt _yconv(const int a, const int b, const int convert_top, const int convert_yy,
    650  1.23  christos     char *pt, const char *const ptlim)
    651  1.20   mlelstv {
    652  1.20   mlelstv 	register int	lead;
    653  1.20   mlelstv 	register int	trail;
    654  1.20   mlelstv 
    655  1.20   mlelstv #define DIVISOR	100
    656  1.20   mlelstv 	trail = a % DIVISOR + b % DIVISOR;
    657  1.20   mlelstv 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
    658  1.20   mlelstv 	trail %= DIVISOR;
    659  1.20   mlelstv 	if (trail < 0 && lead > 0) {
    660  1.20   mlelstv 		trail += DIVISOR;
    661  1.20   mlelstv 		--lead;
    662  1.20   mlelstv 	} else if (lead < 0 && trail > 0) {
    663  1.20   mlelstv 		trail -= DIVISOR;
    664  1.20   mlelstv 		++lead;
    665  1.20   mlelstv 	}
    666  1.20   mlelstv 	if (convert_top) {
    667  1.20   mlelstv 		if (lead == 0 && trail < 0)
    668  1.20   mlelstv 			pt = _add("-0", pt, ptlim);
    669  1.20   mlelstv 		else	pt = _conv(lead, "%02d", pt, ptlim);
    670  1.20   mlelstv 	}
    671  1.20   mlelstv 	if (convert_yy)
    672  1.20   mlelstv 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
    673  1.20   mlelstv 	return pt;
    674  1.20   mlelstv }
    675  1.20   mlelstv 
    676  1.20   mlelstv #ifdef LOCALE_HOME
    677  1.20   mlelstv static struct lc_time_T *
    678  1.20   mlelstv _loc(void)
    679  1.20   mlelstv {
    680  1.20   mlelstv 	static const char	locale_home[] = LOCALE_HOME;
    681  1.20   mlelstv 	static const char	lc_time[] = "LC_TIME";
    682  1.20   mlelstv 	static char *		locale_buf;
    683  1.20   mlelstv 
    684  1.20   mlelstv 	int			fd;
    685  1.20   mlelstv 	int			oldsun;	/* "...ain't got nothin' to do..." */
    686  1.20   mlelstv 	char *			lbuf;
    687  1.20   mlelstv 	char *			name;
    688  1.20   mlelstv 	char *			p;
    689  1.20   mlelstv 	const char **		ap;
    690  1.20   mlelstv 	const char *		plim;
    691  1.20   mlelstv 	char			filename[FILENAME_MAX];
    692  1.20   mlelstv 	struct stat		st;
    693  1.20   mlelstv 	size_t			namesize;
    694  1.20   mlelstv 	size_t			bufsize;
    695  1.20   mlelstv 
    696  1.20   mlelstv 	/*
    697  1.20   mlelstv 	** Use localebuf.mon[0] to signal whether locale is already set up.
    698  1.20   mlelstv 	*/
    699  1.20   mlelstv 	if (localebuf.mon[0])
    700  1.20   mlelstv 		return &localebuf;
    701  1.23  christos 	name = setlocale(LC_TIME, NULL);
    702  1.20   mlelstv 	if (name == NULL || *name == '\0')
    703  1.20   mlelstv 		goto no_locale;
    704  1.20   mlelstv 	/*
    705  1.20   mlelstv 	** If the locale name is the same as our cache, use the cache.
    706  1.20   mlelstv 	*/
    707  1.20   mlelstv 	lbuf = locale_buf;
    708  1.20   mlelstv 	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
    709  1.20   mlelstv 		p = lbuf;
    710  1.20   mlelstv 		for (ap = (const char **) &localebuf;
    711  1.20   mlelstv 			ap < (const char **) (&localebuf + 1);
    712  1.20   mlelstv 				++ap)
    713  1.20   mlelstv 					*ap = p += strlen(p) + 1;
    714  1.20   mlelstv 		return &localebuf;
    715  1.20   mlelstv 	}
    716  1.20   mlelstv 	/*
    717  1.20   mlelstv 	** Slurp the locale file into the cache.
    718  1.20   mlelstv 	*/
    719  1.20   mlelstv 	namesize = strlen(name) + 1;
    720  1.20   mlelstv 	if (sizeof filename <
    721  1.20   mlelstv 		((sizeof locale_home) + namesize + (sizeof lc_time)))
    722  1.20   mlelstv 			goto no_locale;
    723  1.20   mlelstv 	oldsun = 0;
    724  1.20   mlelstv 	(void) sprintf(filename, "%s/%s/%s", locale_home, name, lc_time);
    725  1.20   mlelstv 	fd = open(filename, O_RDONLY);
    726  1.20   mlelstv 	if (fd < 0) {
    727  1.20   mlelstv 		/*
    728  1.20   mlelstv 		** Old Sun systems have a different naming and data convention.
    729  1.20   mlelstv 		*/
    730  1.20   mlelstv 		oldsun = 1;
    731  1.20   mlelstv 		(void) sprintf(filename, "%s/%s/%s", locale_home,
    732  1.20   mlelstv 			lc_time, name);
    733  1.20   mlelstv 		fd = open(filename, O_RDONLY);
    734  1.20   mlelstv 		if (fd < 0)
    735  1.20   mlelstv 			goto no_locale;
    736  1.20   mlelstv 	}
    737  1.20   mlelstv 	if (fstat(fd, &st) != 0)
    738  1.20   mlelstv 		goto bad_locale;
    739  1.20   mlelstv 	if (st.st_size <= 0)
    740  1.20   mlelstv 		goto bad_locale;
    741  1.20   mlelstv 	bufsize = namesize + st.st_size;
    742  1.20   mlelstv 	locale_buf = NULL;
    743  1.20   mlelstv 	lbuf = (lbuf == NULL) ? malloc(bufsize) : realloc(lbuf, bufsize);
    744  1.20   mlelstv 	if (lbuf == NULL)
    745  1.20   mlelstv 		goto bad_locale;
    746  1.20   mlelstv 	(void) strcpy(lbuf, name);
    747  1.20   mlelstv 	p = lbuf + namesize;
    748  1.20   mlelstv 	plim = p + st.st_size;
    749  1.20   mlelstv 	if (read(fd, p, (size_t) st.st_size) != st.st_size)
    750  1.20   mlelstv 		goto bad_lbuf;
    751  1.20   mlelstv 	if (close(fd) != 0)
    752  1.20   mlelstv 		goto bad_lbuf;
    753  1.20   mlelstv 	/*
    754  1.20   mlelstv 	** Parse the locale file into localebuf.
    755  1.20   mlelstv 	*/
    756  1.20   mlelstv 	if (plim[-1] != '\n')
    757  1.20   mlelstv 		goto bad_lbuf;
    758  1.20   mlelstv 	for (ap = (const char **) &localebuf;
    759  1.20   mlelstv 		ap < (const char **) (&localebuf + 1);
    760  1.20   mlelstv 			++ap) {
    761  1.20   mlelstv 				if (p == plim)
    762  1.20   mlelstv 					goto bad_lbuf;
    763  1.20   mlelstv 				*ap = p;
    764  1.20   mlelstv 				while (*p != '\n')
    765  1.20   mlelstv 					++p;
    766  1.20   mlelstv 				*p++ = '\0';
    767  1.20   mlelstv 	}
    768  1.20   mlelstv 	if (oldsun) {
    769  1.20   mlelstv 		/*
    770  1.20   mlelstv 		** SunOS 4 used an obsolescent format; see localdtconv(3).
    771  1.20   mlelstv 		** c_fmt had the ``short format for dates and times together''
    772  1.20   mlelstv 		** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
    773  1.20   mlelstv 		** date_fmt had the ``long format for dates''
    774  1.20   mlelstv 		** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
    775  1.20   mlelstv 		** Discard the latter in favor of the former.
    776  1.20   mlelstv 		*/
    777  1.20   mlelstv 		localebuf.date_fmt = localebuf.c_fmt;
    778  1.20   mlelstv 	}
    779  1.20   mlelstv 	/*
    780  1.20   mlelstv 	** Record the successful parse in the cache.
    781  1.20   mlelstv 	*/
    782  1.20   mlelstv 	locale_buf = lbuf;
    783  1.20   mlelstv 
    784  1.20   mlelstv 	return &localebuf;
    785  1.20   mlelstv 
    786  1.20   mlelstv bad_lbuf:
    787  1.20   mlelstv 	free(lbuf);
    788  1.20   mlelstv bad_locale:
    789  1.20   mlelstv 	(void) close(fd);
    790  1.20   mlelstv no_locale:
    791  1.20   mlelstv 	localebuf = C_time_locale;
    792  1.20   mlelstv 	locale_buf = NULL;
    793  1.20   mlelstv 	return &localebuf;
    794  1.20   mlelstv }
    795  1.20   mlelstv #endif /* defined LOCALE_HOME */
    796