Home | History | Annotate | Line # | Download | only in time
strftime.c revision 1.5
      1 /*	$NetBSD: strftime.c,v 1.5 1998/09/27 16:41:20 kleink 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.5 1998/09/27 16:41:20 kleink Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include "namespace.h"
     46 #include <sys/localedef.h>
     47 #include <locale.h>
     48 #include <string.h>
     49 #include <tzfile.h>
     50 #include <time.h>
     51 
     52 static	int _add __P((const char *, char **, const char *));
     53 static	int _conv __P((int, int, char, char **, const char *));
     54 static	int _secs __P((const struct tm *, char **, const char *));
     55 static	size_t _fmt __P((const char *, const struct tm *, char **,
     56 	    const char *));
     57 
     58 size_t
     59 strftime(s, maxsize, format, t)
     60 	char *s;
     61 	size_t maxsize;
     62 	const char *format;
     63 	const struct tm *t;
     64 {
     65 	char *pt;
     66 
     67 	tzset();
     68 	if (maxsize < 1)
     69 		return (0);
     70 
     71 	pt = s;
     72 	if (_fmt(format, t, &pt, s + maxsize)) {
     73 		*pt = '\0';
     74 		return (pt - s);
     75 	} else
     76 		return (0);
     77 }
     78 
     79 #define SUN_WEEK(t)	(((t)->tm_yday + 7 - \
     80 				((t)->tm_wday)) / 7)
     81 #define MON_WEEK(t)	(((t)->tm_yday + 7 - \
     82 				((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
     83 
     84 static size_t
     85 _fmt(format, t, pt, ptlim)
     86 	const char *format;
     87 	const struct tm *t;
     88 	char **pt;
     89 	const char * const ptlim;
     90 {
     91 	for (; *format; ++format) {
     92 		if (*format == '%') {
     93 			++format;
     94 			if (*format == 'E') {
     95 				/* Alternate Era */
     96 				++format;
     97 			} else if (*format == 'O') {
     98 				/* Alternate numeric symbols */
     99 				++format;
    100 			}
    101 			switch (*format) {
    102 			case '\0':
    103 				--format;
    104 				break;
    105 			case 'A':
    106 				if (t->tm_wday < 0 || t->tm_wday > 6)
    107 					return (0);
    108 				if (!_add(_CurrentTimeLocale->day[t->tm_wday],
    109 				    pt, ptlim))
    110 					return (0);
    111 				continue;
    112 
    113 			case 'a':
    114 				if (t->tm_wday < 0 || t->tm_wday > 6)
    115 					return (0);
    116 				if (!_add(_CurrentTimeLocale->abday[t->tm_wday],
    117 				    pt, ptlim))
    118 					return (0);
    119 				continue;
    120 			case 'B':
    121 				if (t->tm_mon < 0 || t->tm_mon > 11)
    122 					return (0);
    123 				if (!_add(_CurrentTimeLocale->mon[t->tm_mon],
    124 				    pt, ptlim))
    125 					return (0);
    126 				continue;
    127 			case 'b':
    128 			case 'h':
    129 				if (t->tm_mon < 0 || t->tm_mon > 11)
    130 					return (0);
    131 				if (!_add(_CurrentTimeLocale->abmon[t->tm_mon],
    132 				    pt, ptlim))
    133 					return (0);
    134 				continue;
    135 			case 'C':
    136 				if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
    137 				    2, '0', pt, ptlim))
    138 					return (0);
    139 				continue;
    140 			case 'c':
    141 				if (!_fmt(_CurrentTimeLocale->d_t_fmt, t, pt,
    142 				    ptlim))
    143 					return (0);
    144 				continue;
    145 			case 'D':
    146 				if (!_fmt("%m/%d/%y", t, pt, ptlim))
    147 					return (0);
    148 				continue;
    149 			case 'd':
    150 				if (!_conv(t->tm_mday, 2, '0', pt, ptlim))
    151 					return (0);
    152 				continue;
    153 			case 'e':
    154 				if (!_conv(t->tm_mday, 2, ' ', pt, ptlim))
    155 					return (0);
    156 				continue;
    157 			case 'H':
    158 				if (!_conv(t->tm_hour, 2, '0', pt, ptlim))
    159 					return (0);
    160 				continue;
    161 			case 'I':
    162 				if (!_conv(t->tm_hour % 12 ?
    163 				    t->tm_hour % 12 : 12, 2, '0', pt, ptlim))
    164 					return (0);
    165 				continue;
    166 			case 'j':
    167 				if (!_conv(t->tm_yday + 1, 3, '0', pt, ptlim))
    168 					return (0);
    169 				continue;
    170 			case 'k':
    171 				if (!_conv(t->tm_hour, 2, ' ', pt, ptlim))
    172 					return (0);
    173 				continue;
    174 			case 'l':
    175 				if (!_conv(t->tm_hour % 12 ?
    176 				    t->tm_hour % 12: 12, 2, ' ', pt, ptlim))
    177 					return (0);
    178 				continue;
    179 			case 'M':
    180 				if (!_conv(t->tm_min, 2, '0', pt, ptlim))
    181 					return (0);
    182 				continue;
    183 			case 'm':
    184 				if (!_conv(t->tm_mon + 1, 2, '0', pt, ptlim))
    185 					return (0);
    186 				continue;
    187 			case 'n':
    188 				if (!_add("\n", pt, ptlim))
    189 					return (0);
    190 				continue;
    191 			case 'p':
    192 				if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour
    193 				    >= 12], pt, ptlim))
    194 					return (0);
    195 				continue;
    196 			case 'R':
    197 				if (!_fmt("%H:%M", t, pt, ptlim))
    198 					return (0);
    199 				continue;
    200 			case 'r':
    201 				if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t, pt,
    202 				    ptlim))
    203 					return (0);
    204 				continue;
    205 			case 'S':
    206 				if (!_conv(t->tm_sec, 2, '0', pt, ptlim))
    207 					return (0);
    208 				continue;
    209 			case 's':
    210 				if (!_secs(t, pt, ptlim))
    211 					return (0);
    212 				continue;
    213 			case 'T':
    214 				if (!_fmt("%H:%M:%S", t, pt, ptlim))
    215 					return (0);
    216 				continue;
    217 			case 't':
    218 				if (!_add("\t", pt, ptlim))
    219 					return (0);
    220 				continue;
    221 			case 'U':
    222 				if (!_conv(SUN_WEEK(t), 2, '0', pt, ptlim))
    223 					return (0);
    224 				continue;
    225 			case 'u':
    226 				if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0',
    227 				    pt, ptlim))
    228 					return (0);
    229 				continue;
    230 			case 'V':
    231 				{
    232 				/* ISO 8601 Week Of Year:
    233 				 *  If the week (Monday - Sunday) containing
    234 				 * January 1 has four or more days in the new
    235 				 * year, then it is week 1; otherwise it is
    236 				 * week 53 of the previous year and the next
    237 				 * week is week one.
    238 				 */
    239 
    240 				int week = MON_WEEK(t);
    241 
    242 				int days = (((t)->tm_yday + 7 -
    243 				    ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) % 7);
    244 
    245 
    246 				if (days >= 4) {
    247 					week++;
    248 				} else if (week == 0) {
    249 					week = 53;
    250 				}
    251 
    252 				if (!_conv(week, 2, '0', pt, ptlim))
    253 					return (0);
    254 				continue;
    255 				}
    256 			case 'W':
    257 				if (!_conv(MON_WEEK(t), 2, '0', pt, ptlim))
    258 					return (0);
    259 				continue;
    260 			case 'w':
    261 				if (!_conv(t->tm_wday, 1, '0', pt, ptlim))
    262 					return (0);
    263 				continue;
    264 			case 'x':
    265 				if (!_fmt(_CurrentTimeLocale->d_fmt, t, pt,
    266 				    ptlim))
    267 					return (0);
    268 				continue;
    269 			case 'X':
    270 				if (!_fmt(_CurrentTimeLocale->t_fmt, t, pt,
    271 				    ptlim))
    272 					return (0);
    273 				continue;
    274 			case 'y':
    275 				if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
    276 				    2, '0', pt, ptlim))
    277 					return (0);
    278 				continue;
    279 			case 'Y':
    280 				if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0',
    281 				    pt, ptlim))
    282 					return (0);
    283 				continue;
    284 			case 'Z':
    285 				if (tzname[t->tm_isdst ? 1 : 0] &&
    286 				    !_add(tzname[t->tm_isdst ? 1 : 0], pt,
    287 				    ptlim))
    288 					return (0);
    289 				continue;
    290 			case '%':
    291 			/*
    292 			 * X311J/88-090 (4.12.3.5): if conversion char is
    293 			 * undefined, behavior is undefined.  Print out the
    294 			 * character itself as printf(3) does.
    295 			 */
    296 			default:
    297 				break;
    298 			}
    299 		}
    300 		if (*pt == ptlim)
    301 			return (0);
    302 		*(*pt)++ = *format;
    303 	}
    304 	return (ptlim - *pt);
    305 }
    306 
    307 static int
    308 _secs(t, pt, ptlim)
    309 	const struct tm *t;
    310 	char **pt;
    311 	const char * const ptlim;
    312 {
    313 	char buf[15];
    314 	time_t s;
    315 	char *p;
    316 	struct tm tmp;
    317 
    318 	/* Make a copy, mktime(3) modifies the tm struct. */
    319 	tmp = *t;
    320 	s = mktime(&tmp);
    321 	for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
    322 		*p-- = s % 10 + '0';
    323 	return (_add(++p, pt, ptlim));
    324 }
    325 
    326 static int
    327 _conv(n, digits, pad, pt, ptlim)
    328 	int n, digits;
    329 	char pad;
    330 	char **pt;
    331 	const char * const ptlim;
    332 {
    333 	char buf[10];
    334 	char *p;
    335 
    336 	buf[sizeof (buf) - 1] = '\0';
    337 	for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
    338 		*p-- = n % 10 + '0';
    339 	while (p > buf && digits-- > 0)
    340 		*p-- = pad;
    341 	return (_add(++p, pt, ptlim));
    342 }
    343 
    344 static int
    345 _add(str, pt, ptlim)
    346 	const char *str;
    347 	char **pt;
    348 	const char * const ptlim;
    349 {
    350 
    351 	for (;; ++(*pt)) {
    352 		if (*pt == ptlim)
    353 			return (0);
    354 		if ((**pt = *str++) == '\0')
    355 			return (1);
    356 	}
    357 }
    358