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