Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.26
      1  1.26  ginsbach /*	$NetBSD: strptime.c,v 1.26 2008/04/24 21:34:48 ginsbach Exp $	*/
      2   1.1       mrg 
      3   1.3    kleink /*-
      4  1.24       dsl  * Copyright (c) 1997, 1998, 2005 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.3    kleink  * 3. All advertising materials mentioning features or use of this software
     19   1.3    kleink  *    must display the following acknowledgement:
     20   1.3    kleink  *        This product includes software developed by the NetBSD
     21   1.3    kleink  *        Foundation, Inc. and its contributors.
     22   1.3    kleink  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.3    kleink  *    contributors may be used to endorse or promote products derived
     24   1.3    kleink  *    from this software without specific prior written permission.
     25   1.1       mrg  *
     26   1.3    kleink  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.3    kleink  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.3    kleink  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.3    kleink  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.3    kleink  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.3    kleink  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.3    kleink  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.3    kleink  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.3    kleink  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.3    kleink  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1       mrg  */
     38   1.1       mrg 
     39   1.6  christos #include <sys/cdefs.h>
     40   1.3    kleink #if defined(LIBC_SCCS) && !defined(lint)
     41  1.26  ginsbach __RCSID("$NetBSD: strptime.c,v 1.26 2008/04/24 21:34:48 ginsbach Exp $");
     42   1.1       mrg #endif
     43   1.1       mrg 
     44   1.7       jtc #include "namespace.h"
     45   1.3    kleink #include <sys/localedef.h>
     46   1.3    kleink #include <ctype.h>
     47   1.1       mrg #include <locale.h>
     48   1.3    kleink #include <string.h>
     49   1.1       mrg #include <time.h>
     50  1.12   mycroft #include <tzfile.h>
     51   1.7       jtc 
     52   1.7       jtc #ifdef __weak_alias
     53  1.19   mycroft __weak_alias(strptime,_strptime)
     54   1.7       jtc #endif
     55   1.3    kleink 
     56  1.21       cgd #define	_ctloc(x)		(_CurrentTimeLocale->x)
     57   1.3    kleink 
     58   1.3    kleink /*
     59   1.3    kleink  * We do not implement alternate representations. However, we always
     60   1.3    kleink  * check whether a given modifier is allowed for a certain conversion.
     61   1.3    kleink  */
     62  1.16    kleink #define ALT_E			0x01
     63  1.16    kleink #define ALT_O			0x02
     64  1.24       dsl #define	LEGAL_ALT(x)		{ if (alt_format & ~(x)) return NULL; }
     65   1.3    kleink 
     66  1.26  ginsbach static const char gmt[4] = { "GMT" };
     67   1.3    kleink 
     68  1.24       dsl static const u_char *conv_num(const unsigned char *, int *, uint, uint);
     69  1.23       dsl static const u_char *find_string(const u_char *, int *, const char * const *,
     70  1.23       dsl 	const char * const *, int);
     71   1.3    kleink 
     72   1.1       mrg 
     73   1.2    kleink char *
     74  1.23       dsl strptime(const char *buf, const char *fmt, struct tm *tm)
     75   1.1       mrg {
     76  1.20     itohy 	unsigned char c;
     77  1.20     itohy 	const unsigned char *bp;
     78  1.16    kleink 	int alt_format, i, split_year = 0;
     79  1.23       dsl 	const char *new_fmt;
     80   1.3    kleink 
     81  1.22  christos 	bp = (const u_char *)buf;
     82   1.3    kleink 
     83  1.24       dsl 	while (bp != NULL && (c = *fmt++) != '\0') {
     84   1.3    kleink 		/* Clear `alternate' modifier prior to new conversion. */
     85   1.3    kleink 		alt_format = 0;
     86  1.23       dsl 		i = 0;
     87   1.3    kleink 
     88   1.3    kleink 		/* Eat up white-space. */
     89   1.3    kleink 		if (isspace(c)) {
     90   1.3    kleink 			while (isspace(*bp))
     91   1.3    kleink 				bp++;
     92   1.2    kleink 			continue;
     93   1.2    kleink 		}
     94  1.23       dsl 
     95  1.24       dsl 		if (c != '%')
     96   1.3    kleink 			goto literal;
     97   1.3    kleink 
     98   1.3    kleink 
     99   1.3    kleink again:		switch (c = *fmt++) {
    100   1.3    kleink 		case '%':	/* "%%" is converted to "%". */
    101   1.3    kleink literal:
    102  1.14        tv 			if (c != *bp++)
    103  1.24       dsl 				return NULL;
    104  1.23       dsl 			LEGAL_ALT(0);
    105  1.23       dsl 			continue;
    106   1.3    kleink 
    107   1.3    kleink 		/*
    108   1.3    kleink 		 * "Alternative" modifiers. Just set the appropriate flag
    109   1.3    kleink 		 * and start over again.
    110   1.3    kleink 		 */
    111   1.3    kleink 		case 'E':	/* "%E?" alternative conversion modifier. */
    112  1.16    kleink 			LEGAL_ALT(0);
    113  1.16    kleink 			alt_format |= ALT_E;
    114   1.3    kleink 			goto again;
    115   1.3    kleink 
    116   1.3    kleink 		case 'O':	/* "%O?" alternative conversion modifier. */
    117  1.16    kleink 			LEGAL_ALT(0);
    118  1.16    kleink 			alt_format |= ALT_O;
    119   1.3    kleink 			goto again;
    120  1.23       dsl 
    121   1.3    kleink 		/*
    122   1.3    kleink 		 * "Complex" conversion rules, implemented through recursion.
    123   1.3    kleink 		 */
    124   1.3    kleink 		case 'c':	/* Date and time, using the locale's format. */
    125  1.23       dsl 			new_fmt = _ctloc(d_t_fmt);
    126  1.23       dsl 			goto recurse;
    127   1.3    kleink 
    128   1.3    kleink 		case 'D':	/* The date as "%m/%d/%y". */
    129  1.23       dsl 			new_fmt = "%m/%d/%y";
    130  1.16    kleink 			LEGAL_ALT(0);
    131  1.23       dsl 			goto recurse;
    132  1.18        tv 
    133   1.3    kleink 		case 'R':	/* The time as "%H:%M". */
    134  1.23       dsl 			new_fmt = "%H:%M";
    135  1.16    kleink 			LEGAL_ALT(0);
    136  1.23       dsl 			goto recurse;
    137   1.3    kleink 
    138   1.3    kleink 		case 'r':	/* The time in 12-hour clock representation. */
    139  1.23       dsl 			new_fmt =_ctloc(t_fmt_ampm);
    140  1.16    kleink 			LEGAL_ALT(0);
    141  1.23       dsl 			goto recurse;
    142   1.3    kleink 
    143   1.3    kleink 		case 'T':	/* The time as "%H:%M:%S". */
    144  1.23       dsl 			new_fmt = "%H:%M:%S";
    145  1.16    kleink 			LEGAL_ALT(0);
    146  1.23       dsl 			goto recurse;
    147   1.3    kleink 
    148   1.3    kleink 		case 'X':	/* The time, using the locale's format. */
    149  1.23       dsl 			new_fmt =_ctloc(t_fmt);
    150  1.23       dsl 			goto recurse;
    151   1.3    kleink 
    152   1.3    kleink 		case 'x':	/* The date, using the locale's format. */
    153  1.23       dsl 			new_fmt =_ctloc(d_fmt);
    154  1.23       dsl 		    recurse:
    155  1.23       dsl 			bp = (const u_char *)strptime((const char *)bp,
    156  1.23       dsl 							    new_fmt, tm);
    157  1.16    kleink 			LEGAL_ALT(ALT_E);
    158  1.23       dsl 			continue;
    159   1.3    kleink 
    160   1.3    kleink 		/*
    161   1.3    kleink 		 * "Elementary" conversion rules.
    162   1.3    kleink 		 */
    163   1.3    kleink 		case 'A':	/* The day of week, using the locale's form. */
    164   1.3    kleink 		case 'a':
    165  1.23       dsl 			bp = find_string(bp, &tm->tm_wday, _ctloc(day),
    166  1.23       dsl 					_ctloc(abday), 7);
    167  1.16    kleink 			LEGAL_ALT(0);
    168  1.23       dsl 			continue;
    169   1.2    kleink 
    170   1.3    kleink 		case 'B':	/* The month, using the locale's form. */
    171   1.3    kleink 		case 'b':
    172   1.3    kleink 		case 'h':
    173  1.23       dsl 			bp = find_string(bp, &tm->tm_mon, _ctloc(mon),
    174  1.23       dsl 					_ctloc(abmon), 12);
    175  1.16    kleink 			LEGAL_ALT(0);
    176  1.23       dsl 			continue;
    177   1.2    kleink 
    178   1.3    kleink 		case 'C':	/* The century number. */
    179  1.24       dsl 			i = 20;
    180  1.23       dsl 			bp = conv_num(bp, &i, 0, 99);
    181   1.2    kleink 
    182  1.24       dsl 			i = i * 100 - TM_YEAR_BASE;
    183  1.24       dsl 			if (split_year)
    184  1.24       dsl 				i += tm->tm_year % 100;
    185  1.24       dsl 			split_year = 1;
    186  1.24       dsl 			tm->tm_year = i;
    187  1.23       dsl 			LEGAL_ALT(ALT_E);
    188  1.23       dsl 			continue;
    189   1.2    kleink 
    190   1.3    kleink 		case 'd':	/* The day of month. */
    191   1.3    kleink 		case 'e':
    192  1.23       dsl 			bp = conv_num(bp, &tm->tm_mday, 1, 31);
    193  1.16    kleink 			LEGAL_ALT(ALT_O);
    194  1.23       dsl 			continue;
    195   1.2    kleink 
    196   1.3    kleink 		case 'k':	/* The hour (24-hour clock representation). */
    197  1.16    kleink 			LEGAL_ALT(0);
    198   1.3    kleink 			/* FALLTHROUGH */
    199   1.3    kleink 		case 'H':
    200  1.23       dsl 			bp = conv_num(bp, &tm->tm_hour, 0, 23);
    201  1.16    kleink 			LEGAL_ALT(ALT_O);
    202  1.23       dsl 			continue;
    203   1.2    kleink 
    204   1.3    kleink 		case 'l':	/* The hour (12-hour clock representation). */
    205  1.16    kleink 			LEGAL_ALT(0);
    206   1.3    kleink 			/* FALLTHROUGH */
    207   1.3    kleink 		case 'I':
    208  1.23       dsl 			bp = conv_num(bp, &tm->tm_hour, 1, 12);
    209  1.13        tv 			if (tm->tm_hour == 12)
    210  1.13        tv 				tm->tm_hour = 0;
    211  1.23       dsl 			LEGAL_ALT(ALT_O);
    212  1.23       dsl 			continue;
    213   1.2    kleink 
    214   1.3    kleink 		case 'j':	/* The day of year. */
    215  1.23       dsl 			i = 1;
    216  1.23       dsl 			bp = conv_num(bp, &i, 1, 366);
    217  1.23       dsl 			tm->tm_yday = i - 1;
    218  1.16    kleink 			LEGAL_ALT(0);
    219  1.23       dsl 			continue;
    220   1.2    kleink 
    221   1.3    kleink 		case 'M':	/* The minute. */
    222  1.23       dsl 			bp = conv_num(bp, &tm->tm_min, 0, 59);
    223  1.16    kleink 			LEGAL_ALT(ALT_O);
    224  1.23       dsl 			continue;
    225   1.2    kleink 
    226   1.3    kleink 		case 'm':	/* The month. */
    227  1.23       dsl 			i = 1;
    228  1.23       dsl 			bp = conv_num(bp, &i, 1, 12);
    229  1.23       dsl 			tm->tm_mon = i - 1;
    230  1.16    kleink 			LEGAL_ALT(ALT_O);
    231  1.23       dsl 			continue;
    232   1.2    kleink 
    233   1.3    kleink 		case 'p':	/* The locale's equivalent of AM/PM. */
    234  1.24       dsl 			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
    235  1.23       dsl 			if (tm->tm_hour > 11)
    236  1.24       dsl 				return NULL;
    237  1.23       dsl 			tm->tm_hour += i * 12;
    238  1.16    kleink 			LEGAL_ALT(0);
    239  1.23       dsl 			continue;
    240   1.2    kleink 
    241   1.3    kleink 		case 'S':	/* The seconds. */
    242  1.23       dsl 			bp = conv_num(bp, &tm->tm_sec, 0, 61);
    243  1.16    kleink 			LEGAL_ALT(ALT_O);
    244  1.23       dsl 			continue;
    245   1.1       mrg 
    246   1.3    kleink 		case 'U':	/* The week of year, beginning on sunday. */
    247   1.3    kleink 		case 'W':	/* The week of year, beginning on monday. */
    248   1.3    kleink 			/*
    249   1.3    kleink 			 * XXX This is bogus, as we can not assume any valid
    250   1.3    kleink 			 * information present in the tm structure at this
    251   1.3    kleink 			 * point to calculate a real value, so just check the
    252   1.3    kleink 			 * range for now.
    253   1.3    kleink 			 */
    254  1.23       dsl 			 bp = conv_num(bp, &i, 0, 53);
    255  1.23       dsl 			 LEGAL_ALT(ALT_O);
    256  1.23       dsl 			 continue;
    257   1.2    kleink 
    258   1.3    kleink 		case 'w':	/* The day of week, beginning on sunday. */
    259  1.23       dsl 			bp = conv_num(bp, &tm->tm_wday, 0, 6);
    260  1.16    kleink 			LEGAL_ALT(ALT_O);
    261  1.23       dsl 			continue;
    262   1.2    kleink 
    263   1.3    kleink 		case 'Y':	/* The year. */
    264  1.24       dsl 			i = TM_YEAR_BASE;	/* just for data sanity... */
    265  1.23       dsl 			bp = conv_num(bp, &i, 0, 9999);
    266   1.8   mycroft 			tm->tm_year = i - TM_YEAR_BASE;
    267  1.23       dsl 			LEGAL_ALT(ALT_E);
    268  1.23       dsl 			continue;
    269   1.2    kleink 
    270   1.9   mycroft 		case 'y':	/* The year within 100 years of the epoch. */
    271  1.24       dsl 			/* LEGAL_ALT(ALT_E | ALT_O); */
    272  1.23       dsl 			bp = conv_num(bp, &i, 0, 99);
    273   1.8   mycroft 
    274  1.24       dsl 			if (split_year)
    275  1.24       dsl 				/* preserve century */
    276  1.24       dsl 				i += (tm->tm_year / 100) * 100;
    277  1.24       dsl 			else {
    278  1.24       dsl 				split_year = 1;
    279  1.24       dsl 				if (i <= 68)
    280  1.24       dsl 					i = i + 2000 - TM_YEAR_BASE;
    281  1.24       dsl 				else
    282  1.24       dsl 					i = i + 1900 - TM_YEAR_BASE;
    283  1.13        tv 			}
    284  1.24       dsl 			tm->tm_year = i;
    285  1.23       dsl 			continue;
    286   1.2    kleink 
    287  1.26  ginsbach 		case 'Z':
    288  1.26  ginsbach 			tzset();
    289  1.26  ginsbach 			if (strncmp((const char *)bp, gmt, 3) == 0) {
    290  1.26  ginsbach 				tm->tm_isdst = 0;
    291  1.26  ginsbach #ifdef TM_GMTOFF
    292  1.26  ginsbach 				tm->TM_GMTOFF = 0;
    293  1.26  ginsbach #endif
    294  1.26  ginsbach #ifdef TM_ZONE
    295  1.26  ginsbach 				tm->TM_ZONE = gmt;
    296  1.26  ginsbach #endif
    297  1.26  ginsbach 				bp += 3;
    298  1.26  ginsbach 			} else {
    299  1.26  ginsbach 				const unsigned char *ep;
    300  1.26  ginsbach 
    301  1.26  ginsbach 				ep = find_string(bp, &i,
    302  1.26  ginsbach 					       	 (const char * const *)tzname,
    303  1.26  ginsbach 					       	  NULL, 2);
    304  1.26  ginsbach 				if (ep != NULL) {
    305  1.26  ginsbach 					tm->tm_isdst = i;
    306  1.26  ginsbach #ifdef TM_GMTOFF
    307  1.26  ginsbach 					tm->TM_GMTOFF = -(timezone);
    308  1.26  ginsbach #endif
    309  1.26  ginsbach #ifdef TM_ZONE
    310  1.26  ginsbach 					tm->TM_ZONE = tzname[i];
    311  1.26  ginsbach #endif
    312  1.26  ginsbach 				}
    313  1.26  ginsbach 				bp = ep;
    314  1.26  ginsbach 			}
    315  1.26  ginsbach 			continue;
    316  1.26  ginsbach 
    317   1.3    kleink 		/*
    318   1.3    kleink 		 * Miscellaneous conversions.
    319   1.3    kleink 		 */
    320   1.3    kleink 		case 'n':	/* Any kind of white-space. */
    321   1.3    kleink 		case 't':
    322   1.3    kleink 			while (isspace(*bp))
    323   1.3    kleink 				bp++;
    324  1.23       dsl 			LEGAL_ALT(0);
    325  1.23       dsl 			continue;
    326   1.2    kleink 
    327   1.1       mrg 
    328   1.3    kleink 		default:	/* Unknown/unsupported conversion. */
    329  1.24       dsl 			return NULL;
    330   1.3    kleink 		}
    331   1.3    kleink 	}
    332   1.2    kleink 
    333  1.25  christos 	return __UNCONST(bp);
    334   1.3    kleink }
    335   1.2    kleink 
    336   1.2    kleink 
    337  1.23       dsl static const u_char *
    338  1.24       dsl conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
    339   1.3    kleink {
    340  1.24       dsl 	uint result = 0;
    341  1.23       dsl 	unsigned char ch;
    342  1.18        tv 
    343  1.18        tv 	/* The limit also determines the number of valid digits. */
    344  1.24       dsl 	uint rulim = ulim;
    345   1.2    kleink 
    346  1.23       dsl 	ch = *buf;
    347  1.23       dsl 	if (ch < '0' || ch > '9')
    348  1.24       dsl 		return NULL;
    349   1.2    kleink 
    350   1.3    kleink 	do {
    351  1.18        tv 		result *= 10;
    352  1.23       dsl 		result += ch - '0';
    353  1.18        tv 		rulim /= 10;
    354  1.23       dsl 		ch = *++buf;
    355  1.23       dsl 	} while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
    356   1.2    kleink 
    357  1.18        tv 	if (result < llim || result > ulim)
    358  1.24       dsl 		return NULL;
    359   1.1       mrg 
    360  1.18        tv 	*dest = result;
    361  1.23       dsl 	return buf;
    362  1.23       dsl }
    363  1.23       dsl 
    364  1.23       dsl static const u_char *
    365  1.23       dsl find_string(const u_char *bp, int *tgt, const char * const *n1,
    366  1.23       dsl 		const char * const *n2, int c)
    367  1.23       dsl {
    368  1.23       dsl 	int i;
    369  1.23       dsl 	unsigned int len;
    370  1.23       dsl 
    371  1.24       dsl 	/* check full name - then abbreviated ones */
    372  1.24       dsl 	for (; n1 != NULL; n1 = n2, n2 = NULL) {
    373  1.24       dsl 		for (i = 0; i < c; i++, n1++) {
    374  1.24       dsl 			len = strlen(*n1);
    375  1.24       dsl 			if (strncasecmp(*n1, (const char *)bp, len) == 0) {
    376  1.24       dsl 				*tgt = i;
    377  1.24       dsl 				return bp + len;
    378  1.24       dsl 			}
    379  1.24       dsl 		}
    380  1.23       dsl 	}
    381  1.24       dsl 
    382  1.24       dsl 	/* Nothing matched */
    383  1.24       dsl 	return NULL;
    384   1.1       mrg }
    385