Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.12
      1 /*	$NetBSD: strptime.c,v 1.12 1998/01/20 21:39:40 mycroft 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.12 1998/01/20 21:39:40 mycroft 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;
     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 
    103 		break;
    104 
    105 		/*
    106 		 * "Alternative" modifiers. Just set the appropriate flag
    107 		 * and start over again.
    108 		 */
    109 		case 'E':	/* "%E?" alternative conversion modifier. */
    110 			_LEGAL_ALT(0);
    111 			alt_format |= _ALT_E;
    112 			goto again;
    113 
    114 		case 'O':	/* "%O?" alternative conversion modifier. */
    115 			_LEGAL_ALT(0);
    116 			alt_format |= _ALT_O;
    117 			goto again;
    118 
    119 		/*
    120 		 * "Complex" conversion rules, implemented through recursion.
    121 		 */
    122 		case 'c':	/* Date and time, using the locale's format. */
    123 			_LEGAL_ALT(_ALT_E);
    124 			if (!(bp = strptime(bp, _ctloc(d_t_fmt), tm)))
    125 				return (0);
    126 			break;
    127 
    128 		case 'D':	/* The date as "%m/%d/%y". */
    129 			_LEGAL_ALT(0);
    130 			if (!(bp = strptime(bp, "%m/%d/%y", tm)))
    131 				return (0);
    132 			break;
    133 
    134 		case 'R':	/* The time as "%H:%M". */
    135 			_LEGAL_ALT(0);
    136 			if (!(bp = strptime(bp, "%H:%M", tm)))
    137 				return (0);
    138 			break;
    139 
    140 		case 'r':	/* The time in 12-hour clock representation. */
    141 			_LEGAL_ALT(0);
    142 			if (!(bp = strptime(bp, _ctloc(t_fmt_ampm), tm)))
    143 				return (0);
    144 			break;
    145 
    146 		case 'T':	/* The time as "%H:%M:%S". */
    147 			_LEGAL_ALT(0);
    148 			if (!(bp = strptime(bp, "%H:%M:%S", tm)))
    149 				return (0);
    150 			break;
    151 
    152 		case 'X':	/* The time, using the locale's format. */
    153 			_LEGAL_ALT(_ALT_E);
    154 			if (!(bp = strptime(bp, _ctloc(t_fmt), tm)))
    155 				return (0);
    156 			break;
    157 
    158 		case 'x':	/* The date, using the locale's format. */
    159 			_LEGAL_ALT(_ALT_E);
    160 			if (!(bp = strptime(bp, _ctloc(d_fmt), tm)))
    161 				return (0);
    162 			break;
    163 
    164 		/*
    165 		 * "Elementary" conversion rules.
    166 		 */
    167 		case 'A':	/* The day of week, using the locale's form. */
    168 		case 'a':
    169 			_LEGAL_ALT(0);
    170 			for (i = 0; i < 7; i++) {
    171 				/* Full name. */
    172 				len = strlen(_ctloc(day[i]));
    173 				if (strncmp(_ctloc(day[i]), bp, len) == 0)
    174 					break;
    175 
    176 				/* Abbreviated name. */
    177 				len = strlen(_ctloc(abday[i]));
    178 				if (strncmp(_ctloc(abday[i]), bp, len) == 0)
    179 					break;
    180 			}
    181 
    182 			/* Nothing matched. */
    183 			if (i == 7)
    184 				return (0);
    185 
    186 			tm->tm_wday = i;
    187 			bp += len;
    188 			break;
    189 
    190 		case 'B':	/* The month, using the locale's form. */
    191 		case 'b':
    192 		case 'h':
    193 			_LEGAL_ALT(0);
    194 			for (i = 0; i < 12; i++) {
    195 				/* Full name. */
    196 				len = strlen(_ctloc(mon[i]));
    197 				if (strncmp(_ctloc(mon[i]), bp, len) == 0)
    198 					break;
    199 
    200 				/* Abbreviated name. */
    201 				len = strlen(_ctloc(abmon[i]));
    202 				if (strncmp(_ctloc(abmon[i]), bp, len) == 0)
    203 					break;
    204 			}
    205 
    206 			/* Nothing matched. */
    207 			if (i == 12)
    208 				return (0);
    209 
    210 			tm->tm_mon = i;
    211 			bp += len;
    212 			break;
    213 
    214 		case 'C':	/* The century number. */
    215 			_LEGAL_ALT(_ALT_E);
    216 			if (!(_conv_num(&bp, &i, 0, 99)))
    217 				return (0);
    218 
    219 			tm->tm_year = i * 100;
    220 			break;
    221 
    222 		case 'd':	/* The day of month. */
    223 		case 'e':
    224 			_LEGAL_ALT(_ALT_O);
    225 			if (!(_conv_num(&bp, &tm->tm_mday, 1, 31)))
    226 				return (0);
    227 			break;
    228 
    229 		case 'k':	/* The hour (24-hour clock representation). */
    230 			_LEGAL_ALT(0);
    231 			/* FALLTHROUGH */
    232 		case 'H':
    233 			_LEGAL_ALT(_ALT_O);
    234 			if (!(_conv_num(&bp, &tm->tm_hour, 0, 23)))
    235 				return (0);
    236 			break;
    237 
    238 		case 'l':	/* The hour (12-hour clock representation). */
    239 			_LEGAL_ALT(0);
    240 			/* FALLTHROUGH */
    241 		case 'I':
    242 			_LEGAL_ALT(_ALT_O);
    243 			if (!(_conv_num(&bp, &tm->tm_hour, 0, 11)))
    244 				return (0);
    245 			break;
    246 
    247 		case 'j':	/* The day of year. */
    248 			_LEGAL_ALT(0);
    249 			if (!(_conv_num(&bp, &tm->tm_yday, 1, 366)))
    250 				return (0);
    251 			break;
    252 
    253 		case 'M':	/* The minute. */
    254 			_LEGAL_ALT(_ALT_O);
    255 			if (!(_conv_num(&bp, &tm->tm_min, 0, 59)))
    256 				return (0);
    257 			break;
    258 
    259 		case 'm':	/* The month. */
    260 			_LEGAL_ALT(_ALT_O);
    261 			if (!(_conv_num(&bp, &tm->tm_mon, 1, 12)))
    262 				return (0);
    263 			break;
    264 
    265 		case 'p':	/* The locale's equivalent of AM/PM. */
    266 			_LEGAL_ALT(0);
    267 			/* AM? */
    268 			if (strcmp(_ctloc(am_pm[0]), bp) == 0) {
    269 				if (tm->tm_hour > 12)	/* i.e., 13:00 AM ?! */
    270 					return (0);
    271 				else if (tm->tm_hour == 12)
    272 					tm->tm_hour = 0;
    273 
    274 				bp += strlen(_ctloc(am_pm[0]));
    275 				break;
    276 			}
    277 			/* PM? */
    278 			else if (strcmp(_ctloc(am_pm[1]), bp) == 0) {
    279 				if (tm->tm_hour > 12)	/* i.e., 13:00 PM ?! */
    280 					return (0);
    281 				else if (tm->tm_hour < 12)
    282 					tm->tm_hour += 12;
    283 
    284 				bp += strlen(_ctloc(am_pm[1]));
    285 				break;
    286 			}
    287 
    288 			/* Nothing matched. */
    289 			return (0);
    290 
    291 		case 'S':	/* The seconds. */
    292 			_LEGAL_ALT(_ALT_O);
    293 			if (!(_conv_num(&bp, &tm->tm_sec, 1, 61)))
    294 				return (0);
    295 			break;
    296 
    297 		case 'U':	/* The week of year, beginning on sunday. */
    298 		case 'W':	/* The week of year, beginning on monday. */
    299 			_LEGAL_ALT(_ALT_O);
    300 			/*
    301 			 * XXX This is bogus, as we can not assume any valid
    302 			 * information present in the tm structure at this
    303 			 * point to calculate a real value, so just check the
    304 			 * range for now.
    305 			 */
    306 			 if (!(_conv_num(&bp, &i, 0, 53)))
    307 				return (0);
    308 			 break;
    309 
    310 		case 'w':	/* The day of week, beginning on sunday. */
    311 			_LEGAL_ALT(_ALT_O);
    312 			if (!(_conv_num(&bp, &tm->tm_wday, 0, 6)))
    313 				return (0);
    314 			break;
    315 
    316 		case 'Y':	/* The year. */
    317 			_LEGAL_ALT(_ALT_E);
    318 			if (!(_conv_num(&bp, &i, 0, INT_MAX)))
    319 				return (0);
    320 
    321 			tm->tm_year = i - TM_YEAR_BASE;
    322 			break;
    323 
    324 		case 'y':	/* The year within 100 years of the epoch. */
    325 			_LEGAL_ALT(_ALT_E | _ALT_O);
    326 			if (!(_conv_num(&bp, &i, 0, 99)))
    327 				return (0);
    328 
    329 			if (i <= 68)
    330 				tm->tm_year = i + 2000 - TM_YEAR_BASE;
    331 			else
    332 				tm->tm_year = i + 1900 - TM_YEAR_BASE;
    333 			break;
    334 
    335 		/*
    336 		 * Miscellaneous conversions.
    337 		 */
    338 		case 'n':	/* Any kind of white-space. */
    339 		case 't':
    340 			_LEGAL_ALT(0);
    341 			while (isspace(*bp))
    342 				bp++;
    343 			break;
    344 
    345 
    346 		default:	/* Unknown/unsupported conversion. */
    347 			return (0);
    348 		}
    349 
    350 
    351 	}
    352 
    353 	return ((char *)bp);
    354 }
    355 
    356 
    357 static int
    358 _conv_num(buf, dest, llim, ulim)
    359 	const char **buf;
    360 	int *dest;
    361 	int llim, ulim;
    362 {
    363 	*dest = 0;
    364 
    365 	if (**buf < '0' || **buf > '9')
    366 		return (0);
    367 
    368 	do {
    369 		*dest *= 10;
    370 		*dest += *(*buf)++ - '0';
    371 	} while ((*dest * 10 <= ulim) && **buf >= '0' && **buf <= '9');
    372 
    373 	if (*dest < llim || *dest > ulim)
    374 		return (0);
    375 
    376 	return (1);
    377 }
    378