Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.32
      1  1.32  ginsbach /*	$NetBSD: strptime.c,v 1.32 2009/05/01 20:15:05 ginsbach Exp $	*/
      2   1.1       mrg 
      3   1.3    kleink /*-
      4  1.27  ginsbach  * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
      5   1.3    kleink  * All rights reserved.
      6   1.3    kleink  *
      7   1.3    kleink  * This code was contributed to The NetBSD Foundation by Klaus Klein.
      8  1.24       dsl  * Heavily optimised by David Laight
      9   1.1       mrg  *
     10   1.3    kleink  * Redistribution and use in source and binary forms, with or without
     11   1.1       mrg  * modification, are permitted provided that the following conditions
     12   1.1       mrg  * are met:
     13   1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     14   1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     15   1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.3    kleink  *    notice, this list of conditions and the following disclaimer in the
     17   1.3    kleink  *    documentation and/or other materials provided with the distribution.
     18   1.1       mrg  *
     19   1.3    kleink  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.3    kleink  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.3    kleink  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.3    kleink  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.3    kleink  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.3    kleink  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.3    kleink  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.3    kleink  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.3    kleink  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.3    kleink  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1       mrg  */
     31   1.1       mrg 
     32   1.6  christos #include <sys/cdefs.h>
     33   1.3    kleink #if defined(LIBC_SCCS) && !defined(lint)
     34  1.32  ginsbach __RCSID("$NetBSD: strptime.c,v 1.32 2009/05/01 20:15:05 ginsbach Exp $");
     35   1.1       mrg #endif
     36   1.1       mrg 
     37   1.7       jtc #include "namespace.h"
     38   1.3    kleink #include <sys/localedef.h>
     39   1.3    kleink #include <ctype.h>
     40   1.1       mrg #include <locale.h>
     41   1.3    kleink #include <string.h>
     42   1.1       mrg #include <time.h>
     43  1.12   mycroft #include <tzfile.h>
     44  1.29  christos #include "private.h"
     45   1.7       jtc 
     46   1.7       jtc #ifdef __weak_alias
     47  1.19   mycroft __weak_alias(strptime,_strptime)
     48   1.7       jtc #endif
     49   1.3    kleink 
     50  1.21       cgd #define	_ctloc(x)		(_CurrentTimeLocale->x)
     51   1.3    kleink 
     52   1.3    kleink /*
     53   1.3    kleink  * We do not implement alternate representations. However, we always
     54   1.3    kleink  * check whether a given modifier is allowed for a certain conversion.
     55   1.3    kleink  */
     56  1.16    kleink #define ALT_E			0x01
     57  1.16    kleink #define ALT_O			0x02
     58  1.24       dsl #define	LEGAL_ALT(x)		{ if (alt_format & ~(x)) return NULL; }
     59   1.3    kleink 
     60  1.29  christos static char gmt[] = { "GMT" };
     61  1.29  christos static char utc[] = { "UTC" };
     62  1.32  ginsbach /* RFC-822/RFC-2822 */
     63  1.32  ginsbach static const char * const nast[5] = {
     64  1.32  ginsbach        "EST",    "CST",    "MST",    "PST",    "\0\0\0"
     65  1.32  ginsbach };
     66  1.32  ginsbach static const char * const nadt[5] = {
     67  1.32  ginsbach        "EDT",    "CDT",    "MDT",    "PDT",    "\0\0\0"
     68  1.32  ginsbach };
     69   1.3    kleink 
     70  1.24       dsl static const u_char *conv_num(const unsigned char *, int *, uint, uint);
     71  1.23       dsl static const u_char *find_string(const u_char *, int *, const char * const *,
     72  1.23       dsl 	const char * const *, int);
     73   1.3    kleink 
     74   1.1       mrg 
     75   1.2    kleink char *
     76  1.23       dsl strptime(const char *buf, const char *fmt, struct tm *tm)
     77   1.1       mrg {
     78  1.20     itohy 	unsigned char c;
     79  1.32  ginsbach 	const unsigned char *bp, *ep;
     80  1.32  ginsbach 	int alt_format, i, split_year = 0, neg = 0, offs;
     81  1.23       dsl 	const char *new_fmt;
     82   1.3    kleink 
     83  1.22  christos 	bp = (const u_char *)buf;
     84   1.3    kleink 
     85  1.24       dsl 	while (bp != NULL && (c = *fmt++) != '\0') {
     86   1.3    kleink 		/* Clear `alternate' modifier prior to new conversion. */
     87   1.3    kleink 		alt_format = 0;
     88  1.23       dsl 		i = 0;
     89   1.3    kleink 
     90   1.3    kleink 		/* Eat up white-space. */
     91   1.3    kleink 		if (isspace(c)) {
     92   1.3    kleink 			while (isspace(*bp))
     93   1.3    kleink 				bp++;
     94   1.2    kleink 			continue;
     95   1.2    kleink 		}
     96  1.23       dsl 
     97  1.24       dsl 		if (c != '%')
     98   1.3    kleink 			goto literal;
     99   1.3    kleink 
    100   1.3    kleink 
    101   1.3    kleink again:		switch (c = *fmt++) {
    102   1.3    kleink 		case '%':	/* "%%" is converted to "%". */
    103   1.3    kleink literal:
    104  1.14        tv 			if (c != *bp++)
    105  1.24       dsl 				return NULL;
    106  1.23       dsl 			LEGAL_ALT(0);
    107  1.23       dsl 			continue;
    108   1.3    kleink 
    109   1.3    kleink 		/*
    110   1.3    kleink 		 * "Alternative" modifiers. Just set the appropriate flag
    111   1.3    kleink 		 * and start over again.
    112   1.3    kleink 		 */
    113   1.3    kleink 		case 'E':	/* "%E?" alternative conversion modifier. */
    114  1.16    kleink 			LEGAL_ALT(0);
    115  1.16    kleink 			alt_format |= ALT_E;
    116   1.3    kleink 			goto again;
    117   1.3    kleink 
    118   1.3    kleink 		case 'O':	/* "%O?" alternative conversion modifier. */
    119  1.16    kleink 			LEGAL_ALT(0);
    120  1.16    kleink 			alt_format |= ALT_O;
    121   1.3    kleink 			goto again;
    122  1.23       dsl 
    123   1.3    kleink 		/*
    124   1.3    kleink 		 * "Complex" conversion rules, implemented through recursion.
    125   1.3    kleink 		 */
    126   1.3    kleink 		case 'c':	/* Date and time, using the locale's format. */
    127  1.23       dsl 			new_fmt = _ctloc(d_t_fmt);
    128  1.23       dsl 			goto recurse;
    129   1.3    kleink 
    130   1.3    kleink 		case 'D':	/* The date as "%m/%d/%y". */
    131  1.23       dsl 			new_fmt = "%m/%d/%y";
    132  1.16    kleink 			LEGAL_ALT(0);
    133  1.23       dsl 			goto recurse;
    134  1.18        tv 
    135  1.27  ginsbach 		case 'F':	/* The date as "%Y-%m-%d". */
    136  1.27  ginsbach 			new_fmt = "%Y-%m-%d";
    137  1.27  ginsbach 			LEGAL_ALT(0);
    138  1.27  ginsbach 			goto recurse;
    139  1.27  ginsbach 
    140   1.3    kleink 		case 'R':	/* The time as "%H:%M". */
    141  1.23       dsl 			new_fmt = "%H:%M";
    142  1.16    kleink 			LEGAL_ALT(0);
    143  1.23       dsl 			goto recurse;
    144   1.3    kleink 
    145   1.3    kleink 		case 'r':	/* The time in 12-hour clock representation. */
    146  1.23       dsl 			new_fmt =_ctloc(t_fmt_ampm);
    147  1.16    kleink 			LEGAL_ALT(0);
    148  1.23       dsl 			goto recurse;
    149   1.3    kleink 
    150   1.3    kleink 		case 'T':	/* The time as "%H:%M:%S". */
    151  1.23       dsl 			new_fmt = "%H:%M:%S";
    152  1.16    kleink 			LEGAL_ALT(0);
    153  1.23       dsl 			goto recurse;
    154   1.3    kleink 
    155   1.3    kleink 		case 'X':	/* The time, using the locale's format. */
    156  1.23       dsl 			new_fmt =_ctloc(t_fmt);
    157  1.23       dsl 			goto recurse;
    158   1.3    kleink 
    159   1.3    kleink 		case 'x':	/* The date, using the locale's format. */
    160  1.23       dsl 			new_fmt =_ctloc(d_fmt);
    161  1.23       dsl 		    recurse:
    162  1.23       dsl 			bp = (const u_char *)strptime((const char *)bp,
    163  1.23       dsl 							    new_fmt, tm);
    164  1.16    kleink 			LEGAL_ALT(ALT_E);
    165  1.23       dsl 			continue;
    166   1.3    kleink 
    167   1.3    kleink 		/*
    168   1.3    kleink 		 * "Elementary" conversion rules.
    169   1.3    kleink 		 */
    170   1.3    kleink 		case 'A':	/* The day of week, using the locale's form. */
    171   1.3    kleink 		case 'a':
    172  1.23       dsl 			bp = find_string(bp, &tm->tm_wday, _ctloc(day),
    173  1.23       dsl 					_ctloc(abday), 7);
    174  1.16    kleink 			LEGAL_ALT(0);
    175  1.23       dsl 			continue;
    176   1.2    kleink 
    177   1.3    kleink 		case 'B':	/* The month, using the locale's form. */
    178   1.3    kleink 		case 'b':
    179   1.3    kleink 		case 'h':
    180  1.23       dsl 			bp = find_string(bp, &tm->tm_mon, _ctloc(mon),
    181  1.23       dsl 					_ctloc(abmon), 12);
    182  1.16    kleink 			LEGAL_ALT(0);
    183  1.23       dsl 			continue;
    184   1.2    kleink 
    185   1.3    kleink 		case 'C':	/* The century number. */
    186  1.24       dsl 			i = 20;
    187  1.23       dsl 			bp = conv_num(bp, &i, 0, 99);
    188   1.2    kleink 
    189  1.24       dsl 			i = i * 100 - TM_YEAR_BASE;
    190  1.24       dsl 			if (split_year)
    191  1.24       dsl 				i += tm->tm_year % 100;
    192  1.24       dsl 			split_year = 1;
    193  1.24       dsl 			tm->tm_year = i;
    194  1.23       dsl 			LEGAL_ALT(ALT_E);
    195  1.23       dsl 			continue;
    196   1.2    kleink 
    197   1.3    kleink 		case 'd':	/* The day of month. */
    198   1.3    kleink 		case 'e':
    199  1.23       dsl 			bp = conv_num(bp, &tm->tm_mday, 1, 31);
    200  1.16    kleink 			LEGAL_ALT(ALT_O);
    201  1.23       dsl 			continue;
    202   1.2    kleink 
    203   1.3    kleink 		case 'k':	/* The hour (24-hour clock representation). */
    204  1.16    kleink 			LEGAL_ALT(0);
    205   1.3    kleink 			/* FALLTHROUGH */
    206   1.3    kleink 		case 'H':
    207  1.23       dsl 			bp = conv_num(bp, &tm->tm_hour, 0, 23);
    208  1.16    kleink 			LEGAL_ALT(ALT_O);
    209  1.23       dsl 			continue;
    210   1.2    kleink 
    211   1.3    kleink 		case 'l':	/* The hour (12-hour clock representation). */
    212  1.16    kleink 			LEGAL_ALT(0);
    213   1.3    kleink 			/* FALLTHROUGH */
    214   1.3    kleink 		case 'I':
    215  1.23       dsl 			bp = conv_num(bp, &tm->tm_hour, 1, 12);
    216  1.13        tv 			if (tm->tm_hour == 12)
    217  1.13        tv 				tm->tm_hour = 0;
    218  1.23       dsl 			LEGAL_ALT(ALT_O);
    219  1.23       dsl 			continue;
    220   1.2    kleink 
    221   1.3    kleink 		case 'j':	/* The day of year. */
    222  1.23       dsl 			i = 1;
    223  1.23       dsl 			bp = conv_num(bp, &i, 1, 366);
    224  1.23       dsl 			tm->tm_yday = i - 1;
    225  1.16    kleink 			LEGAL_ALT(0);
    226  1.23       dsl 			continue;
    227   1.2    kleink 
    228   1.3    kleink 		case 'M':	/* The minute. */
    229  1.23       dsl 			bp = conv_num(bp, &tm->tm_min, 0, 59);
    230  1.16    kleink 			LEGAL_ALT(ALT_O);
    231  1.23       dsl 			continue;
    232   1.2    kleink 
    233   1.3    kleink 		case 'm':	/* The month. */
    234  1.23       dsl 			i = 1;
    235  1.23       dsl 			bp = conv_num(bp, &i, 1, 12);
    236  1.23       dsl 			tm->tm_mon = i - 1;
    237  1.16    kleink 			LEGAL_ALT(ALT_O);
    238  1.23       dsl 			continue;
    239   1.2    kleink 
    240   1.3    kleink 		case 'p':	/* The locale's equivalent of AM/PM. */
    241  1.24       dsl 			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
    242  1.23       dsl 			if (tm->tm_hour > 11)
    243  1.24       dsl 				return NULL;
    244  1.23       dsl 			tm->tm_hour += i * 12;
    245  1.16    kleink 			LEGAL_ALT(0);
    246  1.23       dsl 			continue;
    247   1.2    kleink 
    248   1.3    kleink 		case 'S':	/* The seconds. */
    249  1.23       dsl 			bp = conv_num(bp, &tm->tm_sec, 0, 61);
    250  1.16    kleink 			LEGAL_ALT(ALT_O);
    251  1.23       dsl 			continue;
    252   1.1       mrg 
    253   1.3    kleink 		case 'U':	/* The week of year, beginning on sunday. */
    254   1.3    kleink 		case 'W':	/* The week of year, beginning on monday. */
    255   1.3    kleink 			/*
    256   1.3    kleink 			 * XXX This is bogus, as we can not assume any valid
    257   1.3    kleink 			 * information present in the tm structure at this
    258   1.3    kleink 			 * point to calculate a real value, so just check the
    259   1.3    kleink 			 * range for now.
    260   1.3    kleink 			 */
    261  1.23       dsl 			 bp = conv_num(bp, &i, 0, 53);
    262  1.23       dsl 			 LEGAL_ALT(ALT_O);
    263  1.23       dsl 			 continue;
    264   1.2    kleink 
    265   1.3    kleink 		case 'w':	/* The day of week, beginning on sunday. */
    266  1.23       dsl 			bp = conv_num(bp, &tm->tm_wday, 0, 6);
    267  1.16    kleink 			LEGAL_ALT(ALT_O);
    268  1.23       dsl 			continue;
    269   1.2    kleink 
    270  1.29  christos 		case 'u':	/* The day of week, monday = 1. */
    271  1.29  christos 			bp = conv_num(bp, &i, 1, 7);
    272  1.29  christos 			tm->tm_wday = i % 7;
    273  1.29  christos 			LEGAL_ALT(ALT_O);
    274  1.29  christos 			continue;
    275  1.29  christos 
    276  1.29  christos 		case 'g':	/* The year corresponding to the ISO week
    277  1.29  christos 				 * number but without the century.
    278  1.29  christos 				 */
    279  1.29  christos 			bp = conv_num(bp, &i, 0, 99);
    280  1.29  christos 			continue;
    281  1.29  christos 
    282  1.29  christos 		case 'G':	/* The year corresponding to the ISO week
    283  1.29  christos 				 * number with century.
    284  1.29  christos 				 */
    285  1.29  christos 			do
    286  1.30  christos 				bp++;
    287  1.29  christos 			while (isdigit(*bp));
    288  1.29  christos 			continue;
    289  1.29  christos 
    290  1.29  christos 		case 'V':	/* The ISO 8601:1988 week number as decimal */
    291  1.29  christos 			bp = conv_num(bp, &i, 0, 53);
    292  1.29  christos 			continue;
    293  1.29  christos 
    294   1.3    kleink 		case 'Y':	/* The year. */
    295  1.24       dsl 			i = TM_YEAR_BASE;	/* just for data sanity... */
    296  1.23       dsl 			bp = conv_num(bp, &i, 0, 9999);
    297   1.8   mycroft 			tm->tm_year = i - TM_YEAR_BASE;
    298  1.23       dsl 			LEGAL_ALT(ALT_E);
    299  1.23       dsl 			continue;
    300   1.2    kleink 
    301   1.9   mycroft 		case 'y':	/* The year within 100 years of the epoch. */
    302  1.24       dsl 			/* LEGAL_ALT(ALT_E | ALT_O); */
    303  1.23       dsl 			bp = conv_num(bp, &i, 0, 99);
    304   1.8   mycroft 
    305  1.24       dsl 			if (split_year)
    306  1.24       dsl 				/* preserve century */
    307  1.24       dsl 				i += (tm->tm_year / 100) * 100;
    308  1.24       dsl 			else {
    309  1.24       dsl 				split_year = 1;
    310  1.24       dsl 				if (i <= 68)
    311  1.24       dsl 					i = i + 2000 - TM_YEAR_BASE;
    312  1.24       dsl 				else
    313  1.24       dsl 					i = i + 1900 - TM_YEAR_BASE;
    314  1.13        tv 			}
    315  1.24       dsl 			tm->tm_year = i;
    316  1.23       dsl 			continue;
    317   1.2    kleink 
    318  1.26  ginsbach 		case 'Z':
    319  1.26  ginsbach 			tzset();
    320  1.26  ginsbach 			if (strncmp((const char *)bp, gmt, 3) == 0) {
    321  1.26  ginsbach 				tm->tm_isdst = 0;
    322  1.26  ginsbach #ifdef TM_GMTOFF
    323  1.26  ginsbach 				tm->TM_GMTOFF = 0;
    324  1.26  ginsbach #endif
    325  1.26  ginsbach #ifdef TM_ZONE
    326  1.26  ginsbach 				tm->TM_ZONE = gmt;
    327  1.26  ginsbach #endif
    328  1.26  ginsbach 				bp += 3;
    329  1.26  ginsbach 			} else {
    330  1.26  ginsbach 				ep = find_string(bp, &i,
    331  1.26  ginsbach 					       	 (const char * const *)tzname,
    332  1.26  ginsbach 					       	  NULL, 2);
    333  1.26  ginsbach 				if (ep != NULL) {
    334  1.26  ginsbach 					tm->tm_isdst = i;
    335  1.26  ginsbach #ifdef TM_GMTOFF
    336  1.26  ginsbach 					tm->TM_GMTOFF = -(timezone);
    337  1.26  ginsbach #endif
    338  1.26  ginsbach #ifdef TM_ZONE
    339  1.26  ginsbach 					tm->TM_ZONE = tzname[i];
    340  1.26  ginsbach #endif
    341  1.26  ginsbach 				}
    342  1.26  ginsbach 				bp = ep;
    343  1.26  ginsbach 			}
    344  1.26  ginsbach 			continue;
    345  1.26  ginsbach 
    346  1.29  christos 		case 'z':
    347  1.29  christos 			/*
    348  1.29  christos 			 * We recognize all ISO 8601 formats:
    349  1.29  christos 			 * Z	= Zulu time/UTC
    350  1.29  christos 			 * [+-]hhmm
    351  1.29  christos 			 * [+-]hh:mm
    352  1.29  christos 			 * [+-]hh
    353  1.32  ginsbach 			 * We recognize all RFC-822/RFC-2822 formats:
    354  1.32  ginsbach 			 * UT|GMT
    355  1.32  ginsbach 			 *          North American : UTC offsets
    356  1.32  ginsbach 			 * E[DS]T = Eastern : -4 | -5
    357  1.32  ginsbach 			 * C[DS]T = Central : -5 | -6
    358  1.32  ginsbach 			 * M[DS]T = Mountain: -6 | -7
    359  1.32  ginsbach 			 * P[DS]T = Pacific : -7 | -8
    360  1.32  ginsbach 			 *          Military
    361  1.32  ginsbach 			 * [A-IL-M] = -1 ... -9 (J not used)
    362  1.32  ginsbach 			 * [N-Y]  = +1 ... +12
    363  1.29  christos 			 */
    364  1.29  christos 			while (isspace(*bp))
    365  1.29  christos 				bp++;
    366  1.29  christos 
    367  1.29  christos 			switch (*bp++) {
    368  1.32  ginsbach 			case 'G':
    369  1.32  ginsbach 				if (*bp++ != 'M')
    370  1.32  ginsbach 					return NULL;
    371  1.32  ginsbach 				/*FALLTHROUGH*/
    372  1.32  ginsbach 			case 'U':
    373  1.32  ginsbach 				if (*bp++ != 'T')
    374  1.32  ginsbach 					return NULL;
    375  1.32  ginsbach 				/*FALLTHROUGH*/
    376  1.29  christos 			case 'Z':
    377  1.29  christos 				tm->tm_isdst = 0;
    378  1.29  christos #ifdef TM_GMTOFF
    379  1.29  christos 				tm->TM_GMTOFF = 0;
    380  1.29  christos #endif
    381  1.29  christos #ifdef TM_ZONE
    382  1.29  christos 				tm->TM_ZONE = utc;
    383  1.29  christos #endif
    384  1.29  christos 				continue;
    385  1.29  christos 			case '+':
    386  1.29  christos 				neg = 0;
    387  1.29  christos 				break;
    388  1.29  christos 			case '-':
    389  1.29  christos 				neg = 1;
    390  1.29  christos 				break;
    391  1.29  christos 			default:
    392  1.32  ginsbach 				--bp;
    393  1.32  ginsbach 				ep = find_string(bp, &i, nast, NULL, 4);
    394  1.32  ginsbach 				if (ep != NULL) {
    395  1.32  ginsbach #ifdef TM_GMTOFF
    396  1.32  ginsbach 					tm->TM_GMTOFF = -5 - i;
    397  1.32  ginsbach #endif
    398  1.32  ginsbach #ifdef TM_ZONE
    399  1.32  ginsbach 					tm->TM_ZONE = __UNCONST(nast[i]);
    400  1.32  ginsbach #endif
    401  1.32  ginsbach 					bp = ep;
    402  1.32  ginsbach 					continue;
    403  1.32  ginsbach 				}
    404  1.32  ginsbach 				ep = find_string(bp, &i, nadt, NULL, 4);
    405  1.32  ginsbach 				if (ep != NULL) {
    406  1.32  ginsbach 					tm->tm_isdst = 1;
    407  1.32  ginsbach #ifdef TM_GMTOFF
    408  1.32  ginsbach 					tm->TM_GMTOFF = -4 - i;
    409  1.32  ginsbach #endif
    410  1.32  ginsbach #ifdef TM_ZONE
    411  1.32  ginsbach 					tm->TM_ZONE = __UNCONST(nadt[i]);
    412  1.32  ginsbach #endif
    413  1.32  ginsbach 					bp = ep;
    414  1.32  ginsbach 					continue;
    415  1.32  ginsbach 				}
    416  1.32  ginsbach 
    417  1.32  ginsbach 				if ((*bp >= 'A' && *bp <= 'I') ||
    418  1.32  ginsbach 				    (*bp >= 'L' && *bp <= 'Y')) {
    419  1.32  ginsbach #ifdef TM_GMTOFF
    420  1.32  ginsbach 					/* Argh! No 'J'! */
    421  1.32  ginsbach 					if (*bp >= 'A' && *bp <= 'I')
    422  1.32  ginsbach 						tm->TM_GMTOFF =
    423  1.32  ginsbach 						    ('A' - 1) - (int)*bp;
    424  1.32  ginsbach 					else if (*bp >= 'L' && *bp <= 'M')
    425  1.32  ginsbach 						tm->TM_GMTOFF = 'A' - (int)*bp;
    426  1.32  ginsbach 					else if (*bp >= 'N' && *bp <= 'Y')
    427  1.32  ginsbach 						tm->TM_GMTOFF = (int)*bp - 'M';
    428  1.32  ginsbach #endif
    429  1.32  ginsbach #ifdef TM_ZONE
    430  1.32  ginsbach 					tm->TM_ZONE = NULL; /* XXX */
    431  1.32  ginsbach #endif
    432  1.32  ginsbach 					bp++;
    433  1.32  ginsbach 					continue;
    434  1.32  ginsbach 				}
    435  1.29  christos 				return NULL;
    436  1.29  christos 			}
    437  1.29  christos 			offs = 0;
    438  1.29  christos 			for (i = 0; i < 4; ) {
    439  1.29  christos 				if (isdigit(*bp)) {
    440  1.29  christos 					offs = offs * 10 + (*bp++ - '0');
    441  1.29  christos 					i++;
    442  1.29  christos 					continue;
    443  1.29  christos 				}
    444  1.29  christos 				if (i == 2 && *bp == ':') {
    445  1.29  christos 					bp++;
    446  1.29  christos 					continue;
    447  1.29  christos 				}
    448  1.29  christos 				break;
    449  1.29  christos 			}
    450  1.29  christos 			switch (i) {
    451  1.29  christos 			case 2:
    452  1.29  christos 				offs *= 100;
    453  1.29  christos 				break;
    454  1.29  christos 			case 4:
    455  1.29  christos 				i = offs % 100;
    456  1.29  christos 				if (i >= 60)
    457  1.29  christos 					return NULL;
    458  1.29  christos 				/* Convert minutes into decimal */
    459  1.29  christos 				offs = (offs / 100) * 100 + (i * 50) / 30;
    460  1.29  christos 				break;
    461  1.29  christos 			default:
    462  1.29  christos 				return NULL;
    463  1.29  christos 			}
    464  1.31  christos 			if (neg)
    465  1.31  christos 				offs = -offs;
    466  1.29  christos 			tm->tm_isdst = 0;	/* XXX */
    467  1.29  christos #ifdef TM_GMTOFF
    468  1.29  christos 			tm->TM_GMTOFF = offs;
    469  1.29  christos #endif
    470  1.29  christos #ifdef TM_ZONE
    471  1.29  christos 			tm->TM_ZONE = NULL;	/* XXX */
    472  1.29  christos #endif
    473  1.29  christos 			continue;
    474  1.29  christos 
    475   1.3    kleink 		/*
    476   1.3    kleink 		 * Miscellaneous conversions.
    477   1.3    kleink 		 */
    478   1.3    kleink 		case 'n':	/* Any kind of white-space. */
    479   1.3    kleink 		case 't':
    480   1.3    kleink 			while (isspace(*bp))
    481   1.3    kleink 				bp++;
    482  1.23       dsl 			LEGAL_ALT(0);
    483  1.23       dsl 			continue;
    484   1.2    kleink 
    485   1.1       mrg 
    486   1.3    kleink 		default:	/* Unknown/unsupported conversion. */
    487  1.24       dsl 			return NULL;
    488   1.3    kleink 		}
    489   1.3    kleink 	}
    490   1.2    kleink 
    491  1.25  christos 	return __UNCONST(bp);
    492   1.3    kleink }
    493   1.2    kleink 
    494   1.2    kleink 
    495  1.23       dsl static const u_char *
    496  1.24       dsl conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
    497   1.3    kleink {
    498  1.24       dsl 	uint result = 0;
    499  1.23       dsl 	unsigned char ch;
    500  1.18        tv 
    501  1.18        tv 	/* The limit also determines the number of valid digits. */
    502  1.24       dsl 	uint rulim = ulim;
    503   1.2    kleink 
    504  1.23       dsl 	ch = *buf;
    505  1.23       dsl 	if (ch < '0' || ch > '9')
    506  1.24       dsl 		return NULL;
    507   1.2    kleink 
    508   1.3    kleink 	do {
    509  1.18        tv 		result *= 10;
    510  1.23       dsl 		result += ch - '0';
    511  1.18        tv 		rulim /= 10;
    512  1.23       dsl 		ch = *++buf;
    513  1.23       dsl 	} while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
    514   1.2    kleink 
    515  1.18        tv 	if (result < llim || result > ulim)
    516  1.24       dsl 		return NULL;
    517   1.1       mrg 
    518  1.18        tv 	*dest = result;
    519  1.23       dsl 	return buf;
    520  1.23       dsl }
    521  1.23       dsl 
    522  1.23       dsl static const u_char *
    523  1.23       dsl find_string(const u_char *bp, int *tgt, const char * const *n1,
    524  1.23       dsl 		const char * const *n2, int c)
    525  1.23       dsl {
    526  1.23       dsl 	int i;
    527  1.23       dsl 	unsigned int len;
    528  1.23       dsl 
    529  1.24       dsl 	/* check full name - then abbreviated ones */
    530  1.24       dsl 	for (; n1 != NULL; n1 = n2, n2 = NULL) {
    531  1.24       dsl 		for (i = 0; i < c; i++, n1++) {
    532  1.24       dsl 			len = strlen(*n1);
    533  1.24       dsl 			if (strncasecmp(*n1, (const char *)bp, len) == 0) {
    534  1.24       dsl 				*tgt = i;
    535  1.24       dsl 				return bp + len;
    536  1.24       dsl 			}
    537  1.24       dsl 		}
    538  1.23       dsl 	}
    539  1.24       dsl 
    540  1.24       dsl 	/* Nothing matched */
    541  1.24       dsl 	return NULL;
    542   1.1       mrg }
    543