Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.26
      1 /*	$NetBSD: strptime.c,v 1.26 2008/04/24 21:34:48 ginsbach Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code was contributed to The NetBSD Foundation by Klaus Klein.
      8  * Heavily optimised by David Laight
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 __RCSID("$NetBSD: strptime.c,v 1.26 2008/04/24 21:34:48 ginsbach Exp $");
     42 #endif
     43 
     44 #include "namespace.h"
     45 #include <sys/localedef.h>
     46 #include <ctype.h>
     47 #include <locale.h>
     48 #include <string.h>
     49 #include <time.h>
     50 #include <tzfile.h>
     51 
     52 #ifdef __weak_alias
     53 __weak_alias(strptime,_strptime)
     54 #endif
     55 
     56 #define	_ctloc(x)		(_CurrentTimeLocale->x)
     57 
     58 /*
     59  * We do not implement alternate representations. However, we always
     60  * check whether a given modifier is allowed for a certain conversion.
     61  */
     62 #define ALT_E			0x01
     63 #define ALT_O			0x02
     64 #define	LEGAL_ALT(x)		{ if (alt_format & ~(x)) return NULL; }
     65 
     66 static const char gmt[4] = { "GMT" };
     67 
     68 static const u_char *conv_num(const unsigned char *, int *, uint, uint);
     69 static const u_char *find_string(const u_char *, int *, const char * const *,
     70 	const char * const *, int);
     71 
     72 
     73 char *
     74 strptime(const char *buf, const char *fmt, struct tm *tm)
     75 {
     76 	unsigned char c;
     77 	const unsigned char *bp;
     78 	int alt_format, i, split_year = 0;
     79 	const char *new_fmt;
     80 
     81 	bp = (const u_char *)buf;
     82 
     83 	while (bp != NULL && (c = *fmt++) != '\0') {
     84 		/* Clear `alternate' modifier prior to new conversion. */
     85 		alt_format = 0;
     86 		i = 0;
     87 
     88 		/* Eat up white-space. */
     89 		if (isspace(c)) {
     90 			while (isspace(*bp))
     91 				bp++;
     92 			continue;
     93 		}
     94 
     95 		if (c != '%')
     96 			goto literal;
     97 
     98 
     99 again:		switch (c = *fmt++) {
    100 		case '%':	/* "%%" is converted to "%". */
    101 literal:
    102 			if (c != *bp++)
    103 				return NULL;
    104 			LEGAL_ALT(0);
    105 			continue;
    106 
    107 		/*
    108 		 * "Alternative" modifiers. Just set the appropriate flag
    109 		 * and start over again.
    110 		 */
    111 		case 'E':	/* "%E?" alternative conversion modifier. */
    112 			LEGAL_ALT(0);
    113 			alt_format |= ALT_E;
    114 			goto again;
    115 
    116 		case 'O':	/* "%O?" alternative conversion modifier. */
    117 			LEGAL_ALT(0);
    118 			alt_format |= ALT_O;
    119 			goto again;
    120 
    121 		/*
    122 		 * "Complex" conversion rules, implemented through recursion.
    123 		 */
    124 		case 'c':	/* Date and time, using the locale's format. */
    125 			new_fmt = _ctloc(d_t_fmt);
    126 			goto recurse;
    127 
    128 		case 'D':	/* The date as "%m/%d/%y". */
    129 			new_fmt = "%m/%d/%y";
    130 			LEGAL_ALT(0);
    131 			goto recurse;
    132 
    133 		case 'R':	/* The time as "%H:%M". */
    134 			new_fmt = "%H:%M";
    135 			LEGAL_ALT(0);
    136 			goto recurse;
    137 
    138 		case 'r':	/* The time in 12-hour clock representation. */
    139 			new_fmt =_ctloc(t_fmt_ampm);
    140 			LEGAL_ALT(0);
    141 			goto recurse;
    142 
    143 		case 'T':	/* The time as "%H:%M:%S". */
    144 			new_fmt = "%H:%M:%S";
    145 			LEGAL_ALT(0);
    146 			goto recurse;
    147 
    148 		case 'X':	/* The time, using the locale's format. */
    149 			new_fmt =_ctloc(t_fmt);
    150 			goto recurse;
    151 
    152 		case 'x':	/* The date, using the locale's format. */
    153 			new_fmt =_ctloc(d_fmt);
    154 		    recurse:
    155 			bp = (const u_char *)strptime((const char *)bp,
    156 							    new_fmt, tm);
    157 			LEGAL_ALT(ALT_E);
    158 			continue;
    159 
    160 		/*
    161 		 * "Elementary" conversion rules.
    162 		 */
    163 		case 'A':	/* The day of week, using the locale's form. */
    164 		case 'a':
    165 			bp = find_string(bp, &tm->tm_wday, _ctloc(day),
    166 					_ctloc(abday), 7);
    167 			LEGAL_ALT(0);
    168 			continue;
    169 
    170 		case 'B':	/* The month, using the locale's form. */
    171 		case 'b':
    172 		case 'h':
    173 			bp = find_string(bp, &tm->tm_mon, _ctloc(mon),
    174 					_ctloc(abmon), 12);
    175 			LEGAL_ALT(0);
    176 			continue;
    177 
    178 		case 'C':	/* The century number. */
    179 			i = 20;
    180 			bp = conv_num(bp, &i, 0, 99);
    181 
    182 			i = i * 100 - TM_YEAR_BASE;
    183 			if (split_year)
    184 				i += tm->tm_year % 100;
    185 			split_year = 1;
    186 			tm->tm_year = i;
    187 			LEGAL_ALT(ALT_E);
    188 			continue;
    189 
    190 		case 'd':	/* The day of month. */
    191 		case 'e':
    192 			bp = conv_num(bp, &tm->tm_mday, 1, 31);
    193 			LEGAL_ALT(ALT_O);
    194 			continue;
    195 
    196 		case 'k':	/* The hour (24-hour clock representation). */
    197 			LEGAL_ALT(0);
    198 			/* FALLTHROUGH */
    199 		case 'H':
    200 			bp = conv_num(bp, &tm->tm_hour, 0, 23);
    201 			LEGAL_ALT(ALT_O);
    202 			continue;
    203 
    204 		case 'l':	/* The hour (12-hour clock representation). */
    205 			LEGAL_ALT(0);
    206 			/* FALLTHROUGH */
    207 		case 'I':
    208 			bp = conv_num(bp, &tm->tm_hour, 1, 12);
    209 			if (tm->tm_hour == 12)
    210 				tm->tm_hour = 0;
    211 			LEGAL_ALT(ALT_O);
    212 			continue;
    213 
    214 		case 'j':	/* The day of year. */
    215 			i = 1;
    216 			bp = conv_num(bp, &i, 1, 366);
    217 			tm->tm_yday = i - 1;
    218 			LEGAL_ALT(0);
    219 			continue;
    220 
    221 		case 'M':	/* The minute. */
    222 			bp = conv_num(bp, &tm->tm_min, 0, 59);
    223 			LEGAL_ALT(ALT_O);
    224 			continue;
    225 
    226 		case 'm':	/* The month. */
    227 			i = 1;
    228 			bp = conv_num(bp, &i, 1, 12);
    229 			tm->tm_mon = i - 1;
    230 			LEGAL_ALT(ALT_O);
    231 			continue;
    232 
    233 		case 'p':	/* The locale's equivalent of AM/PM. */
    234 			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
    235 			if (tm->tm_hour > 11)
    236 				return NULL;
    237 			tm->tm_hour += i * 12;
    238 			LEGAL_ALT(0);
    239 			continue;
    240 
    241 		case 'S':	/* The seconds. */
    242 			bp = conv_num(bp, &tm->tm_sec, 0, 61);
    243 			LEGAL_ALT(ALT_O);
    244 			continue;
    245 
    246 		case 'U':	/* The week of year, beginning on sunday. */
    247 		case 'W':	/* The week of year, beginning on monday. */
    248 			/*
    249 			 * XXX This is bogus, as we can not assume any valid
    250 			 * information present in the tm structure at this
    251 			 * point to calculate a real value, so just check the
    252 			 * range for now.
    253 			 */
    254 			 bp = conv_num(bp, &i, 0, 53);
    255 			 LEGAL_ALT(ALT_O);
    256 			 continue;
    257 
    258 		case 'w':	/* The day of week, beginning on sunday. */
    259 			bp = conv_num(bp, &tm->tm_wday, 0, 6);
    260 			LEGAL_ALT(ALT_O);
    261 			continue;
    262 
    263 		case 'Y':	/* The year. */
    264 			i = TM_YEAR_BASE;	/* just for data sanity... */
    265 			bp = conv_num(bp, &i, 0, 9999);
    266 			tm->tm_year = i - TM_YEAR_BASE;
    267 			LEGAL_ALT(ALT_E);
    268 			continue;
    269 
    270 		case 'y':	/* The year within 100 years of the epoch. */
    271 			/* LEGAL_ALT(ALT_E | ALT_O); */
    272 			bp = conv_num(bp, &i, 0, 99);
    273 
    274 			if (split_year)
    275 				/* preserve century */
    276 				i += (tm->tm_year / 100) * 100;
    277 			else {
    278 				split_year = 1;
    279 				if (i <= 68)
    280 					i = i + 2000 - TM_YEAR_BASE;
    281 				else
    282 					i = i + 1900 - TM_YEAR_BASE;
    283 			}
    284 			tm->tm_year = i;
    285 			continue;
    286 
    287 		case 'Z':
    288 			tzset();
    289 			if (strncmp((const char *)bp, gmt, 3) == 0) {
    290 				tm->tm_isdst = 0;
    291 #ifdef TM_GMTOFF
    292 				tm->TM_GMTOFF = 0;
    293 #endif
    294 #ifdef TM_ZONE
    295 				tm->TM_ZONE = gmt;
    296 #endif
    297 				bp += 3;
    298 			} else {
    299 				const unsigned char *ep;
    300 
    301 				ep = find_string(bp, &i,
    302 					       	 (const char * const *)tzname,
    303 					       	  NULL, 2);
    304 				if (ep != NULL) {
    305 					tm->tm_isdst = i;
    306 #ifdef TM_GMTOFF
    307 					tm->TM_GMTOFF = -(timezone);
    308 #endif
    309 #ifdef TM_ZONE
    310 					tm->TM_ZONE = tzname[i];
    311 #endif
    312 				}
    313 				bp = ep;
    314 			}
    315 			continue;
    316 
    317 		/*
    318 		 * Miscellaneous conversions.
    319 		 */
    320 		case 'n':	/* Any kind of white-space. */
    321 		case 't':
    322 			while (isspace(*bp))
    323 				bp++;
    324 			LEGAL_ALT(0);
    325 			continue;
    326 
    327 
    328 		default:	/* Unknown/unsupported conversion. */
    329 			return NULL;
    330 		}
    331 	}
    332 
    333 	return __UNCONST(bp);
    334 }
    335 
    336 
    337 static const u_char *
    338 conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
    339 {
    340 	uint result = 0;
    341 	unsigned char ch;
    342 
    343 	/* The limit also determines the number of valid digits. */
    344 	uint rulim = ulim;
    345 
    346 	ch = *buf;
    347 	if (ch < '0' || ch > '9')
    348 		return NULL;
    349 
    350 	do {
    351 		result *= 10;
    352 		result += ch - '0';
    353 		rulim /= 10;
    354 		ch = *++buf;
    355 	} while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
    356 
    357 	if (result < llim || result > ulim)
    358 		return NULL;
    359 
    360 	*dest = result;
    361 	return buf;
    362 }
    363 
    364 static const u_char *
    365 find_string(const u_char *bp, int *tgt, const char * const *n1,
    366 		const char * const *n2, int c)
    367 {
    368 	int i;
    369 	unsigned int len;
    370 
    371 	/* check full name - then abbreviated ones */
    372 	for (; n1 != NULL; n1 = n2, n2 = NULL) {
    373 		for (i = 0; i < c; i++, n1++) {
    374 			len = strlen(*n1);
    375 			if (strncasecmp(*n1, (const char *)bp, len) == 0) {
    376 				*tgt = i;
    377 				return bp + len;
    378 			}
    379 		}
    380 	}
    381 
    382 	/* Nothing matched */
    383 	return NULL;
    384 }
    385