Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.6
      1 /*	$NetBSD: strptime.c,v 1.6 1997/07/13 20:26:53 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 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.6 1997/07/13 20:26:53 christos Exp $");
     41 #endif
     42 
     43 #include <sys/localedef.h>
     44 #include <ctype.h>
     45 #include <locale.h>
     46 #include <string.h>
     47 #include <time.h>
     48 
     49 #define	_ctloc(x)		__CONCAT(_CurrentTimeLocale->,x)
     50 
     51 /*
     52  * We do not implement alternate representations. However, we always
     53  * check whether a given modifier is allowed for a certain conversion.
     54  */
     55 #define _ALT_E			0x01
     56 #define _ALT_O			0x02
     57 #define	_LEGAL_ALT(x)		{ if (alt_format & ~(x)) return (0); }
     58 
     59 
     60 static	int _conv_num __P((const char **, int *, int, int));
     61 
     62 
     63 char *
     64 strptime(buf, fmt, tm)
     65 	const char *buf, *fmt;
     66 	struct tm *tm;
     67 {
     68 	char c;
     69 	const char *bp;
     70 	int alt_format, i, len;
     71 
     72 	bp = buf;
     73 
     74 	while ((c = *fmt) != '\0') {
     75 		/* Clear `alternate' modifier prior to new conversion. */
     76 		alt_format = 0;
     77 
     78 		/* Eat up white-space. */
     79 		if (isspace(c)) {
     80 			while (isspace(*bp))
     81 				bp++;
     82 
     83 			fmt++;
     84 			continue;
     85 		}
     86 
     87 		if ((c = *fmt++) != '%')
     88 			goto literal;
     89 
     90 
     91 again:		switch (c = *fmt++) {
     92 		case '%':	/* "%%" is converted to "%". */
     93 literal:
     94 		if (c != *bp++)
     95 			return (0);
     96 
     97 		break;
     98 
     99 		/*
    100 		 * "Alternative" modifiers. Just set the appropriate flag
    101 		 * and start over again.
    102 		 */
    103 		case 'E':	/* "%E?" alternative conversion modifier. */
    104 			_LEGAL_ALT(0);
    105 			alt_format |= _ALT_E;
    106 			goto again;
    107 
    108 		case 'O':	/* "%O?" alternative conversion modifier. */
    109 			_LEGAL_ALT(0);
    110 			alt_format |= _ALT_O;
    111 			goto again;
    112 
    113 		/*
    114 		 * "Complex" conversion rules, implemented through recursion.
    115 		 */
    116 		case 'c':	/* Date and time, using the locale's format. */
    117 			_LEGAL_ALT(_ALT_E);
    118 			if (!(bp = strptime(bp, _ctloc(d_t_fmt), tm)))
    119 				return (0);
    120 			break;
    121 
    122 		case 'D':	/* The date as "%m/%d/%y". */
    123 			_LEGAL_ALT(0);
    124 			if (!(bp = strptime(bp, "%m/%d/%y", tm)))
    125 				return (0);
    126 			break;
    127 
    128 		case 'R':	/* The time as "%H:%M". */
    129 			_LEGAL_ALT(0);
    130 			if (!(bp = strptime(bp, "%H:%M", tm)))
    131 				return (0);
    132 			break;
    133 
    134 		case 'r':	/* The time in 12-hour clock representation. */
    135 			_LEGAL_ALT(0);
    136 			if (!(bp = strptime(bp, _ctloc(t_fmt_ampm), tm)))
    137 				return (0);
    138 			break;
    139 
    140 		case 'T':	/* The time as "%H:%M:%S". */
    141 			_LEGAL_ALT(0);
    142 			if (!(bp = strptime(bp, "%H:%M:%S", tm)))
    143 				return (0);
    144 			break;
    145 
    146 		case 'X':	/* The time, using the locale's format. */
    147 			_LEGAL_ALT(_ALT_E);
    148 			if (!(bp = strptime(bp, _ctloc(t_fmt), tm)))
    149 				return (0);
    150 			break;
    151 
    152 		case 'x':	/* The date, using the locale's format. */
    153 			_LEGAL_ALT(_ALT_E);
    154 			if (!(bp = strptime(bp, _ctloc(d_fmt), tm)))
    155 				return (0);
    156 			break;
    157 
    158 		/*
    159 		 * "Elementary" conversion rules.
    160 		 */
    161 		case 'A':	/* The day of week, using the locale's form. */
    162 		case 'a':
    163 			_LEGAL_ALT(0);
    164 			for (i = 0; i < 7; i++) {
    165 				/* Full name. */
    166 				len = strlen(_ctloc(day[i]));
    167 				if (strncmp(_ctloc(day[i]), bp, len) == 0)
    168 					break;
    169 
    170 				/* Abbreviated name. */
    171 				len = strlen(_ctloc(abday[i]));
    172 				if (strncmp(_ctloc(abday[i]), bp, len) == 0)
    173 					break;
    174 			}
    175 
    176 			/* Nothing matched. */
    177 			if (i == 7)
    178 				return (0);
    179 
    180 			tm->tm_wday = i;
    181 			bp += len;
    182 			break;
    183 
    184 		case 'B':	/* The month, using the locale's form. */
    185 		case 'b':
    186 		case 'h':
    187 			_LEGAL_ALT(0);
    188 			for (i = 0; i < 12; i++) {
    189 				/* Full name. */
    190 				len = strlen(_ctloc(mon[i]));
    191 				if (strncmp(_ctloc(mon[i]), bp, len) == 0)
    192 					break;
    193 
    194 				/* Abbreviated name. */
    195 				len = strlen(_ctloc(abmon[i]));
    196 				if (strncmp(_ctloc(abmon[i]), bp, len) == 0)
    197 					break;
    198 			}
    199 
    200 			/* Nothing matched. */
    201 			if (i == 12)
    202 				return (0);
    203 
    204 			tm->tm_mon = i;
    205 			bp += len;
    206 			break;
    207 
    208 		case 'C':	/* The century number. */
    209 			_LEGAL_ALT(_ALT_E);
    210 			if (!(_conv_num(&bp, &i, 0, 99)))
    211 				return (0);
    212 
    213 			tm->tm_year = i * 100;
    214 			break;
    215 
    216 		case 'd':	/* The day of month. */
    217 		case 'e':
    218 			_LEGAL_ALT(_ALT_O);
    219 			if (!(_conv_num(&bp, &tm->tm_mday, 1, 31)))
    220 				return (0);
    221 			break;
    222 
    223 		case 'k':	/* The hour (24-hour clock representation). */
    224 			_LEGAL_ALT(0);
    225 			/* FALLTHROUGH */
    226 		case 'H':
    227 			_LEGAL_ALT(_ALT_O);
    228 			if (!(_conv_num(&bp, &tm->tm_hour, 0, 23)))
    229 				return (0);
    230 			break;
    231 
    232 		case 'l':	/* The hour (12-hour clock representation). */
    233 			_LEGAL_ALT(0);
    234 			/* FALLTHROUGH */
    235 		case 'I':
    236 			_LEGAL_ALT(_ALT_O);
    237 			if (!(_conv_num(&bp, &tm->tm_hour, 0, 11)))
    238 				return (0);
    239 			break;
    240 
    241 		case 'j':	/* The day of year. */
    242 			_LEGAL_ALT(0);
    243 			if (!(_conv_num(&bp, &tm->tm_yday, 1, 366)))
    244 				return (0);
    245 			break;
    246 
    247 		case 'M':	/* The minute. */
    248 			_LEGAL_ALT(_ALT_O);
    249 			if (!(_conv_num(&bp, &tm->tm_min, 0, 59)))
    250 				return (0);
    251 			break;
    252 
    253 		case 'm':	/* The month. */
    254 			_LEGAL_ALT(_ALT_O);
    255 			if (!(_conv_num(&bp, &tm->tm_mon, 1, 12)))
    256 				return (0);
    257 			break;
    258 
    259 		case 'p':	/* The locale's equivalent of AM/PM. */
    260 			_LEGAL_ALT(0);
    261 			/* AM? */
    262 			if (strcmp(_ctloc(am_pm[0]), bp) == 0) {
    263 				if (tm->tm_hour > 12)	/* i.e., 13:00 AM ?! */
    264 					return (0);
    265 				else if (tm->tm_hour == 12)
    266 					tm->tm_hour = 0;
    267 
    268 				bp += strlen(_ctloc(am_pm[0]));
    269 				break;
    270 			}
    271 			/* PM? */
    272 			else if (strcmp(_ctloc(am_pm[1]), bp) == 0) {
    273 				if (tm->tm_hour > 12)	/* i.e., 13:00 PM ?! */
    274 					return (0);
    275 				else if (tm->tm_hour < 12)
    276 					tm->tm_hour += 12;
    277 
    278 				bp += strlen(_ctloc(am_pm[1]));
    279 				break;
    280 			}
    281 
    282 			/* Nothing matched. */
    283 			return (0);
    284 
    285 		case 'S':	/* The seconds. */
    286 			_LEGAL_ALT(_ALT_O);
    287 			if (!(_conv_num(&bp, &tm->tm_sec, 1, 61)))
    288 				return (0);
    289 			break;
    290 
    291 		case 'U':	/* The week of year, beginning on sunday. */
    292 		case 'W':	/* The week of year, beginning on monday. */
    293 			_LEGAL_ALT(_ALT_O);
    294 			/*
    295 			 * XXX This is bogus, as we can not assume any valid
    296 			 * information present in the tm structure at this
    297 			 * point to calculate a real value, so just check the
    298 			 * range for now.
    299 			 */
    300 			 if (!(_conv_num(&bp, &i, 0, 53)))
    301 				return (0);
    302 			 break;
    303 
    304 		case 'w':	/* The day of week, beginning on sunday. */
    305 			_LEGAL_ALT(_ALT_O);
    306 			if (!(_conv_num(&bp, &tm->tm_wday, 0, 6)))
    307 				return (0);
    308 			break;
    309 
    310 		case 'Y':	/* The year. */
    311 			_LEGAL_ALT(_ALT_E);
    312 			if (!(_conv_num(&bp, &i, 0, INT_MAX)))
    313 				return (0);
    314 
    315 			tm->tm_year = i - 1900;
    316 			break;
    317 
    318 		case 'y':	/* The year within the 20th century. */
    319 			_LEGAL_ALT(_ALT_E | _ALT_O);
    320 			if (!(_conv_num(&bp, &tm->tm_year, 0, 99)))
    321 				return (0);
    322 			break;
    323 
    324 		/*
    325 		 * Miscellaneous conversions.
    326 		 */
    327 		case 'n':	/* Any kind of white-space. */
    328 		case 't':
    329 			_LEGAL_ALT(0);
    330 			while (isspace(*bp))
    331 				bp++;
    332 			break;
    333 
    334 
    335 		default:	/* Unknown/unsupported conversion. */
    336 			return (0);
    337 		}
    338 
    339 
    340 	}
    341 
    342 	return ((char *)bp);
    343 }
    344 
    345 
    346 static int
    347 _conv_num(buf, dest, llim, ulim)
    348 	const char **buf;
    349 	int *dest;
    350 	int llim, ulim;
    351 {
    352 	*dest = 0;
    353 
    354 	if (**buf < '0' || **buf > '9')
    355 		return (0);
    356 
    357 	do {
    358 		*dest *= 10;
    359 		*dest += *(*buf)++ - '0';
    360 	} while ((*dest * 10 <= ulim) && **buf >= '0' && **buf <= '9');
    361 
    362 	if (*dest < llim || *dest > ulim)
    363 		return (0);
    364 
    365 	return (1);
    366 }
    367