Home | History | Annotate | Line # | Download | only in time
strftime.c revision 1.8.2.1
      1 /*	$NetBSD: strftime.c,v 1.8.2.1 2000/09/11 20:13:36 he 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.8.2.1 2000/09/11 20:13:36 he 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 'H':
    159 				if (!_conv(t->tm_hour, 2, '0', pt, ptlim))
    160 					return (0);
    161 				continue;
    162 			case 'I':
    163 				if (!_conv(t->tm_hour % 12 ?
    164 				    t->tm_hour % 12 : 12, 2, '0', pt, ptlim))
    165 					return (0);
    166 				continue;
    167 			case 'j':
    168 				if (!_conv(t->tm_yday + 1, 3, '0', pt, ptlim))
    169 					return (0);
    170 				continue;
    171 			case 'k':
    172 				if (!_conv(t->tm_hour, 2, ' ', pt, ptlim))
    173 					return (0);
    174 				continue;
    175 			case 'l':
    176 				if (!_conv(t->tm_hour % 12 ?
    177 				    t->tm_hour % 12: 12, 2, ' ', pt, ptlim))
    178 					return (0);
    179 				continue;
    180 			case 'M':
    181 				if (!_conv(t->tm_min, 2, '0', pt, ptlim))
    182 					return (0);
    183 				continue;
    184 			case 'm':
    185 				if (!_conv(t->tm_mon + 1, 2, '0', pt, ptlim))
    186 					return (0);
    187 				continue;
    188 			case 'n':
    189 				if (!_add("\n", pt, ptlim))
    190 					return (0);
    191 				continue;
    192 			case 'p':
    193 				if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour
    194 				    >= 12], pt, ptlim))
    195 					return (0);
    196 				continue;
    197 			case 'R':
    198 				if (!_fmt("%H:%M", t, pt, ptlim))
    199 					return (0);
    200 				continue;
    201 			case 'r':
    202 				if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t, pt,
    203 				    ptlim))
    204 					return (0);
    205 				continue;
    206 			case 'S':
    207 				if (!_conv(t->tm_sec, 2, '0', pt, ptlim))
    208 					return (0);
    209 				continue;
    210 			case 's':
    211 				if (!_secs(t, pt, ptlim))
    212 					return (0);
    213 				continue;
    214 			case 'T':
    215 				if (!_fmt("%H:%M:%S", t, pt, ptlim))
    216 					return (0);
    217 				continue;
    218 			case 't':
    219 				if (!_add("\t", pt, ptlim))
    220 					return (0);
    221 				continue;
    222 			case 'U':
    223 				if (!_conv(SUN_WEEK(t), 2, '0', pt, ptlim))
    224 					return (0);
    225 				continue;
    226 			case 'u':
    227 				if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0',
    228 				    pt, ptlim))
    229 					return (0);
    230 				continue;
    231 			case 'V':	/* ISO 8601 week number */
    232 			case 'G':	/* ISO 8601 year (four digits) */
    233 			case 'g':	/* ISO 8601 year (two digits) */
    234 /*
    235 ** From Arnold Robbins' strftime version 3.0:  "the week number of the
    236 ** year (the first Monday as the first day of week 1) as a decimal number
    237 ** (01-53)."
    238 ** (ado, 1993-05-24)
    239 **
    240 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
    241 ** "Week 01 of a year is per definition the first week which has the
    242 ** Thursday in this year, which is equivalent to the week which contains
    243 ** the fourth day of January. In other words, the first week of a new year
    244 ** is the week which has the majority of its days in the new year. Week 01
    245 ** might also contain days from the previous year and the week before week
    246 ** 01 of a year is the last week (52 or 53) of the previous year even if
    247 ** it contains days from the new year. A week starts with Monday (day 1)
    248 ** and ends with Sunday (day 7).  For example, the first week of the year
    249 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
    250 ** (ado, 1996-01-02)
    251 */
    252 				{
    253 					int	year;
    254 					int	yday;
    255 					int	wday;
    256 					int	w;
    257 
    258 					year = t->tm_year + TM_YEAR_BASE;
    259 					yday = t->tm_yday;
    260 					wday = t->tm_wday;
    261 					for ( ; ; ) {
    262 						int	len;
    263 						int	bot;
    264 						int	top;
    265 
    266 						len = isleap(year) ?
    267 							DAYSPERLYEAR :
    268 							DAYSPERNYEAR;
    269 						/*
    270 						** What yday (-3 ... 3) does
    271 						** the ISO year begin on?
    272 						*/
    273 						bot = ((yday + 11 - wday) %
    274 							DAYSPERWEEK) - 3;
    275 						/*
    276 						** What yday does the NEXT
    277 						** ISO year begin on?
    278 						*/
    279 						top = bot -
    280 							(len % DAYSPERWEEK);
    281 						if (top < -3)
    282 							top += DAYSPERWEEK;
    283 						top += len;
    284 						if (yday >= top) {
    285 							++year;
    286 							w = 1;
    287 							break;
    288 						}
    289 						if (yday >= bot) {
    290 							w = 1 + ((yday - bot) /
    291 								DAYSPERWEEK);
    292 							break;
    293 						}
    294 						--year;
    295 						yday += isleap(year) ?
    296 							DAYSPERLYEAR :
    297 							DAYSPERNYEAR;
    298 					}
    299 #ifdef XPG4_1994_04_09
    300 					if ((w == 52
    301 					     && t->tm_mon == TM_JANUARY)
    302 					    || (w == 1
    303 						&& t->tm_mon == TM_DECEMBER))
    304 						w = 53;
    305 #endif /* defined XPG4_1994_04_09 */
    306 					if (*format == 'V') {
    307 						if (!_conv(w, 2, '0',
    308 							pt, ptlim))
    309 							return (0);
    310 					} else if (*format == 'g') {
    311 						if (!_conv(year % 100, 2, '0',
    312 							pt, ptlim))
    313 							return (0);
    314 					} else	if (!_conv(year, 4, '0',
    315 							pt, ptlim))
    316 							return (0);
    317 				}
    318 				continue;
    319 			case 'W':
    320 				if (!_conv(MON_WEEK(t), 2, '0', pt, ptlim))
    321 					return (0);
    322 				continue;
    323 			case 'w':
    324 				if (!_conv(t->tm_wday, 1, '0', pt, ptlim))
    325 					return (0);
    326 				continue;
    327 			case 'x':
    328 				if (!_fmt(_CurrentTimeLocale->d_fmt, t, pt,
    329 				    ptlim))
    330 					return (0);
    331 				continue;
    332 			case 'X':
    333 				if (!_fmt(_CurrentTimeLocale->t_fmt, t, pt,
    334 				    ptlim))
    335 					return (0);
    336 				continue;
    337 			case 'y':
    338 				if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
    339 				    2, '0', pt, ptlim))
    340 					return (0);
    341 				continue;
    342 			case 'Y':
    343 				if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0',
    344 				    pt, ptlim))
    345 					return (0);
    346 				continue;
    347 			case 'Z':
    348 #ifdef TM_ZONE
    349 				if (t->TM_ZONE != NULL) {
    350 					if (!_add(t->TM_ZONE, pt, ptlim))
    351 						return (0);
    352 				} else
    353 #endif /* TM_ZONE */
    354 				if (tzname[t->tm_isdst ? 1 : 0] &&
    355 				    !_add(tzname[t->tm_isdst ? 1 : 0], pt,
    356 				    ptlim))
    357 					return (0);
    358 				continue;
    359 			case '%':
    360 			/*
    361 			 * X311J/88-090 (4.12.3.5): if conversion char is
    362 			 * undefined, behavior is undefined.  Print out the
    363 			 * character itself as printf(3) does.
    364 			 */
    365 			default:
    366 				break;
    367 			}
    368 		}
    369 		if (*pt == ptlim)
    370 			return (0);
    371 		*(*pt)++ = *format;
    372 	}
    373 	return (ptlim - *pt);
    374 }
    375 
    376 static int
    377 _secs(t, pt, ptlim)
    378 	const struct tm *t;
    379 	char **pt;
    380 	const char * const ptlim;
    381 {
    382 	char buf[15];
    383 	time_t s;
    384 	char *p;
    385 	struct tm tmp;
    386 
    387 	buf[sizeof (buf) - 1] = '\0';
    388 	/* Make a copy, mktime(3) modifies the tm struct. */
    389 	tmp = *t;
    390 	s = mktime(&tmp);
    391 	for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
    392 		*p-- = (char)(s % 10 + '0');
    393 	return (_add(++p, pt, ptlim));
    394 }
    395 
    396 static int
    397 _conv(n, digits, pad, pt, ptlim)
    398 	int n, digits;
    399 	int pad;
    400 	char **pt;
    401 	const char * const ptlim;
    402 {
    403 	char buf[10];
    404 	char *p;
    405 
    406 	buf[sizeof (buf) - 1] = '\0';
    407 	for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
    408 		*p-- = n % 10 + '0';
    409 	while (p > buf && digits-- > 0)
    410 		*p-- = pad;
    411 	return (_add(++p, pt, ptlim));
    412 }
    413 
    414 static int
    415 _add(str, pt, ptlim)
    416 	const char *str;
    417 	char **pt;
    418 	const char * const ptlim;
    419 {
    420 
    421 	for (;; ++(*pt)) {
    422 		if (*pt == ptlim)
    423 			return (0);
    424 		if ((**pt = *str++) == '\0')
    425 			return (1);
    426 	}
    427 }
    428