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