Home | History | Annotate | Line # | Download | only in time
      1 /*	$NetBSD: strftime.c,v 1.57 2025/01/23 22:44:22 christos Exp $	*/
      2 
      3 /* Convert a broken-down timestamp to a string.  */
      4 
      5 /* Copyright 1989 The Regents of the University of California.
      6    All rights reserved.
      7 
      8    Redistribution and use in source and binary forms, with or without
      9    modification, are permitted provided that the following conditions
     10    are met:
     11    1. Redistributions of source code must retain the above copyright
     12       notice, this list of conditions and the following disclaimer.
     13    2. Redistributions in binary form must reproduce the above copyright
     14       notice, this list of conditions and the following disclaimer in the
     15       documentation and/or other materials provided with the distribution.
     16    3. Neither the name of the University nor the names of its contributors
     17       may be used to endorse or promote products derived from this software
     18       without specific prior written permission.
     19 
     20    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
     21    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30    SUCH DAMAGE.  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char	elsieid[] = "@(#)strftime.c	7.64";
     36 static char	elsieid[] = "@(#)strftime.c	8.3";
     37 #else
     38 __RCSID("$NetBSD: strftime.c,v 1.57 2025/01/23 22:44:22 christos Exp $");
     39 #endif
     40 #endif /* LIBC_SCCS and not lint */
     41 
     42 #include "namespace.h"
     43 
     44 #include <stddef.h>
     45 #include <assert.h>
     46 #include <locale.h>
     47 #include "setlocale_local.h"
     48 
     49 /*
     50 ** Based on the UCB version with the copyright notice appearing above.
     51 **
     52 ** This is ANSIish only when "multibyte character == plain character".
     53 */
     54 
     55 #include "private.h"
     56 
     57 /*
     58 ** We don't use these extensions in strftime operation even when
     59 ** supported by the local tzcode configuration.  A strictly
     60 ** conforming C application may leave them in undefined state.
     61 */
     62 
     63 #ifdef _LIBC
     64 #undef TM_ZONE
     65 #undef TM_GMTOFF
     66 #endif
     67 
     68 #include <fcntl.h>
     69 #include <locale.h>
     70 #include <stdio.h>
     71 
     72 /* If true, the value returned by an idealized unlimited-range mktime
     73    always fits into an integer type with bounds MIN and MAX.
     74    If false, the value might not fit.
     75    This macro is usable in #if if its arguments are.
     76    Add or subtract 2**31 - 1 for the maximum UT offset allowed in a TZif file,
     77    divide by the maximum number of non-leap seconds in a year,
     78    divide again by two just to be safe,
     79    and account for the tm_year origin (1900) and time_t origin (1970).  */
     80 #define MKTIME_FITS_IN(min, max) \
     81   ((min) < 0 \
     82    && ((min) + 0x7fffffff) / 366 / 24 / 60 / 60 / 2 + 1970 - 1900 < INT_MIN \
     83    && INT_MAX < ((max) - 0x7fffffff) / 366 / 24 / 60 / 60 / 2 + 1970 - 1900)
     84 
     85 /* MKTIME_MIGHT_OVERFLOW is true if mktime can fail due to time_t overflow
     86    or if it is not known whether mktime can fail,
     87    and is false if mktime definitely cannot fail.
     88    This macro is usable in #if, and so does not use TIME_T_MAX or sizeof.
     89    If the builder has not configured this macro, guess based on what
     90    known platforms do.  When in doubt, guess true.  */
     91 #ifndef MKTIME_MIGHT_OVERFLOW
     92 # if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
     93 #  include <sys/param.h>
     94 # endif
     95 # if ((/* The following heuristics assume native time_t.  */ \
     96        defined_time_tz) \
     97       || ((/* Traditional time_t is 'long', so if 'long' is not wide enough \
     98 	      assume overflow unless we're on a known-safe host.  */ \
     99 	   !MKTIME_FITS_IN(LONG_MIN, LONG_MAX)) \
    100 	  && (/* GNU C Library 2.29 (2019-02-01) and later has 64-bit time_t \
    101 		 if __TIMESIZE is 64.  */ \
    102 	      !defined __TIMESIZE || __TIMESIZE < 64) \
    103 	  && (/* FreeBSD 12 r320347 (__FreeBSD_version 1200036; 2017-06-26), \
    104 		 and later has 64-bit time_t on all platforms but i386 which \
    105 		 is currently scheduled for end-of-life on 2028-11-30.  */ \
    106 	      !defined __FreeBSD_version || __FreeBSD_version < 1200036 \
    107 	      || defined __i386) \
    108 	  && (/* NetBSD 6.0 (2012-10-17) and later has 64-bit time_t.  */ \
    109 	      !defined __NetBSD_Version__ || __NetBSD_Version__ < 600000000) \
    110 	  && (/* OpenBSD 5.5 (2014-05-01) and later has 64-bit time_t.  */ \
    111 	      !defined OpenBSD || OpenBSD < 201405)))
    112 #  define MKTIME_MIGHT_OVERFLOW 1
    113 # else
    114 #  define MKTIME_MIGHT_OVERFLOW 0
    115 # endif
    116 #endif
    117 /* Check that MKTIME_MIGHT_OVERFLOW is consistent with time_t's range.  */
    118 static_assert(MKTIME_MIGHT_OVERFLOW
    119 	      || MKTIME_FITS_IN(TIME_T_MIN, TIME_T_MAX));
    120 
    121 #if MKTIME_MIGHT_OVERFLOW
    122 /* Do this after system includes as it redefines time_t, mktime, timeoff.  */
    123 # define USE_TIMEX_T true
    124 # include "localtime.c"
    125 #endif
    126 
    127 #ifndef DEPRECATE_TWO_DIGIT_YEARS
    128 # define DEPRECATE_TWO_DIGIT_YEARS 0
    129 #endif
    130 
    131 #ifdef __weak_alias
    132 __weak_alias(strftime_l, _strftime_l)
    133 __weak_alias(strftime_lz, _strftime_lz)
    134 __weak_alias(strftime_z, _strftime_z)
    135 #endif
    136 
    137 #include "sys/localedef.h"
    138 #define _TIME_LOCALE(loc) \
    139     ((_TimeLocale *)((loc)->part_impl[LC_TIME]))
    140 #define c_fmt   d_t_fmt
    141 
    142 enum warn { IN_NONE, IN_SOME, IN_THIS, IN_ALL };
    143 
    144 static char *	_add(const char *, char *, const char *);
    145 static char *	_conv(int, const char *, char *, const char *, locale_t);
    146 static char *	_fmt(const timezone_t, const char *, const struct tm *, char *,
    147 			const char *, enum warn *, locale_t);
    148 static char *	_yconv(int, int, bool, bool, char *, const char *, locale_t);
    149 
    150 #ifndef YEAR_2000_NAME
    151 # define YEAR_2000_NAME "CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
    152 #endif /* !defined YEAR_2000_NAME */
    153 
    154 #define	IN_NONE	0
    155 #define	IN_SOME	1
    156 #define	IN_THIS	2
    157 #define	IN_ALL	3
    158 
    159 #define	PAD_DEFAULT	0
    160 #define	PAD_LESS	1
    161 #define	PAD_SPACE	2
    162 #define	PAD_ZERO	3
    163 
    164 static const char fmt_padding[][4][5] = {
    165 	/* DEFAULT,	LESS,	SPACE,	ZERO */
    166 #define	PAD_FMT_MONTHDAY	0
    167 #define	PAD_FMT_HMS		0
    168 #define	PAD_FMT_CENTURY		0
    169 #define	PAD_FMT_SHORTYEAR	0
    170 #define	PAD_FMT_MONTH		0
    171 #define	PAD_FMT_WEEKOFYEAR	0
    172 #define	PAD_FMT_DAYOFMONTH	0
    173 	{ "%02d",	"%d",	"%2d",	"%02d" },
    174 #define	PAD_FMT_SDAYOFMONTH	1
    175 #define	PAD_FMT_SHMS		1
    176 	{ "%2d",	"%d",	"%2d",	"%02d" },
    177 #define	PAD_FMT_DAYOFYEAR	2
    178 	{ "%03d",	"%d",	"%3d",	"%03d" },
    179 #define	PAD_FMT_YEAR		3
    180 	{ "%04d",	"%d",	"%4d",	"%04d" }
    181 };
    182 
    183 size_t
    184 strftime_z(const timezone_t sp, char * __restrict s, size_t maxsize,
    185     const char * __restrict format, const struct tm * __restrict t)
    186 {
    187 	return strftime_lz(sp, s, maxsize, format, t, _current_locale());
    188 }
    189 
    190 #if HAVE_STRFTIME_L
    191 size_t
    192 strftime_l(char *restrict s, size_t maxsize, char const *restrict format,
    193 	   struct tm const *restrict t,
    194 	   ATTRIBUTE_MAYBE_UNUSED locale_t locale)
    195 {
    196   /* Just call strftime, as only the C locale is supported.  */
    197   return strftime(s, maxsize, format, t);
    198 }
    199 #endif
    200 
    201 size_t
    202 strftime_lz(const timezone_t sp, char *const s, const size_t maxsize,
    203     const char *const format, const struct tm *const t, locale_t loc)
    204 {
    205 	char *	p;
    206 	int saved_errno = errno;
    207 	enum warn warn = IN_NONE;
    208 
    209 	p = _fmt(sp, format, t, s, s + maxsize, &warn, loc);
    210 	if (/*CONSTCOND*/DEPRECATE_TWO_DIGIT_YEARS
    211 	    && warn != IN_NONE && getenv(YEAR_2000_NAME)) {
    212 		(void) fprintf(stderr, "\n");
    213 		(void) fprintf(stderr, "strftime format \"%s\" ", format);
    214 		(void) fprintf(stderr, "yields only two digits of years in ");
    215 		if (warn == IN_SOME)
    216 			(void) fprintf(stderr, "some locales");
    217 		else if (warn == IN_THIS)
    218 			(void) fprintf(stderr, "the current locale");
    219 		else	(void) fprintf(stderr, "all locales");
    220 		(void) fprintf(stderr, "\n");
    221 	}
    222 	if (p == s + maxsize) {
    223 		errno = ERANGE;
    224 		return 0;
    225 	}
    226 	*p = '\0';
    227 	errno = saved_errno;
    228 	return p - s;
    229 }
    230 
    231 static char *
    232 _fmt(const timezone_t sp, const char *format, const struct tm *t, char *pt,
    233      const char *ptlim, enum warn *warnp, locale_t loc)
    234 {
    235 	int Ealternative, Oalternative, PadIndex;
    236 	_TimeLocale *tptr = _TIME_LOCALE(loc);
    237 
    238 	for ( ; *format; ++format) {
    239 		if (*format == '%') {
    240 			Ealternative = 0;
    241 			Oalternative = 0;
    242 			PadIndex = PAD_DEFAULT;
    243 label:
    244 			switch (*++format) {
    245 			case '\0':
    246 				/* Output unknown conversion specifiers as-is,
    247 				   to aid debugging.  This includes '%' at
    248 				   format end.  This conforms to C23 section
    249 				   7.29.3.5 paragraph 6, which says behavior
    250 				   is undefined here.  */
    251 			default:
    252 				--format;
    253 				break;
    254 			case 'A':
    255 				pt = _add((t->tm_wday < 0 ||
    256 					t->tm_wday >= DAYSPERWEEK) ?
    257 					"?" : tptr->day[t->tm_wday],
    258 					pt, ptlim);
    259 				continue;
    260 			case 'a':
    261 				pt = _add((t->tm_wday < 0 ||
    262 					t->tm_wday >= DAYSPERWEEK) ?
    263 					"?" : tptr->abday[t->tm_wday],
    264 					pt, ptlim);
    265 				continue;
    266 			case 'B':
    267 				pt = _add((t->tm_mon < 0 ||
    268 					t->tm_mon >= MONSPERYEAR) ?
    269 					"?" :
    270 					/* no alt_month in _TimeLocale */
    271 					(Oalternative ? tptr->mon/*alt_month*/:
    272 					tptr->mon)[t->tm_mon],
    273 					pt, ptlim);
    274 				continue;
    275 			case 'b':
    276 			case 'h':
    277 				pt = _add((t->tm_mon < 0 ||
    278 					t->tm_mon >= MONSPERYEAR) ?
    279 					"?" : tptr->abmon[t->tm_mon],
    280 					pt, ptlim);
    281 				continue;
    282 			case 'C':
    283 				/*
    284 				** %C used to do a...
    285 				**	_fmt("%a %b %e %X %Y", t);
    286 				** ...whereas now POSIX 1003.2 calls for
    287 				** something completely different.
    288 				** (ado, 1993-05-24)
    289 				*/
    290 				pt = _yconv(t->tm_year, TM_YEAR_BASE,
    291 					    true, false, pt, ptlim, loc);
    292 				continue;
    293 			case 'c':
    294 				{
    295 				enum warn warn2 = IN_SOME;
    296 
    297 				pt = _fmt(sp, tptr->c_fmt, t, pt,
    298 				    ptlim, &warn2, loc);
    299 				if (warn2 == IN_ALL)
    300 					warn2 = IN_THIS;
    301 				if (warn2 > *warnp)
    302 					*warnp = warn2;
    303 				}
    304 				continue;
    305 			case 'D':
    306 				pt = _fmt(sp, "%m/%d/%y", t, pt, ptlim, warnp,
    307 				    loc);
    308 				continue;
    309 			case 'd':
    310 				pt = _conv(t->tm_mday,
    311 				    fmt_padding[PAD_FMT_DAYOFMONTH][PadIndex],
    312 				    pt, ptlim, loc);
    313 				continue;
    314 			case 'E':
    315 				if (Ealternative || Oalternative)
    316 					break;
    317 				Ealternative++;
    318 				goto label;
    319 			case 'O':
    320 				/*
    321 				** Locale modifiers of C99 and later.
    322 				** The sequences
    323 				**	%Ec %EC %Ex %EX %Ey %EY
    324 				**	%Od %oe %OH %OI %Om %OM
    325 				**	%OS %Ou %OU %OV %Ow %OW %Oy
    326 				** are supposed to provide alternative
    327 				** representations.
    328 				*/
    329 				if (Ealternative || Oalternative)
    330 					break;
    331 				Oalternative++;
    332 				goto label;
    333 			case 'e':
    334 				pt = _conv(t->tm_mday,
    335 				    fmt_padding[PAD_FMT_SDAYOFMONTH][PadIndex],
    336 				    pt, ptlim, loc);
    337 				continue;
    338 			case 'F':
    339 				pt = _fmt(sp, "%Y-%m-%d", t, pt, ptlim, warnp,
    340 				    loc);
    341 				continue;
    342 			case 'H':
    343 				pt = _conv(t->tm_hour,
    344 				    fmt_padding[PAD_FMT_HMS][PadIndex],
    345 				    pt, ptlim, loc);
    346 				continue;
    347 			case 'I':
    348 				pt = _conv((t->tm_hour % 12) ?
    349 				    (t->tm_hour % 12) : 12,
    350 				    fmt_padding[PAD_FMT_HMS][PadIndex],
    351 				    pt, ptlim, loc);
    352 				continue;
    353 			case 'j':
    354 				pt = _conv(t->tm_yday + 1,
    355 				    fmt_padding[PAD_FMT_DAYOFYEAR][PadIndex],
    356 				    pt, ptlim, loc);
    357 				continue;
    358 			case 'k':
    359 				/*
    360 				** This used to be...
    361 				**	_conv(t->tm_hour % 12 ?
    362 				**		t->tm_hour % 12 : 12, 2, ' ');
    363 				** ...and has been changed to the below to
    364 				** match SunOS 4.1.1 and Arnold Robbins'
    365 				** strftime version 3.0. That is, "%k" and
    366 				** "%l" have been swapped.
    367 				** (ado, 1993-05-24)
    368 				*/
    369 				pt = _conv(t->tm_hour,
    370 				    fmt_padding[PAD_FMT_SHMS][PadIndex],
    371 				    pt, ptlim, loc);
    372 				continue;
    373 #ifdef KITCHEN_SINK
    374 			case 'K':
    375 				/*
    376 				** After all this time, still unclaimed!
    377 				*/
    378 				pt = _add("kitchen sink", pt, ptlim);
    379 				continue;
    380 #endif /* defined KITCHEN_SINK */
    381 			case 'l':
    382 				/*
    383 				** This used to be...
    384 				**	_conv(t->tm_hour, 2, ' ');
    385 				** ...and has been changed to the below to
    386 				** match SunOS 4.1.1 and Arnold Robbin's
    387 				** strftime version 3.0. That is, "%k" and
    388 				** "%l" have been swapped.
    389 				** (ado, 1993-05-24)
    390 				*/
    391 				pt = _conv((t->tm_hour % 12) ?
    392 					(t->tm_hour % 12) : 12,
    393 					fmt_padding[PAD_FMT_SHMS][PadIndex],
    394 					pt, ptlim, loc);
    395 				continue;
    396 			case 'M':
    397 				pt = _conv(t->tm_min,
    398 				    fmt_padding[PAD_FMT_HMS][PadIndex],
    399 				    pt, ptlim, loc);
    400 				continue;
    401 			case 'm':
    402 				pt = _conv(t->tm_mon + 1,
    403 				    fmt_padding[PAD_FMT_MONTH][PadIndex],
    404 				    pt, ptlim, loc);
    405 				continue;
    406 			case 'n':
    407 				pt = _add("\n", pt, ptlim);
    408 				continue;
    409 			case 'p':
    410 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
    411 					tptr->am_pm[1] :
    412 					tptr->am_pm[0],
    413 					pt, ptlim);
    414 				continue;
    415 			case 'R':
    416 				pt = _fmt(sp, "%H:%M", t, pt, ptlim, warnp,
    417 				    loc);
    418 				continue;
    419 			case 'r':
    420 				pt = _fmt(sp, tptr->t_fmt_ampm, t,
    421 				    pt, ptlim, warnp, loc);
    422 				continue;
    423 			case 'S':
    424 				pt = _conv(t->tm_sec,
    425 				    fmt_padding[PAD_FMT_HMS][PadIndex],
    426 				    pt, ptlim, loc);
    427 				continue;
    428 			case 's':
    429 				{
    430 					struct tm	tm;
    431 					char		buf[INT_STRLEN_MAXIMUM(
    432 								time_t) + 1];
    433 					time_t		mkt;
    434 
    435 					tm.tm_sec = t->tm_sec;
    436 					tm.tm_min = t->tm_min;
    437 					tm.tm_hour = t->tm_hour;
    438 					tm.tm_mday = t->tm_mday;
    439 					tm.tm_mon = t->tm_mon;
    440 					tm.tm_year = t->tm_year;
    441 
    442 					/* Get the time_t value for TM.
    443 					   Native time_t, or its redefinition
    444 					   by localtime.c above, is wide enough
    445 					   so that this cannot overflow.  */
    446 #if defined TM_GMTOFF
    447 					mkt = timeoff(&tm, t->TM_GMTOFF);
    448 #else
    449 					tm.tm_isdst = t->tm_isdst;
    450 					mkt = mktime_z(sp, &tm);
    451 #endif
    452 					/* CONSTCOND */
    453 					if (TYPE_SIGNED(time_t)) {
    454 						intmax_t n = mkt;
    455 						(void)snprintf(buf, sizeof(buf),
    456 						    "%"PRIdMAX, n);
    457 					} else {
    458 						/*LINTED possibly unreached*/
    459 						uintmax_t n = mkt;
    460 						(void)snprintf(buf, sizeof(buf),
    461 						    "%"PRIuMAX, n);
    462 					}
    463 					pt = _add(buf, pt, ptlim);
    464 				}
    465 				continue;
    466 			case 'T':
    467 				pt = _fmt(sp, "%H:%M:%S", t, pt, ptlim, warnp,
    468 				    loc);
    469 				continue;
    470 			case 't':
    471 				pt = _add("\t", pt, ptlim);
    472 				continue;
    473 			case 'U':
    474 				pt = _conv((t->tm_yday + DAYSPERWEEK -
    475 				    t->tm_wday) / DAYSPERWEEK,
    476 				    fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
    477 				    pt, ptlim, loc);
    478 				continue;
    479 			case 'u':
    480 				/*
    481 				** From Arnold Robbins' strftime version 3.0:
    482 				** "ISO 8601: Weekday as a decimal number
    483 				** [1 (Monday) - 7]"
    484 				** (ado, 1993-05-24)
    485 				*/
    486 				pt = _conv((t->tm_wday == 0) ?
    487 					DAYSPERWEEK : t->tm_wday,
    488 					"%d", pt, ptlim, loc);
    489 				continue;
    490 			case 'V':	/* ISO 8601 week number */
    491 			case 'G':	/* ISO 8601 year (four digits) */
    492 			case 'g':	/* ISO 8601 year (two digits) */
    493 /*
    494 ** From Arnold Robbins' strftime version 3.0: "the week number of the
    495 ** year (the first Monday as the first day of week 1) as a decimal number
    496 ** (01-53)."
    497 ** (ado, 1993-05-24)
    498 **
    499 ** From <https://www.cl.cam.ac.uk/~mgk25/iso-time.html> by Markus Kuhn:
    500 ** "Week 01 of a year is per definition the first week which has the
    501 ** Thursday in this year, which is equivalent to the week which contains
    502 ** the fourth day of January. In other words, the first week of a new year
    503 ** is the week which has the majority of its days in the new year. Week 01
    504 ** might also contain days from the previous year and the week before week
    505 ** 01 of a year is the last week (52 or 53) of the previous year even if
    506 ** it contains days from the new year. A week starts with Monday (day 1)
    507 ** and ends with Sunday (day 7). For example, the first week of the year
    508 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
    509 ** (ado, 1996-01-02)
    510 */
    511 				{
    512 					int	year;
    513 					int	base;
    514 					int	yday;
    515 					int	wday;
    516 					int	w;
    517 
    518 					year = t->tm_year;
    519 					base = TM_YEAR_BASE;
    520 					yday = t->tm_yday;
    521 					wday = t->tm_wday;
    522 					for ( ; ; ) {
    523 						int	len;
    524 						int	bot;
    525 						int	top;
    526 
    527 						len = isleap_sum(year, base) ?
    528 							DAYSPERLYEAR :
    529 							DAYSPERNYEAR;
    530 						/*
    531 						** What yday (-3 ... 3) does
    532 						** the ISO year begin on?
    533 						*/
    534 						bot = ((yday + 11 - wday) %
    535 							DAYSPERWEEK) - 3;
    536 						/*
    537 						** What yday does the NEXT
    538 						** ISO year begin on?
    539 						*/
    540 						top = bot -
    541 							(len % DAYSPERWEEK);
    542 						if (top < -3)
    543 							top += DAYSPERWEEK;
    544 						top += len;
    545 						if (yday >= top) {
    546 							++base;
    547 							w = 1;
    548 							break;
    549 						}
    550 						if (yday >= bot) {
    551 							w = 1 + ((yday - bot) /
    552 								DAYSPERWEEK);
    553 							break;
    554 						}
    555 						--base;
    556 						yday += isleap_sum(year, base) ?
    557 							DAYSPERLYEAR :
    558 							DAYSPERNYEAR;
    559 					}
    560 #ifdef XPG4_1994_04_09
    561 					if ((w == 52 &&
    562 						t->tm_mon == TM_JANUARY) ||
    563 						(w == 1 &&
    564 						t->tm_mon == TM_DECEMBER))
    565 							w = 53;
    566 #endif /* defined XPG4_1994_04_09 */
    567 					if (*format == 'V')
    568 						pt = _conv(w,
    569 						    fmt_padding[
    570 						    PAD_FMT_WEEKOFYEAR][
    571 						    PadIndex], pt, ptlim, loc);
    572 					else if (*format == 'g') {
    573 						*warnp = IN_ALL;
    574 						pt = _yconv(year, base,
    575 							false, true,
    576 							pt, ptlim, loc);
    577 					} else	pt = _yconv(year, base,
    578 							true, true,
    579 							pt, ptlim, loc);
    580 				}
    581 				continue;
    582 			case 'v':
    583 				/*
    584 				** From Arnold Robbins' strftime version 3.0:
    585 				** "date as dd-bbb-YYYY"
    586 				** (ado, 1993-05-24)
    587 				*/
    588 				pt = _fmt(sp, "%e-%b-%Y", t, pt, ptlim, warnp,
    589 				    loc);
    590 				continue;
    591 			case 'W':
    592 				pt = _conv((t->tm_yday + DAYSPERWEEK -
    593 				    (t->tm_wday ?
    594 				    (t->tm_wday - 1) :
    595 				    (DAYSPERWEEK - 1))) / DAYSPERWEEK,
    596 				    fmt_padding[PAD_FMT_WEEKOFYEAR][PadIndex],
    597 				    pt, ptlim, loc);
    598 				continue;
    599 			case 'w':
    600 				pt = _conv(t->tm_wday, "%d", pt, ptlim, loc);
    601 				continue;
    602 			case 'X':
    603 				pt = _fmt(sp, tptr->t_fmt, t, pt,
    604 				    ptlim, warnp, loc);
    605 				continue;
    606 			case 'x':
    607 				{
    608 				enum warn warn2 = IN_SOME;
    609 
    610 				pt = _fmt(sp, tptr->d_fmt, t, pt,
    611 				    ptlim, &warn2, loc);
    612 				if (warn2 == IN_ALL)
    613 					warn2 = IN_THIS;
    614 				if (warn2 > *warnp)
    615 					*warnp = warn2;
    616 				}
    617 				continue;
    618 			case 'y':
    619 				*warnp = IN_ALL;
    620 				pt = _yconv(t->tm_year, TM_YEAR_BASE,
    621 					false, true,
    622 					pt, ptlim, loc);
    623 				continue;
    624 			case 'Y':
    625 				pt = _yconv(t->tm_year, TM_YEAR_BASE,
    626 					true, true,
    627 					pt, ptlim, loc);
    628 				continue;
    629 			case 'Z':
    630 #ifdef TM_ZONE
    631 				pt = _add(t->TM_ZONE, pt, ptlim);
    632 #elif HAVE_TZNAME
    633 				if (t->tm_isdst >= 0) {
    634 					int oerrno = errno, dst = t->tm_isdst;
    635 					const char *z =
    636 					    tzgetname(sp, dst);
    637 					if (z == NULL)
    638 						z = tzgetname(sp, !dst);
    639 					if (z != NULL)
    640 						pt = _add(z, pt, ptlim);
    641 					errno = oerrno;
    642 				}
    643 #endif
    644 				/*
    645 				** C99 and later say that %Z must be
    646 				** replaced by the empty string if the
    647 				** time zone abbreviation is not
    648 				** determinable.
    649 				*/
    650 				continue;
    651 			case 'z':
    652 #if defined TM_GMTOFF || USG_COMPAT || ALTZONE
    653 				{
    654 				long		diff;
    655 				char const *	sign;
    656 				bool negative;
    657 
    658 				if (t->tm_isdst < 0)
    659 					continue;
    660 # ifdef TM_GMTOFF
    661 				diff = (int)t->TM_GMTOFF;
    662 # else
    663 				/*
    664 				** C99 and later say that the UT offset must
    665 				** be computed by looking only at
    666 				** tm_isdst. This requirement is
    667 				** incorrect, since it means the code
    668 				** must rely on magic (in this case
    669 				** altzone and timezone), and the
    670 				** magic might not have the correct
    671 				** offset. Doing things correctly is
    672 				** tricky and requires disobeying the standard;
    673 				** see GNU C strftime for details.
    674 				** For now, punt and conform to the
    675 				** standard, even though it's incorrect.
    676 				**
    677 				** C99 and later say that %z must be replaced by
    678 				** the empty string if the time zone is not
    679 				** determinable, so output nothing if the
    680 				** appropriate variables are not available.
    681 				*/
    682 #  ifndef STD_INSPIRED
    683 				if (t->tm_isdst == 0)
    684 #   if USG_COMPAT
    685 					diff = -timezone;
    686 #   else
    687 					continue;
    688 #   endif
    689 				else
    690 #   if ALTZONE
    691 					diff = -altzone;
    692 #   else
    693 					continue;
    694 #   endif
    695 #  else
    696 				{
    697 					struct tm tmp;
    698 					time_t lct, gct;
    699 
    700 					/*
    701 					** Get calendar time from t
    702 					** being treated as local.
    703 					*/
    704 					tmp = *t; /* mktime discards const */
    705 					lct = mktime_z(sp, &tmp);
    706 
    707 					if (lct == (time_t)-1)
    708 						continue;
    709 
    710 					/*
    711 					** Get calendar time from t
    712 					** being treated as GMT.
    713 					**/
    714 					tmp = *t; /* mktime discards const */
    715 					gct = timegm(&tmp);
    716 
    717 					if (gct == (time_t)-1)
    718 						continue;
    719 
    720 					/* LINTED difference will fit int */
    721 					diff = (intmax_t)gct - (intmax_t)lct;
    722 				}
    723 #  endif
    724 # endif
    725 				negative = diff < 0;
    726 				if (diff == 0) {
    727 # ifdef TM_ZONE
    728 				  negative = t->TM_ZONE[0] == '-';
    729 # else
    730 				  negative = t->tm_isdst < 0;
    731 #  if HAVE_TZNAME
    732 				  if (tzname[t->tm_isdst != 0][0] == '-')
    733 				    negative = true;
    734 #  endif
    735 # endif
    736 				}
    737 				if (negative) {
    738 					sign = "-";
    739 					diff = -diff;
    740 				} else	sign = "+";
    741 				pt = _add(sign, pt, ptlim);
    742 				diff /= SECSPERMIN;
    743 				diff = (diff / MINSPERHOUR) * 100 +
    744 					(diff % MINSPERHOUR);
    745 				_DIAGASSERT(__type_fit(int, diff));
    746 				pt = _conv((int)diff,
    747 				    fmt_padding[PAD_FMT_YEAR][PadIndex],
    748 				    pt, ptlim, loc);
    749 				}
    750 #endif
    751 				continue;
    752 			case '+':
    753 #ifdef notyet
    754 				/* XXX: no date_fmt in _TimeLocale */
    755 				pt = _fmt(sp, tptr->date_fmt, t,
    756 				    pt, ptlim, warnp, loc);
    757 #else
    758 				pt = _fmt(sp, "%a %b %e %H:%M:%S %Z %Y", t,
    759 				    pt, ptlim, warnp, loc);
    760 #endif
    761 				continue;
    762 			case '-':
    763 				if (PadIndex != PAD_DEFAULT)
    764 					break;
    765 				PadIndex = PAD_LESS;
    766 				goto label;
    767 			case '_':
    768 				if (PadIndex != PAD_DEFAULT)
    769 					break;
    770 				PadIndex = PAD_SPACE;
    771 				goto label;
    772 			case '0':
    773 				if (PadIndex != PAD_DEFAULT)
    774 					break;
    775 				PadIndex = PAD_ZERO;
    776 				goto label;
    777 			case '%':
    778 				break;
    779 			}
    780 		}
    781 		if (pt == ptlim)
    782 			break;
    783 		*pt++ = *format;
    784 	}
    785 	return pt;
    786 }
    787 
    788 size_t
    789 strftime(char *restrict s, size_t maxsize, char const *restrict format,
    790 	 struct tm const *restrict t)
    791 {
    792 	size_t r;
    793 
    794 	rwlock_wrlock(&__lcl_lock);
    795 	tzset_unlocked();
    796 	r = strftime_z(__lclptr, s, maxsize, format, t);
    797 	rwlock_unlock(&__lcl_lock);
    798 
    799 	return r;
    800 }
    801 
    802 size_t
    803 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
    804     const struct tm * __restrict t, locale_t loc)
    805 {
    806 	size_t r;
    807 
    808 	rwlock_wrlock(&__lcl_lock);
    809 	tzset_unlocked();
    810 	r = strftime_lz(__lclptr, s, maxsize, format, t, loc);
    811 	rwlock_unlock(&__lcl_lock);
    812 
    813 	return r;
    814 }
    815 
    816 static char *
    817 _conv(int n, const char *format, char *pt, const char *ptlim, locale_t loc)
    818 {
    819 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
    820 
    821 	(void) snprintf_l(buf, sizeof(buf), loc, format, n);
    822 	return _add(buf, pt, ptlim);
    823 }
    824 
    825 static char *
    826 _add(const char *str, char *pt, const char *ptlim)
    827 {
    828 	while (pt < ptlim && (*pt = *str++) != '\0')
    829 		++pt;
    830 	return pt;
    831 }
    832 
    833 /*
    834 ** POSIX and the C Standard are unclear or inconsistent about
    835 ** what %C and %y do if the year is negative or exceeds 9999.
    836 ** Use the convention that %C concatenated with %y yields the
    837 ** same output as %Y, and that %Y contains at least 4 bytes,
    838 ** with more only if necessary.
    839 */
    840 
    841 static char *
    842 _yconv(int a, int b, bool convert_top, bool convert_yy,
    843     char *pt, const char * ptlim, locale_t loc)
    844 {
    845 	int	lead;
    846 	int	trail;
    847 
    848 	int DIVISOR = 100;
    849 	trail = a % DIVISOR + b % DIVISOR;
    850 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
    851 	trail %= DIVISOR;
    852 	if (trail < 0 && lead > 0) {
    853 		trail += DIVISOR;
    854 		--lead;
    855 	} else if (lead < 0 && trail > 0) {
    856 		trail -= DIVISOR;
    857 		++lead;
    858 	}
    859 	if (convert_top) {
    860 		if (lead == 0 && trail < 0)
    861 			pt = _add("-0", pt, ptlim);
    862 		else	pt = _conv(lead, "%02d", pt, ptlim, loc);
    863 	}
    864 	if (convert_yy)
    865 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim,
    866 		    loc);
    867 	return pt;
    868 }
    869