Home | History | Annotate | Line # | Download | only in time
strftime.c revision 1.11
      1 /*	$NetBSD: strftime.c,v 1.11 2000/09/07 12:45:03 taca Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char *sccsid = "@(#)strftime.c	5.11 (Berkeley) 2/24/91";
     40 #else
     41 __RCSID("$NetBSD: strftime.c,v 1.11 2000/09/07 12:45:03 taca Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include "namespace.h"
     46 #include "private.h"
     47 #include <sys/localedef.h>
     48 #include <locale.h>
     49 #include <string.h>
     50 #include <tzfile.h>
     51 #include <time.h>
     52 
     53 static	int _add __P((const char *, char **, const char *));
     54 static	int _conv __P((int, int, int, char **, const char *));
     55 static	int _secs __P((const struct tm *, char **, const char *));
     56 static	size_t _fmt __P((const char *, const struct tm *, char **,
     57 	    const char *));
     58 
     59 size_t
     60 strftime(s, maxsize, format, t)
     61 	char *s;
     62 	size_t maxsize;
     63 	const char *format;
     64 	const struct tm *t;
     65 {
     66 	char *pt;
     67 
     68 	tzset();
     69 	if (maxsize < 1)
     70 		return (0);
     71 
     72 	pt = s;
     73 	if (_fmt(format, t, &pt, s + maxsize)) {
     74 		*pt = '\0';
     75 		return (pt - s);
     76 	} else
     77 		return (0);
     78 }
     79 
     80 #define SUN_WEEK(t)	(((t)->tm_yday + 7 - \
     81 				((t)->tm_wday)) / 7)
     82 #define MON_WEEK(t)	(((t)->tm_yday + 7 - \
     83 				((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
     84 
     85 static size_t
     86 _fmt(format, t, pt, ptlim)
     87 	const char *format;
     88 	const struct tm *t;
     89 	char **pt;
     90 	const char * const ptlim;
     91 {
     92 	for (; *format; ++format) {
     93 		if (*format == '%') {
     94 			++format;
     95 			if (*format == 'E') {
     96 				/* Alternate Era */
     97 				++format;
     98 			} else if (*format == 'O') {
     99 				/* Alternate numeric symbols */
    100 				++format;
    101 			}
    102 			switch (*format) {
    103 			case '\0':
    104 				--format;
    105 				break;
    106 			case 'A':
    107 				if (t->tm_wday < 0 || t->tm_wday > 6)
    108 					return (0);
    109 				if (!_add(_CurrentTimeLocale->day[t->tm_wday],
    110 				    pt, ptlim))
    111 					return (0);
    112 				continue;
    113 
    114 			case 'a':
    115 				if (t->tm_wday < 0 || t->tm_wday > 6)
    116 					return (0);
    117 				if (!_add(_CurrentTimeLocale->abday[t->tm_wday],
    118 				    pt, ptlim))
    119 					return (0);
    120 				continue;
    121 			case 'B':
    122 				if (t->tm_mon < 0 || t->tm_mon > 11)
    123 					return (0);
    124 				if (!_add(_CurrentTimeLocale->mon[t->tm_mon],
    125 				    pt, ptlim))
    126 					return (0);
    127 				continue;
    128 			case 'b':
    129 			case 'h':
    130 				if (t->tm_mon < 0 || t->tm_mon > 11)
    131 					return (0);
    132 				if (!_add(_CurrentTimeLocale->abmon[t->tm_mon],
    133 				    pt, ptlim))
    134 					return (0);
    135 				continue;
    136 			case 'C':
    137 				if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
    138 				    2, '0', pt, ptlim))
    139 					return (0);
    140 				continue;
    141 			case 'c':
    142 				if (!_fmt(_CurrentTimeLocale->d_t_fmt, t, pt,
    143 				    ptlim))
    144 					return (0);
    145 				continue;
    146 			case 'D':
    147 				if (!_fmt("%m/%d/%y", t, pt, ptlim))
    148 					return (0);
    149 				continue;
    150 			case 'd':
    151 				if (!_conv(t->tm_mday, 2, '0', pt, ptlim))
    152 					return (0);
    153 				continue;
    154 			case 'e':
    155 				if (!_conv(t->tm_mday, 2, ' ', pt, ptlim))
    156 					return (0);
    157 				continue;
    158 			case 'F':
    159 				if (!_fmt("%Y-%m-%d", t, pt, ptlim))
    160 					return (0);
    161 				continue;
    162 			case 'H':
    163 				if (!_conv(t->tm_hour, 2, '0', pt, ptlim))
    164 					return (0);
    165 				continue;
    166 			case 'I':
    167 				if (!_conv(t->tm_hour % 12 ?
    168 				    t->tm_hour % 12 : 12, 2, '0', pt, ptlim))
    169 					return (0);
    170 				continue;
    171 			case 'j':
    172 				if (!_conv(t->tm_yday + 1, 3, '0', pt, ptlim))
    173 					return (0);
    174 				continue;
    175 			case 'k':
    176 				if (!_conv(t->tm_hour, 2, ' ', pt, ptlim))
    177 					return (0);
    178 				continue;
    179 			case 'l':
    180 				if (!_conv(t->tm_hour % 12 ?
    181 				    t->tm_hour % 12: 12, 2, ' ', pt, ptlim))
    182 					return (0);
    183 				continue;
    184 			case 'M':
    185 				if (!_conv(t->tm_min, 2, '0', pt, ptlim))
    186 					return (0);
    187 				continue;
    188 			case 'm':
    189 				if (!_conv(t->tm_mon + 1, 2, '0', pt, ptlim))
    190 					return (0);
    191 				continue;
    192 			case 'n':
    193 				if (!_add("\n", pt, ptlim))
    194 					return (0);
    195 				continue;
    196 			case 'p':
    197 				if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour
    198 				    >= 12], pt, ptlim))
    199 					return (0);
    200 				continue;
    201 			case 'R':
    202 				if (!_fmt("%H:%M", t, pt, ptlim))
    203 					return (0);
    204 				continue;
    205 			case 'r':
    206 				if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t, pt,
    207 				    ptlim))
    208 					return (0);
    209 				continue;
    210 			case 'S':
    211 				if (!_conv(t->tm_sec, 2, '0', pt, ptlim))
    212 					return (0);
    213 				continue;
    214 			case 's':
    215 				if (!_secs(t, pt, ptlim))
    216 					return (0);
    217 				continue;
    218 			case 'T':
    219 				if (!_fmt("%H:%M:%S", t, pt, ptlim))
    220 					return (0);
    221 				continue;
    222 			case 't':
    223 				if (!_add("\t", pt, ptlim))
    224 					return (0);
    225 				continue;
    226 			case 'U':
    227 				if (!_conv(SUN_WEEK(t), 2, '0', pt, ptlim))
    228 					return (0);
    229 				continue;
    230 			case 'u':
    231 				if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0',
    232 				    pt, ptlim))
    233 					return (0);
    234 				continue;
    235 			case 'V':	/* ISO 8601 week number */
    236 			case 'G':	/* ISO 8601 year (four digits) */
    237 			case 'g':	/* ISO 8601 year (two digits) */
    238 /*
    239 ** From Arnold Robbins' strftime version 3.0:  "the week number of the
    240 ** year (the first Monday as the first day of week 1) as a decimal number
    241 ** (01-53)."
    242 ** (ado, 1993-05-24)
    243 **
    244 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
    245 ** "Week 01 of a year is per definition the first week which has the
    246 ** Thursday in this year, which is equivalent to the week which contains
    247 ** the fourth day of January. In other words, the first week of a new year
    248 ** is the week which has the majority of its days in the new year. Week 01
    249 ** might also contain days from the previous year and the week before week
    250 ** 01 of a year is the last week (52 or 53) of the previous year even if
    251 ** it contains days from the new year. A week starts with Monday (day 1)
    252 ** and ends with Sunday (day 7).  For example, the first week of the year
    253 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
    254 ** (ado, 1996-01-02)
    255 */
    256 				{
    257 					int	year;
    258 					int	yday;
    259 					int	wday;
    260 					int	w;
    261 
    262 					year = t->tm_year + TM_YEAR_BASE;
    263 					yday = t->tm_yday;
    264 					wday = t->tm_wday;
    265 					for ( ; ; ) {
    266 						int	len;
    267 						int	bot;
    268 						int	top;
    269 
    270 						len = isleap(year) ?
    271 							DAYSPERLYEAR :
    272 							DAYSPERNYEAR;
    273 						/*
    274 						** What yday (-3 ... 3) does
    275 						** the ISO year begin on?
    276 						*/
    277 						bot = ((yday + 11 - wday) %
    278 							DAYSPERWEEK) - 3;
    279 						/*
    280 						** What yday does the NEXT
    281 						** ISO year begin on?
    282 						*/
    283 						top = bot -
    284 							(len % DAYSPERWEEK);
    285 						if (top < -3)
    286 							top += DAYSPERWEEK;
    287 						top += len;
    288 						if (yday >= top) {
    289 							++year;
    290 							w = 1;
    291 							break;
    292 						}
    293 						if (yday >= bot) {
    294 							w = 1 + ((yday - bot) /
    295 								DAYSPERWEEK);
    296 							break;
    297 						}
    298 						--year;
    299 						yday += isleap(year) ?
    300 							DAYSPERLYEAR :
    301 							DAYSPERNYEAR;
    302 					}
    303 #ifdef XPG4_1994_04_09
    304 					if ((w == 52
    305 					     && t->tm_mon == TM_JANUARY)
    306 					    || (w == 1
    307 						&& t->tm_mon == TM_DECEMBER))
    308 						w = 53;
    309 #endif /* defined XPG4_1994_04_09 */
    310 					if (*format == 'V') {
    311 						if (!_conv(w, 2, '0',
    312 							pt, ptlim))
    313 							return (0);
    314 					} else if (*format == 'g') {
    315 						if (!_conv(year % 100, 2, '0',
    316 							pt, ptlim))
    317 							return (0);
    318 					} else	if (!_conv(year, 4, '0',
    319 							pt, ptlim))
    320 							return (0);
    321 				}
    322 				continue;
    323 			case 'W':
    324 				if (!_conv(MON_WEEK(t), 2, '0', pt, ptlim))
    325 					return (0);
    326 				continue;
    327 			case 'w':
    328 				if (!_conv(t->tm_wday, 1, '0', pt, ptlim))
    329 					return (0);
    330 				continue;
    331 			case 'x':
    332 				if (!_fmt(_CurrentTimeLocale->d_fmt, t, pt,
    333 				    ptlim))
    334 					return (0);
    335 				continue;
    336 			case 'X':
    337 				if (!_fmt(_CurrentTimeLocale->t_fmt, t, pt,
    338 				    ptlim))
    339 					return (0);
    340 				continue;
    341 			case 'y':
    342 				if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
    343 				    2, '0', pt, ptlim))
    344 					return (0);
    345 				continue;
    346 			case 'Y':
    347 				if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0',
    348 				    pt, ptlim))
    349 					return (0);
    350 				continue;
    351 			case 'Z':
    352 #ifdef TM_ZONE
    353 				if (t->TM_ZONE != NULL) {
    354 					if (!_add(t->TM_ZONE, pt, ptlim))
    355 						return (0);
    356 				} else
    357 #endif /* TM_ZONE */
    358 				if (tzname[t->tm_isdst ? 1 : 0] &&
    359 				    !_add(tzname[t->tm_isdst ? 1 : 0], pt,
    360 				    ptlim))
    361 					return (0);
    362 				continue;
    363 			case '%':
    364 			/*
    365 			 * X311J/88-090 (4.12.3.5): if conversion char is
    366 			 * undefined, behavior is undefined.  Print out the
    367 			 * character itself as printf(3) does.
    368 			 */
    369 			default:
    370 				break;
    371 			}
    372 		}
    373 		if (*pt == ptlim)
    374 			return (0);
    375 		*(*pt)++ = *format;
    376 	}
    377 	return (ptlim - *pt);
    378 }
    379 
    380 static int
    381 _secs(t, pt, ptlim)
    382 	const struct tm *t;
    383 	char **pt;
    384 	const char * const ptlim;
    385 {
    386 	char buf[15];
    387 	time_t s;
    388 	char *p;
    389 	struct tm tmp;
    390 
    391 	buf[sizeof (buf) - 1] = '\0';
    392 	/* Make a copy, mktime(3) modifies the tm struct. */
    393 	tmp = *t;
    394 	s = mktime(&tmp);
    395 	for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
    396 		*p-- = (char)(s % 10 + '0');
    397 	return (_add(++p, pt, ptlim));
    398 }
    399 
    400 static int
    401 _conv(n, digits, pad, pt, ptlim)
    402 	int n, digits;
    403 	int pad;
    404 	char **pt;
    405 	const char * const ptlim;
    406 {
    407 	char buf[10];
    408 	char *p;
    409 
    410 	buf[sizeof (buf) - 1] = '\0';
    411 	p = buf + sizeof(buf) - 2;
    412 	do {
    413 		*p-- = n % 10 + '0';
    414 		n /= 10;
    415 		--digits;
    416 	} while (n > 0 && p > buf);
    417 	while (p > buf && digits-- > 0)
    418 		*p-- = pad;
    419 	return (_add(++p, pt, ptlim));
    420 }
    421 
    422 static int
    423 _add(str, pt, ptlim)
    424 	const char *str;
    425 	char **pt;
    426 	const char * const ptlim;
    427 {
    428 
    429 	for (;; ++(*pt)) {
    430 		if (*pt == ptlim)
    431 			return (0);
    432 		if ((**pt = *str++) == '\0')
    433 			return (1);
    434 	}
    435 }
    436