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