Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.52
      1 /*	$NetBSD: strptime.c,v 1.52 2015/10/30 01:49:36 ginsbach Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 2005, 2008 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 __RCSID("$NetBSD: strptime.c,v 1.52 2015/10/30 01:49:36 ginsbach Exp $");
     35 #endif
     36 
     37 #include "namespace.h"
     38 #include <sys/localedef.h>
     39 #include <sys/types.h>
     40 #include <ctype.h>
     41 #include <locale.h>
     42 #include <string.h>
     43 #include <time.h>
     44 #include <tzfile.h>
     45 #include "private.h"
     46 #include "setlocale_local.h"
     47 
     48 #ifdef __weak_alias
     49 __weak_alias(strptime,_strptime)
     50 __weak_alias(strptime_l, _strptime_l)
     51 #endif
     52 
     53 static const u_char *conv_num(const unsigned char *, int *, uint, uint);
     54 static const u_char *find_string(const u_char *, int *, const char * const *,
     55 	const char * const *, int);
     56 
     57 #define _TIME_LOCALE(loc) \
     58     ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
     59 
     60 /*
     61  * We do not implement alternate representations. However, we always
     62  * check whether a given modifier is allowed for a certain conversion.
     63  */
     64 #define ALT_E			0x01
     65 #define ALT_O			0x02
     66 #define LEGAL_ALT(x)		{ if (alt_format & ~(x)) return NULL; }
     67 
     68 #define S_YEAR			(1 << 0)
     69 #define S_MON			(1 << 1)
     70 #define S_YDAY			(1 << 2)
     71 #define S_MDAY			(1 << 3)
     72 #define S_WDAY			(1 << 4)
     73 #define S_HOUR			(1 << 5)
     74 
     75 #define HAVE_MDAY(s)		(s & S_MDAY)
     76 #define HAVE_MON(s)		(s & S_MON)
     77 #define HAVE_WDAY(s)		(s & S_WDAY)
     78 #define HAVE_YDAY(s)		(s & S_YDAY)
     79 #define HAVE_YEAR(s)		(s & S_YEAR)
     80 #define HAVE_HOUR(s)		(s & S_HOUR)
     81 
     82 static char gmt[] = { "GMT" };
     83 static char utc[] = { "UTC" };
     84 /* RFC-822/RFC-2822 */
     85 static const char * const nast[5] = {
     86        "EST",    "CST",    "MST",    "PST",    "\0\0\0"
     87 };
     88 static const char * const nadt[5] = {
     89        "EDT",    "CDT",    "MDT",    "PDT",    "\0\0\0"
     90 };
     91 
     92 /*
     93  * Table to determine the ordinal date for the start of a month.
     94  * Ref: http://en.wikipedia.org/wiki/ISO_week_date
     95  */
     96 static const int start_of_month[2][13] = {
     97 	/* non-leap year */
     98 	{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
     99 	/* leap year */
    100 	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
    101 };
    102 
    103 /*
    104  * Calculate the week day of the first day of a year. Valid for
    105  * the Gregorian calendar, which began Sept 14, 1752 in the UK
    106  * and its colonies. Ref:
    107  * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
    108  */
    109 
    110 static int
    111 first_wday_of(int yr)
    112 {
    113 	return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) /  4) +
    114 	    (isleap(yr) ? 6 : 0) + 1) % 7;
    115 }
    116 
    117 #define delim(p)	((p) == '\0' || isspace((unsigned char)(p)))
    118 
    119 static int
    120 fromzone(const unsigned char **bp, struct tm *tm)
    121 {
    122 	timezone_t tz;
    123 	char buf[512], *p;
    124 
    125 	for (p = buf; !delim(**bp) && p < &buf[sizeof(buf) - 1]; (*bp)++)
    126 		*p++ = **bp;
    127 	*p = '\0';
    128 
    129 	tz = tzalloc(buf);
    130 	if (tz == NULL)
    131 		return 0;
    132 
    133 	tm->tm_isdst = 0;	/* XXX */
    134 #ifdef TM_GMTOFF
    135 	tm->TM_GMTOFF = tzgetgmtoff(tz, tm->tm_isdst);
    136 #endif
    137 #ifdef TM_ZONE
    138 	// Can't use tzgetname() here because we are going to free()
    139 	tm->TM_ZONE = NULL; /* XXX */
    140 #endif
    141 	tzfree(tz);
    142 	return 1;
    143 }
    144 
    145 char *
    146 strptime(const char *buf, const char *fmt, struct tm *tm)
    147 {
    148 	return strptime_l(buf, fmt, tm, _current_locale());
    149 }
    150 
    151 char *
    152 strptime_l(const char *buf, const char *fmt, struct tm *tm, locale_t loc)
    153 {
    154 	unsigned char c;
    155 	const unsigned char *bp, *ep, *zname;
    156 	int alt_format, i, split_year = 0, neg = 0, state = 0,
    157 	    day_offset = -1, week_offset = 0, offs;
    158 	const char *new_fmt;
    159 
    160 	bp = (const u_char *)buf;
    161 
    162 	while (bp != NULL && (c = *fmt++) != '\0') {
    163 		/* Clear `alternate' modifier prior to new conversion. */
    164 		alt_format = 0;
    165 		i = 0;
    166 
    167 		/* Eat up white-space. */
    168 		if (isspace(c)) {
    169 			while (isspace(*bp))
    170 				bp++;
    171 			continue;
    172 		}
    173 
    174 		if (c != '%')
    175 			goto literal;
    176 
    177 
    178 again:		switch (c = *fmt++) {
    179 		case '%':	/* "%%" is converted to "%". */
    180 literal:
    181 			if (c != *bp++)
    182 				return NULL;
    183 			LEGAL_ALT(0);
    184 			continue;
    185 
    186 		/*
    187 		 * "Alternative" modifiers. Just set the appropriate flag
    188 		 * and start over again.
    189 		 */
    190 		case 'E':	/* "%E?" alternative conversion modifier. */
    191 			LEGAL_ALT(0);
    192 			alt_format |= ALT_E;
    193 			goto again;
    194 
    195 		case 'O':	/* "%O?" alternative conversion modifier. */
    196 			LEGAL_ALT(0);
    197 			alt_format |= ALT_O;
    198 			goto again;
    199 
    200 		/*
    201 		 * "Complex" conversion rules, implemented through recursion.
    202 		 */
    203 		case 'c':	/* Date and time, using the locale's format. */
    204 			new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
    205 			state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
    206 			goto recurse;
    207 
    208 		case 'D':	/* The date as "%m/%d/%y". */
    209 			new_fmt = "%m/%d/%y";
    210 			LEGAL_ALT(0);
    211 			state |= S_MON | S_MDAY | S_YEAR;
    212 			goto recurse;
    213 
    214 		case 'F':	/* The date as "%Y-%m-%d". */
    215 			new_fmt = "%Y-%m-%d";
    216 			LEGAL_ALT(0);
    217 			state |= S_MON | S_MDAY | S_YEAR;
    218 			goto recurse;
    219 
    220 		case 'R':	/* The time as "%H:%M". */
    221 			new_fmt = "%H:%M";
    222 			LEGAL_ALT(0);
    223 			goto recurse;
    224 
    225 		case 'r':	/* The time in 12-hour clock representation. */
    226 			new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
    227 			LEGAL_ALT(0);
    228 			goto recurse;
    229 
    230 		case 'T':	/* The time as "%H:%M:%S". */
    231 			new_fmt = "%H:%M:%S";
    232 			LEGAL_ALT(0);
    233 			goto recurse;
    234 
    235 		case 'X':	/* The time, using the locale's format. */
    236 			new_fmt = _TIME_LOCALE(loc)->t_fmt;
    237 			goto recurse;
    238 
    239 		case 'x':	/* The date, using the locale's format. */
    240 			new_fmt = _TIME_LOCALE(loc)->d_fmt;
    241 			state |= S_MON | S_MDAY | S_YEAR;
    242 		    recurse:
    243 			bp = (const u_char *)strptime((const char *)bp,
    244 							    new_fmt, tm);
    245 			LEGAL_ALT(ALT_E);
    246 			continue;
    247 
    248 		/*
    249 		 * "Elementary" conversion rules.
    250 		 */
    251 		case 'A':	/* The day of week, using the locale's form. */
    252 		case 'a':
    253 			bp = find_string(bp, &tm->tm_wday,
    254 			    _TIME_LOCALE(loc)->day, _TIME_LOCALE(loc)->abday, 7);
    255 			LEGAL_ALT(0);
    256 			state |= S_WDAY;
    257 			continue;
    258 
    259 		case 'B':	/* The month, using the locale's form. */
    260 		case 'b':
    261 		case 'h':
    262 			bp = find_string(bp, &tm->tm_mon,
    263 			    _TIME_LOCALE(loc)->mon, _TIME_LOCALE(loc)->abmon,
    264 			    12);
    265 			LEGAL_ALT(0);
    266 			state |= S_MON;
    267 			continue;
    268 
    269 		case 'C':	/* The century number. */
    270 			i = 20;
    271 			bp = conv_num(bp, &i, 0, 99);
    272 
    273 			i = i * 100 - TM_YEAR_BASE;
    274 			if (split_year)
    275 				i += tm->tm_year % 100;
    276 			split_year = 1;
    277 			tm->tm_year = i;
    278 			LEGAL_ALT(ALT_E);
    279 			state |= S_YEAR;
    280 			continue;
    281 
    282 		case 'd':	/* The day of month. */
    283 		case 'e':
    284 			bp = conv_num(bp, &tm->tm_mday, 1, 31);
    285 			LEGAL_ALT(ALT_O);
    286 			state |= S_MDAY;
    287 			continue;
    288 
    289 		case 'k':	/* The hour (24-hour clock representation). */
    290 			LEGAL_ALT(0);
    291 			/* FALLTHROUGH */
    292 		case 'H':
    293 			bp = conv_num(bp, &tm->tm_hour, 0, 23);
    294 			LEGAL_ALT(ALT_O);
    295 			state |= S_HOUR;
    296 			continue;
    297 
    298 		case 'l':	/* The hour (12-hour clock representation). */
    299 			LEGAL_ALT(0);
    300 			/* FALLTHROUGH */
    301 		case 'I':
    302 			bp = conv_num(bp, &tm->tm_hour, 1, 12);
    303 			if (tm->tm_hour == 12)
    304 				tm->tm_hour = 0;
    305 			LEGAL_ALT(ALT_O);
    306 			state |= S_HOUR;
    307 			continue;
    308 
    309 		case 'j':	/* The day of year. */
    310 			i = 1;
    311 			bp = conv_num(bp, &i, 1, 366);
    312 			tm->tm_yday = i - 1;
    313 			LEGAL_ALT(0);
    314 			state |= S_YDAY;
    315 			continue;
    316 
    317 		case 'M':	/* The minute. */
    318 			bp = conv_num(bp, &tm->tm_min, 0, 59);
    319 			LEGAL_ALT(ALT_O);
    320 			continue;
    321 
    322 		case 'm':	/* The month. */
    323 			i = 1;
    324 			bp = conv_num(bp, &i, 1, 12);
    325 			tm->tm_mon = i - 1;
    326 			LEGAL_ALT(ALT_O);
    327 			state |= S_MON;
    328 			continue;
    329 
    330 		case 'p':	/* The locale's equivalent of AM/PM. */
    331 			bp = find_string(bp, &i, _TIME_LOCALE(loc)->am_pm,
    332 			    NULL, 2);
    333 			if (HAVE_HOUR(state) && tm->tm_hour > 11)
    334 				return NULL;
    335 			tm->tm_hour += i * 12;
    336 			LEGAL_ALT(0);
    337 			continue;
    338 
    339 		case 'S':	/* The seconds. */
    340 			bp = conv_num(bp, &tm->tm_sec, 0, 61);
    341 			LEGAL_ALT(ALT_O);
    342 			continue;
    343 
    344 #ifndef TIME_MAX
    345 #define TIME_MAX	INT64_MAX
    346 #endif
    347 		case 's':	/* seconds since the epoch */
    348 			{
    349 				time_t sse = 0;
    350 				uint64_t rulim = TIME_MAX;
    351 
    352 				if (*bp < '0' || *bp > '9') {
    353 					bp = NULL;
    354 					continue;
    355 				}
    356 
    357 				do {
    358 					sse *= 10;
    359 					sse += *bp++ - '0';
    360 					rulim /= 10;
    361 				} while ((sse * 10 <= TIME_MAX) &&
    362 					 rulim && *bp >= '0' && *bp <= '9');
    363 
    364 				if (sse < 0 || (uint64_t)sse > TIME_MAX) {
    365 					bp = NULL;
    366 					continue;
    367 				}
    368 
    369 				if (localtime_r(&sse, tm) == NULL)
    370 					bp = NULL;
    371 				else
    372 					state |= S_YDAY | S_WDAY |
    373 					    S_MON | S_MDAY | S_YEAR;
    374 			}
    375 			continue;
    376 
    377 		case 'U':	/* The week of year, beginning on sunday. */
    378 		case 'W':	/* The week of year, beginning on monday. */
    379 			/*
    380 			 * XXX This is bogus, as we can not assume any valid
    381 			 * information present in the tm structure at this
    382 			 * point to calculate a real value, so just check the
    383 			 * range for now.
    384 			 */
    385 			bp = conv_num(bp, &i, 0, 53);
    386 			LEGAL_ALT(ALT_O);
    387 			if (c == 'U')
    388 				day_offset = TM_SUNDAY;
    389 			else
    390 				day_offset = TM_MONDAY;
    391 			week_offset = i;
    392 			continue;
    393 
    394 		case 'w':	/* The day of week, beginning on sunday. */
    395 			bp = conv_num(bp, &tm->tm_wday, 0, 6);
    396 			LEGAL_ALT(ALT_O);
    397 			state |= S_WDAY;
    398 			continue;
    399 
    400 		case 'u':	/* The day of week, monday = 1. */
    401 			bp = conv_num(bp, &i, 1, 7);
    402 			tm->tm_wday = i % 7;
    403 			LEGAL_ALT(ALT_O);
    404 			state |= S_WDAY;
    405 			continue;
    406 
    407 		case 'g':	/* The year corresponding to the ISO week
    408 				 * number but without the century.
    409 				 */
    410 			bp = conv_num(bp, &i, 0, 99);
    411 			continue;
    412 
    413 		case 'G':	/* The year corresponding to the ISO week
    414 				 * number with century.
    415 				 */
    416 			do
    417 				bp++;
    418 			while (isdigit(*bp));
    419 			continue;
    420 
    421 		case 'V':	/* The ISO 8601:1988 week number as decimal */
    422 			bp = conv_num(bp, &i, 0, 53);
    423 			continue;
    424 
    425 		case 'Y':	/* The year. */
    426 			i = TM_YEAR_BASE;	/* just for data sanity... */
    427 			bp = conv_num(bp, &i, 0, 9999);
    428 			tm->tm_year = i - TM_YEAR_BASE;
    429 			LEGAL_ALT(ALT_E);
    430 			state |= S_YEAR;
    431 			continue;
    432 
    433 		case 'y':	/* The year within 100 years of the epoch. */
    434 			/* LEGAL_ALT(ALT_E | ALT_O); */
    435 			bp = conv_num(bp, &i, 0, 99);
    436 
    437 			if (split_year)
    438 				/* preserve century */
    439 				i += (tm->tm_year / 100) * 100;
    440 			else {
    441 				split_year = 1;
    442 				if (i <= 68)
    443 					i = i + 2000 - TM_YEAR_BASE;
    444 				else
    445 					i = i + 1900 - TM_YEAR_BASE;
    446 			}
    447 			tm->tm_year = i;
    448 			state |= S_YEAR;
    449 			continue;
    450 
    451 		case 'Z':
    452 			tzset();
    453 			if (strncmp((const char *)bp, gmt, 3) == 0 ||
    454 			    strncmp((const char *)bp, utc, 3) == 0) {
    455 				tm->tm_isdst = 0;
    456 #ifdef TM_GMTOFF
    457 				tm->TM_GMTOFF = 0;
    458 #endif
    459 #ifdef TM_ZONE
    460 				tm->TM_ZONE = gmt;
    461 #endif
    462 				bp += 3;
    463 			} else {
    464 				ep = find_string(bp, &i,
    465 					       	 (const char * const *)tzname,
    466 					       	  NULL, 2);
    467 				if (ep != NULL) {
    468 					tm->tm_isdst = i;
    469 #ifdef TM_GMTOFF
    470 #ifdef USG_COMPAT
    471 					tm->TM_GMTOFF = -timezone;
    472 #else
    473 					tm->TM_GMTOFF = -timezone();
    474 #endif
    475 #endif
    476 #ifdef TM_ZONE
    477 					tm->TM_ZONE = tzname[i];
    478 					bp = ep;
    479 #endif
    480 				} else
    481 					(void)fromzone(&bp, tm);
    482 			}
    483 			continue;
    484 
    485 		case 'z':
    486 			/*
    487 			 * We recognize all ISO 8601 formats:
    488 			 * Z	= Zulu time/UTC
    489 			 * [+-]hhmm
    490 			 * [+-]hh:mm
    491 			 * [+-]hh
    492 			 * We recognize all RFC-822/RFC-2822 formats:
    493 			 * UT|GMT
    494 			 *          North American : UTC offsets
    495 			 * E[DS]T = Eastern : -4 | -5
    496 			 * C[DS]T = Central : -5 | -6
    497 			 * M[DS]T = Mountain: -6 | -7
    498 			 * P[DS]T = Pacific : -7 | -8
    499 			 *          Military
    500 			 * [A-IL-M] = -1 ... -9 (J not used)
    501 			 * [N-Y]  = +1 ... +12
    502 			 */
    503 			while (isspace(*bp))
    504 				bp++;
    505 
    506 			zname = bp;
    507 			switch (*bp++) {
    508 			case 'G':
    509 				if (*bp++ != 'M')
    510 					goto namedzone;
    511 				/*FALLTHROUGH*/
    512 			case 'U':
    513 				if (*bp++ != 'T')
    514 					goto namedzone;
    515 				else if (!delim(*bp) && *bp++ != 'C')
    516 					goto namedzone;
    517 				/*FALLTHROUGH*/
    518 			case 'Z':
    519 				if (!delim(*bp))
    520 					goto namedzone;
    521 				tm->tm_isdst = 0;
    522 #ifdef TM_GMTOFF
    523 				tm->TM_GMTOFF = 0;
    524 #endif
    525 #ifdef TM_ZONE
    526 				tm->TM_ZONE = utc;
    527 #endif
    528 				continue;
    529 			case '+':
    530 				neg = 0;
    531 				break;
    532 			case '-':
    533 				neg = 1;
    534 				break;
    535 			default:
    536 namedzone:
    537 				bp = zname;
    538 
    539 				/* Military style */
    540 				if (delim(bp[1]) &&
    541 				    ((*bp >= 'A' && *bp <= 'I') ||
    542 				    (*bp >= 'L' && *bp <= 'Y'))) {
    543 #ifdef TM_GMTOFF
    544 					/* Argh! No 'J'! */
    545 					if (*bp >= 'A' && *bp <= 'I')
    546 						tm->TM_GMTOFF =
    547 						    ('A' - 1) - (int)*bp;
    548 					else if (*bp >= 'L' && *bp <= 'M')
    549 						tm->TM_GMTOFF = 'A' - (int)*bp;
    550 					else if (*bp >= 'N' && *bp <= 'Y')
    551 						tm->TM_GMTOFF = (int)*bp - 'M';
    552 					tm->TM_GMTOFF *= SECSPERHOUR;
    553 #endif
    554 #ifdef TM_ZONE
    555 					tm->TM_ZONE = NULL; /* XXX */
    556 #endif
    557 					bp++;
    558 					continue;
    559 				}
    560 
    561 				/*
    562 				 * From our 3 letter hard-coded table
    563 				 * XXX: Can be removed, handled by tzload()
    564 				 */
    565 				if (delim(bp[0]) || delim(bp[1]) ||
    566 				    delim(bp[2]) || !delim(bp[3]))
    567 					goto loadzone;
    568 				ep = find_string(bp, &i, nast, NULL, 4);
    569 				if (ep != NULL) {
    570 #ifdef TM_GMTOFF
    571 					tm->TM_GMTOFF = (-5 - i) * SECSPERHOUR;
    572 #endif
    573 #ifdef TM_ZONE
    574 					tm->TM_ZONE = __UNCONST(nast[i]);
    575 #endif
    576 					bp = ep;
    577 					continue;
    578 				}
    579 				ep = find_string(bp, &i, nadt, NULL, 4);
    580 				if (ep != NULL) {
    581 					tm->tm_isdst = 1;
    582 #ifdef TM_GMTOFF
    583 					tm->TM_GMTOFF = (-4 - i) * SECSPERHOUR;
    584 #endif
    585 #ifdef TM_ZONE
    586 					tm->TM_ZONE = __UNCONST(nadt[i]);
    587 #endif
    588 					bp = ep;
    589 					continue;
    590 				}
    591 
    592 loadzone:
    593 				/*
    594 				 * The hard way, load the zone!
    595 				 */
    596 				if (fromzone(&bp, tm))
    597 					continue;
    598 				return NULL;
    599 			}
    600 			offs = 0;
    601 			for (i = 0; i < 4; ) {
    602 				if (isdigit(*bp)) {
    603 					offs = offs * 10 + (*bp++ - '0');
    604 					i++;
    605 					continue;
    606 				}
    607 				if (i == 2 && *bp == ':') {
    608 					bp++;
    609 					continue;
    610 				}
    611 				break;
    612 			}
    613 			if (isdigit(*bp))
    614 				return NULL;
    615 			switch (i) {
    616 			case 2:
    617 				offs *= SECSPERHOUR;
    618 				break;
    619 			case 4:
    620 				i = offs % 100;
    621 				if (i >= SECSPERMIN)
    622 					return NULL;
    623 				/* Convert minutes into decimal */
    624 				offs = (offs / 100) * SECSPERHOUR + i * SECSPERMIN;
    625 				break;
    626 			default:
    627 				return NULL;
    628 			}
    629 			if (offs > (12 * SECSPERHOUR))
    630 				return NULL;
    631 			if (neg)
    632 				offs = -offs;
    633 			tm->tm_isdst = 0;	/* XXX */
    634 #ifdef TM_GMTOFF
    635 			tm->TM_GMTOFF = offs;
    636 #endif
    637 #ifdef TM_ZONE
    638 			tm->TM_ZONE = NULL;	/* XXX */
    639 #endif
    640 			continue;
    641 
    642 		/*
    643 		 * Miscellaneous conversions.
    644 		 */
    645 		case 'n':	/* Any kind of white-space. */
    646 		case 't':
    647 			while (isspace(*bp))
    648 				bp++;
    649 			LEGAL_ALT(0);
    650 			continue;
    651 
    652 
    653 		default:	/* Unknown/unsupported conversion. */
    654 			return NULL;
    655 		}
    656 	}
    657 
    658 	if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
    659 		if (HAVE_MON(state) && HAVE_MDAY(state)) {
    660 			/* calculate day of year (ordinal date) */
    661 			tm->tm_yday =  start_of_month[isleap_sum(tm->tm_year,
    662 			    TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
    663 			state |= S_YDAY;
    664 		} else if (day_offset != -1) {
    665 			/*
    666 			 * Set the date to the first Sunday (or Monday)
    667 			 * of the specified week of the year.
    668 			 */
    669 			if (!HAVE_WDAY(state)) {
    670 				tm->tm_wday = day_offset;
    671 				state |= S_WDAY;
    672 			}
    673 			tm->tm_yday = (7 -
    674 			    first_wday_of(tm->tm_year + TM_YEAR_BASE) +
    675 			    day_offset) % 7 + (week_offset - 1) * 7 +
    676 			    tm->tm_wday  - day_offset;
    677 			state |= S_YDAY;
    678 		}
    679 	}
    680 
    681 	if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
    682 		int isleap;
    683 
    684 		if (!HAVE_MON(state)) {
    685 			/* calculate month of day of year */
    686 			i = 0;
    687 			isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
    688 			while (tm->tm_yday >= start_of_month[isleap][i])
    689 				i++;
    690 			if (i > 12) {
    691 				i = 1;
    692 				tm->tm_yday -= start_of_month[isleap][12];
    693 				tm->tm_year++;
    694 			}
    695 			tm->tm_mon = i - 1;
    696 			state |= S_MON;
    697 		}
    698 
    699 		if (!HAVE_MDAY(state)) {
    700 			/* calculate day of month */
    701 			isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
    702 			tm->tm_mday = tm->tm_yday -
    703 			    start_of_month[isleap][tm->tm_mon] + 1;
    704 			state |= S_MDAY;
    705 		}
    706 
    707 		if (!HAVE_WDAY(state)) {
    708 			/* calculate day of week */
    709 			i = 0;
    710 			week_offset = first_wday_of(tm->tm_year);
    711 			while (i++ <= tm->tm_yday) {
    712 				if (week_offset++ >= 6)
    713 					week_offset = 0;
    714 			}
    715 			tm->tm_wday = week_offset;
    716 			state |= S_WDAY;
    717 		}
    718 	}
    719 
    720 	return __UNCONST(bp);
    721 }
    722 
    723 
    724 static const u_char *
    725 conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
    726 {
    727 	uint result = 0;
    728 	unsigned char ch;
    729 
    730 	/* The limit also determines the number of valid digits. */
    731 	uint rulim = ulim;
    732 
    733 	ch = *buf;
    734 	if (ch < '0' || ch > '9')
    735 		return NULL;
    736 
    737 	do {
    738 		result *= 10;
    739 		result += ch - '0';
    740 		rulim /= 10;
    741 		ch = *++buf;
    742 	} while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
    743 
    744 	if (result < llim || result > ulim)
    745 		return NULL;
    746 
    747 	*dest = result;
    748 	return buf;
    749 }
    750 
    751 static const u_char *
    752 find_string(const u_char *bp, int *tgt, const char * const *n1,
    753 		const char * const *n2, int c)
    754 {
    755 	int i;
    756 	size_t len;
    757 
    758 	/* check full name - then abbreviated ones */
    759 	for (; n1 != NULL; n1 = n2, n2 = NULL) {
    760 		for (i = 0; i < c; i++, n1++) {
    761 			len = strlen(*n1);
    762 			if (strncasecmp(*n1, (const char *)bp, len) == 0) {
    763 				*tgt = i;
    764 				return bp + len;
    765 			}
    766 		}
    767 	}
    768 
    769 	/* Nothing matched */
    770 	return NULL;
    771 }
    772