Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.24
      1  1.24       dsl /*	$NetBSD: strptime.c,v 1.24 2005/03/05 14:07:15 dsl 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.24       dsl __RCSID("$NetBSD: strptime.c,v 1.24 2005/03/05 14:07:15 dsl 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.3    kleink 
     67  1.24       dsl static const u_char *conv_num(const unsigned char *, int *, uint, uint);
     68  1.23       dsl static const u_char *find_string(const u_char *, int *, const char * const *,
     69  1.23       dsl 	const char * const *, int);
     70   1.3    kleink 
     71   1.1       mrg 
     72   1.2    kleink char *
     73  1.23       dsl strptime(const char *buf, const char *fmt, struct tm *tm)
     74   1.1       mrg {
     75  1.20     itohy 	unsigned char c;
     76  1.20     itohy 	const unsigned char *bp;
     77  1.16    kleink 	int alt_format, i, split_year = 0;
     78  1.23       dsl 	const char *new_fmt;
     79   1.3    kleink 
     80  1.22  christos 	bp = (const u_char *)buf;
     81   1.3    kleink 
     82  1.24       dsl 	while (bp != NULL && (c = *fmt++) != '\0') {
     83   1.3    kleink 		/* Clear `alternate' modifier prior to new conversion. */
     84   1.3    kleink 		alt_format = 0;
     85  1.23       dsl 		i = 0;
     86   1.3    kleink 
     87   1.3    kleink 		/* Eat up white-space. */
     88   1.3    kleink 		if (isspace(c)) {
     89   1.3    kleink 			while (isspace(*bp))
     90   1.3    kleink 				bp++;
     91   1.2    kleink 			continue;
     92   1.2    kleink 		}
     93  1.23       dsl 
     94  1.24       dsl 		if (c != '%')
     95   1.3    kleink 			goto literal;
     96   1.3    kleink 
     97   1.3    kleink 
     98   1.3    kleink again:		switch (c = *fmt++) {
     99   1.3    kleink 		case '%':	/* "%%" is converted to "%". */
    100   1.3    kleink literal:
    101  1.14        tv 			if (c != *bp++)
    102  1.24       dsl 				return NULL;
    103  1.23       dsl 			LEGAL_ALT(0);
    104  1.23       dsl 			continue;
    105   1.3    kleink 
    106   1.3    kleink 		/*
    107   1.3    kleink 		 * "Alternative" modifiers. Just set the appropriate flag
    108   1.3    kleink 		 * and start over again.
    109   1.3    kleink 		 */
    110   1.3    kleink 		case 'E':	/* "%E?" alternative conversion modifier. */
    111  1.16    kleink 			LEGAL_ALT(0);
    112  1.16    kleink 			alt_format |= ALT_E;
    113   1.3    kleink 			goto again;
    114   1.3    kleink 
    115   1.3    kleink 		case 'O':	/* "%O?" alternative conversion modifier. */
    116  1.16    kleink 			LEGAL_ALT(0);
    117  1.16    kleink 			alt_format |= ALT_O;
    118   1.3    kleink 			goto again;
    119  1.23       dsl 
    120   1.3    kleink 		/*
    121   1.3    kleink 		 * "Complex" conversion rules, implemented through recursion.
    122   1.3    kleink 		 */
    123   1.3    kleink 		case 'c':	/* Date and time, using the locale's format. */
    124  1.23       dsl 			new_fmt = _ctloc(d_t_fmt);
    125  1.23       dsl 			goto recurse;
    126   1.3    kleink 
    127   1.3    kleink 		case 'D':	/* The date as "%m/%d/%y". */
    128  1.23       dsl 			new_fmt = "%m/%d/%y";
    129  1.16    kleink 			LEGAL_ALT(0);
    130  1.23       dsl 			goto recurse;
    131  1.18        tv 
    132   1.3    kleink 		case 'R':	/* The time as "%H:%M". */
    133  1.23       dsl 			new_fmt = "%H:%M";
    134  1.16    kleink 			LEGAL_ALT(0);
    135  1.23       dsl 			goto recurse;
    136   1.3    kleink 
    137   1.3    kleink 		case 'r':	/* The time in 12-hour clock representation. */
    138  1.23       dsl 			new_fmt =_ctloc(t_fmt_ampm);
    139  1.16    kleink 			LEGAL_ALT(0);
    140  1.23       dsl 			goto recurse;
    141   1.3    kleink 
    142   1.3    kleink 		case 'T':	/* The time as "%H:%M:%S". */
    143  1.23       dsl 			new_fmt = "%H:%M:%S";
    144  1.16    kleink 			LEGAL_ALT(0);
    145  1.23       dsl 			goto recurse;
    146   1.3    kleink 
    147   1.3    kleink 		case 'X':	/* The time, using the locale's format. */
    148  1.23       dsl 			new_fmt =_ctloc(t_fmt);
    149  1.23       dsl 			goto recurse;
    150   1.3    kleink 
    151   1.3    kleink 		case 'x':	/* The date, using the locale's format. */
    152  1.23       dsl 			new_fmt =_ctloc(d_fmt);
    153  1.23       dsl 		    recurse:
    154  1.23       dsl 			bp = (const u_char *)strptime((const char *)bp,
    155  1.23       dsl 							    new_fmt, tm);
    156  1.16    kleink 			LEGAL_ALT(ALT_E);
    157  1.23       dsl 			continue;
    158   1.3    kleink 
    159   1.3    kleink 		/*
    160   1.3    kleink 		 * "Elementary" conversion rules.
    161   1.3    kleink 		 */
    162   1.3    kleink 		case 'A':	/* The day of week, using the locale's form. */
    163   1.3    kleink 		case 'a':
    164  1.23       dsl 			bp = find_string(bp, &tm->tm_wday, _ctloc(day),
    165  1.23       dsl 					_ctloc(abday), 7);
    166  1.16    kleink 			LEGAL_ALT(0);
    167  1.23       dsl 			continue;
    168   1.2    kleink 
    169   1.3    kleink 		case 'B':	/* The month, using the locale's form. */
    170   1.3    kleink 		case 'b':
    171   1.3    kleink 		case 'h':
    172  1.23       dsl 			bp = find_string(bp, &tm->tm_mon, _ctloc(mon),
    173  1.23       dsl 					_ctloc(abmon), 12);
    174  1.16    kleink 			LEGAL_ALT(0);
    175  1.23       dsl 			continue;
    176   1.2    kleink 
    177   1.3    kleink 		case 'C':	/* The century number. */
    178  1.24       dsl 			i = 20;
    179  1.23       dsl 			bp = conv_num(bp, &i, 0, 99);
    180   1.2    kleink 
    181  1.24       dsl 			i = i * 100 - TM_YEAR_BASE;
    182  1.24       dsl 			if (split_year)
    183  1.24       dsl 				i += tm->tm_year % 100;
    184  1.24       dsl 			split_year = 1;
    185  1.24       dsl 			tm->tm_year = i;
    186  1.23       dsl 			LEGAL_ALT(ALT_E);
    187  1.23       dsl 			continue;
    188   1.2    kleink 
    189   1.3    kleink 		case 'd':	/* The day of month. */
    190   1.3    kleink 		case 'e':
    191  1.23       dsl 			bp = conv_num(bp, &tm->tm_mday, 1, 31);
    192  1.16    kleink 			LEGAL_ALT(ALT_O);
    193  1.23       dsl 			continue;
    194   1.2    kleink 
    195   1.3    kleink 		case 'k':	/* The hour (24-hour clock representation). */
    196  1.16    kleink 			LEGAL_ALT(0);
    197   1.3    kleink 			/* FALLTHROUGH */
    198   1.3    kleink 		case 'H':
    199  1.23       dsl 			bp = conv_num(bp, &tm->tm_hour, 0, 23);
    200  1.16    kleink 			LEGAL_ALT(ALT_O);
    201  1.23       dsl 			continue;
    202   1.2    kleink 
    203   1.3    kleink 		case 'l':	/* The hour (12-hour clock representation). */
    204  1.16    kleink 			LEGAL_ALT(0);
    205   1.3    kleink 			/* FALLTHROUGH */
    206   1.3    kleink 		case 'I':
    207  1.23       dsl 			bp = conv_num(bp, &tm->tm_hour, 1, 12);
    208  1.13        tv 			if (tm->tm_hour == 12)
    209  1.13        tv 				tm->tm_hour = 0;
    210  1.23       dsl 			LEGAL_ALT(ALT_O);
    211  1.23       dsl 			continue;
    212   1.2    kleink 
    213   1.3    kleink 		case 'j':	/* The day of year. */
    214  1.23       dsl 			i = 1;
    215  1.23       dsl 			bp = conv_num(bp, &i, 1, 366);
    216  1.23       dsl 			tm->tm_yday = i - 1;
    217  1.16    kleink 			LEGAL_ALT(0);
    218  1.23       dsl 			continue;
    219   1.2    kleink 
    220   1.3    kleink 		case 'M':	/* The minute. */
    221  1.23       dsl 			bp = conv_num(bp, &tm->tm_min, 0, 59);
    222  1.16    kleink 			LEGAL_ALT(ALT_O);
    223  1.23       dsl 			continue;
    224   1.2    kleink 
    225   1.3    kleink 		case 'm':	/* The month. */
    226  1.23       dsl 			i = 1;
    227  1.23       dsl 			bp = conv_num(bp, &i, 1, 12);
    228  1.23       dsl 			tm->tm_mon = i - 1;
    229  1.16    kleink 			LEGAL_ALT(ALT_O);
    230  1.23       dsl 			continue;
    231   1.2    kleink 
    232   1.3    kleink 		case 'p':	/* The locale's equivalent of AM/PM. */
    233  1.24       dsl 			bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
    234  1.23       dsl 			if (tm->tm_hour > 11)
    235  1.24       dsl 				return NULL;
    236  1.23       dsl 			tm->tm_hour += i * 12;
    237  1.16    kleink 			LEGAL_ALT(0);
    238  1.23       dsl 			continue;
    239   1.2    kleink 
    240   1.3    kleink 		case 'S':	/* The seconds. */
    241  1.23       dsl 			bp = conv_num(bp, &tm->tm_sec, 0, 61);
    242  1.16    kleink 			LEGAL_ALT(ALT_O);
    243  1.23       dsl 			continue;
    244   1.1       mrg 
    245   1.3    kleink 		case 'U':	/* The week of year, beginning on sunday. */
    246   1.3    kleink 		case 'W':	/* The week of year, beginning on monday. */
    247   1.3    kleink 			/*
    248   1.3    kleink 			 * XXX This is bogus, as we can not assume any valid
    249   1.3    kleink 			 * information present in the tm structure at this
    250   1.3    kleink 			 * point to calculate a real value, so just check the
    251   1.3    kleink 			 * range for now.
    252   1.3    kleink 			 */
    253  1.23       dsl 			 bp = conv_num(bp, &i, 0, 53);
    254  1.23       dsl 			 LEGAL_ALT(ALT_O);
    255  1.23       dsl 			 continue;
    256   1.2    kleink 
    257   1.3    kleink 		case 'w':	/* The day of week, beginning on sunday. */
    258  1.23       dsl 			bp = conv_num(bp, &tm->tm_wday, 0, 6);
    259  1.16    kleink 			LEGAL_ALT(ALT_O);
    260  1.23       dsl 			continue;
    261   1.2    kleink 
    262   1.3    kleink 		case 'Y':	/* The year. */
    263  1.24       dsl 			i = TM_YEAR_BASE;	/* just for data sanity... */
    264  1.23       dsl 			bp = conv_num(bp, &i, 0, 9999);
    265   1.8   mycroft 			tm->tm_year = i - TM_YEAR_BASE;
    266  1.23       dsl 			LEGAL_ALT(ALT_E);
    267  1.23       dsl 			continue;
    268   1.2    kleink 
    269   1.9   mycroft 		case 'y':	/* The year within 100 years of the epoch. */
    270  1.24       dsl 			/* LEGAL_ALT(ALT_E | ALT_O); */
    271  1.23       dsl 			bp = conv_num(bp, &i, 0, 99);
    272   1.8   mycroft 
    273  1.24       dsl 			if (split_year)
    274  1.24       dsl 				/* preserve century */
    275  1.24       dsl 				i += (tm->tm_year / 100) * 100;
    276  1.24       dsl 			else {
    277  1.24       dsl 				split_year = 1;
    278  1.24       dsl 				if (i <= 68)
    279  1.24       dsl 					i = i + 2000 - TM_YEAR_BASE;
    280  1.24       dsl 				else
    281  1.24       dsl 					i = i + 1900 - TM_YEAR_BASE;
    282  1.13        tv 			}
    283  1.24       dsl 			tm->tm_year = i;
    284  1.23       dsl 			continue;
    285   1.2    kleink 
    286   1.3    kleink 		/*
    287   1.3    kleink 		 * Miscellaneous conversions.
    288   1.3    kleink 		 */
    289   1.3    kleink 		case 'n':	/* Any kind of white-space. */
    290   1.3    kleink 		case 't':
    291   1.3    kleink 			while (isspace(*bp))
    292   1.3    kleink 				bp++;
    293  1.23       dsl 			LEGAL_ALT(0);
    294  1.23       dsl 			continue;
    295   1.2    kleink 
    296   1.1       mrg 
    297   1.3    kleink 		default:	/* Unknown/unsupported conversion. */
    298  1.24       dsl 			return NULL;
    299   1.3    kleink 		}
    300   1.3    kleink 	}
    301   1.2    kleink 
    302  1.17  christos 	/* LINTED functional specification */
    303  1.23       dsl 	return (char *)bp;
    304   1.3    kleink }
    305   1.2    kleink 
    306   1.2    kleink 
    307  1.23       dsl static const u_char *
    308  1.24       dsl conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
    309   1.3    kleink {
    310  1.24       dsl 	uint result = 0;
    311  1.23       dsl 	unsigned char ch;
    312  1.18        tv 
    313  1.18        tv 	/* The limit also determines the number of valid digits. */
    314  1.24       dsl 	uint rulim = ulim;
    315   1.2    kleink 
    316  1.23       dsl 	ch = *buf;
    317  1.23       dsl 	if (ch < '0' || ch > '9')
    318  1.24       dsl 		return NULL;
    319   1.2    kleink 
    320   1.3    kleink 	do {
    321  1.18        tv 		result *= 10;
    322  1.23       dsl 		result += ch - '0';
    323  1.18        tv 		rulim /= 10;
    324  1.23       dsl 		ch = *++buf;
    325  1.23       dsl 	} while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
    326   1.2    kleink 
    327  1.18        tv 	if (result < llim || result > ulim)
    328  1.24       dsl 		return NULL;
    329   1.1       mrg 
    330  1.18        tv 	*dest = result;
    331  1.23       dsl 	return buf;
    332  1.23       dsl }
    333  1.23       dsl 
    334  1.23       dsl static const u_char *
    335  1.23       dsl find_string(const u_char *bp, int *tgt, const char * const *n1,
    336  1.23       dsl 		const char * const *n2, int c)
    337  1.23       dsl {
    338  1.23       dsl 	int i;
    339  1.23       dsl 	unsigned int len;
    340  1.23       dsl 
    341  1.24       dsl 	/* check full name - then abbreviated ones */
    342  1.24       dsl 	for (; n1 != NULL; n1 = n2, n2 = NULL) {
    343  1.24       dsl 		for (i = 0; i < c; i++, n1++) {
    344  1.24       dsl 			len = strlen(*n1);
    345  1.24       dsl 			if (strncasecmp(*n1, (const char *)bp, len) == 0) {
    346  1.24       dsl 				*tgt = i;
    347  1.24       dsl 				return bp + len;
    348  1.24       dsl 			}
    349  1.24       dsl 		}
    350  1.23       dsl 	}
    351  1.24       dsl 
    352  1.24       dsl 	/* Nothing matched */
    353  1.24       dsl 	return NULL;
    354   1.1       mrg }
    355