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