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