Home | History | Annotate | Line # | Download | only in time
strptime.c revision 1.22
      1  1.22  christos /*	$NetBSD: strptime.c,v 1.22 2000/12/20 20:56:34 christos Exp $	*/
      2   1.1       mrg 
      3   1.3    kleink /*-
      4  1.11   mycroft  * Copyright (c) 1997, 1998 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.6  christos #include <sys/cdefs.h>
     39   1.3    kleink #if defined(LIBC_SCCS) && !defined(lint)
     40  1.22  christos __RCSID("$NetBSD: strptime.c,v 1.22 2000/12/20 20:56:34 christos Exp $");
     41   1.1       mrg #endif
     42   1.1       mrg 
     43   1.7       jtc #include "namespace.h"
     44   1.3    kleink #include <sys/localedef.h>
     45   1.3    kleink #include <ctype.h>
     46   1.1       mrg #include <locale.h>
     47   1.3    kleink #include <string.h>
     48   1.1       mrg #include <time.h>
     49  1.12   mycroft #include <tzfile.h>
     50   1.7       jtc 
     51   1.7       jtc #ifdef __weak_alias
     52  1.19   mycroft __weak_alias(strptime,_strptime)
     53   1.7       jtc #endif
     54   1.3    kleink 
     55  1.21       cgd #define	_ctloc(x)		(_CurrentTimeLocale->x)
     56   1.3    kleink 
     57   1.3    kleink /*
     58   1.3    kleink  * We do not implement alternate representations. However, we always
     59   1.3    kleink  * check whether a given modifier is allowed for a certain conversion.
     60   1.3    kleink  */
     61  1.16    kleink #define ALT_E			0x01
     62  1.16    kleink #define ALT_O			0x02
     63  1.16    kleink #define	LEGAL_ALT(x)		{ if (alt_format & ~(x)) return (0); }
     64   1.3    kleink 
     65   1.3    kleink 
     66  1.20     itohy static	int conv_num __P((const unsigned char **, int *, int, int));
     67   1.3    kleink 
     68   1.1       mrg 
     69   1.2    kleink char *
     70   1.2    kleink strptime(buf, fmt, tm)
     71   1.2    kleink 	const char *buf, *fmt;
     72   1.2    kleink 	struct tm *tm;
     73   1.1       mrg {
     74  1.20     itohy 	unsigned char c;
     75  1.20     itohy 	const unsigned char *bp;
     76  1.16    kleink 	size_t len = 0;
     77  1.16    kleink 	int alt_format, i, split_year = 0;
     78   1.3    kleink 
     79  1.22  christos 	bp = (const u_char *)buf;
     80   1.3    kleink 
     81   1.3    kleink 	while ((c = *fmt) != '\0') {
     82   1.3    kleink 		/* Clear `alternate' modifier prior to new conversion. */
     83   1.3    kleink 		alt_format = 0;
     84   1.3    kleink 
     85   1.3    kleink 		/* Eat up white-space. */
     86   1.3    kleink 		if (isspace(c)) {
     87   1.3    kleink 			while (isspace(*bp))
     88   1.3    kleink 				bp++;
     89   1.3    kleink 
     90   1.3    kleink 			fmt++;
     91   1.2    kleink 			continue;
     92   1.2    kleink 		}
     93   1.3    kleink 
     94   1.3    kleink 		if ((c = *fmt++) != '%')
     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.14        tv 				return (0);
    103  1.14        tv 			break;
    104   1.3    kleink 
    105   1.3    kleink 		/*
    106   1.3    kleink 		 * "Alternative" modifiers. Just set the appropriate flag
    107   1.3    kleink 		 * and start over again.
    108   1.3    kleink 		 */
    109   1.3    kleink 		case 'E':	/* "%E?" alternative conversion modifier. */
    110  1.16    kleink 			LEGAL_ALT(0);
    111  1.16    kleink 			alt_format |= ALT_E;
    112   1.3    kleink 			goto again;
    113   1.3    kleink 
    114   1.3    kleink 		case 'O':	/* "%O?" alternative conversion modifier. */
    115  1.16    kleink 			LEGAL_ALT(0);
    116  1.16    kleink 			alt_format |= ALT_O;
    117   1.3    kleink 			goto again;
    118   1.3    kleink 
    119   1.3    kleink 		/*
    120   1.3    kleink 		 * "Complex" conversion rules, implemented through recursion.
    121   1.3    kleink 		 */
    122   1.3    kleink 		case 'c':	/* Date and time, using the locale's format. */
    123  1.16    kleink 			LEGAL_ALT(ALT_E);
    124  1.22  christos 			if (!(bp = (const u_char *)strptime((const char *)bp,
    125  1.22  christos 			    _ctloc(d_t_fmt), tm)))
    126   1.3    kleink 				return (0);
    127   1.3    kleink 			break;
    128   1.3    kleink 
    129   1.3    kleink 		case 'D':	/* The date as "%m/%d/%y". */
    130  1.16    kleink 			LEGAL_ALT(0);
    131  1.22  christos 			if (!(bp = (const u_char *) strptime((const char *)bp,
    132  1.22  christos 			    "%m/%d/%y", tm)))
    133   1.3    kleink 				return (0);
    134   1.3    kleink 			break;
    135  1.18        tv 
    136   1.3    kleink 		case 'R':	/* The time as "%H:%M". */
    137  1.16    kleink 			LEGAL_ALT(0);
    138  1.22  christos 			if (!(bp = (const u_char *)strptime((const char *)bp,
    139  1.22  christos 			    "%H:%M", tm)))
    140   1.3    kleink 				return (0);
    141   1.3    kleink 			break;
    142   1.3    kleink 
    143   1.3    kleink 		case 'r':	/* The time in 12-hour clock representation. */
    144  1.16    kleink 			LEGAL_ALT(0);
    145  1.22  christos 			if (!(bp = (const u_char *)strptime((const char *)bp,
    146  1.22  christos 			    _ctloc(t_fmt_ampm), tm)))
    147   1.3    kleink 				return (0);
    148   1.3    kleink 			break;
    149   1.3    kleink 
    150   1.3    kleink 		case 'T':	/* The time as "%H:%M:%S". */
    151  1.16    kleink 			LEGAL_ALT(0);
    152  1.22  christos 			if (!(bp = (const u_char *)strptime((const char *)bp,
    153  1.22  christos 			    "%H:%M:%S", tm)))
    154   1.3    kleink 				return (0);
    155   1.3    kleink 			break;
    156   1.3    kleink 
    157   1.3    kleink 		case 'X':	/* The time, using the locale's format. */
    158  1.16    kleink 			LEGAL_ALT(ALT_E);
    159  1.22  christos 			if (!(bp = (const u_char *)strptime((const char *)bp,
    160  1.22  christos 			    _ctloc(t_fmt), tm)))
    161   1.3    kleink 				return (0);
    162   1.3    kleink 			break;
    163   1.3    kleink 
    164   1.3    kleink 		case 'x':	/* The date, using the locale's format. */
    165  1.16    kleink 			LEGAL_ALT(ALT_E);
    166  1.22  christos 			if (!(bp = (const u_char *)strptime((const char *)bp,
    167  1.22  christos 			    _ctloc(d_fmt), tm)))
    168   1.3    kleink 				return (0);
    169   1.3    kleink 			break;
    170   1.3    kleink 
    171   1.3    kleink 		/*
    172   1.3    kleink 		 * "Elementary" conversion rules.
    173   1.3    kleink 		 */
    174   1.3    kleink 		case 'A':	/* The day of week, using the locale's form. */
    175   1.3    kleink 		case 'a':
    176  1.16    kleink 			LEGAL_ALT(0);
    177   1.3    kleink 			for (i = 0; i < 7; i++) {
    178   1.3    kleink 				/* Full name. */
    179   1.3    kleink 				len = strlen(_ctloc(day[i]));
    180  1.22  christos 				if (strncasecmp(_ctloc(day[i]),
    181  1.22  christos 				    (const char *)bp, len) == 0)
    182   1.3    kleink 					break;
    183   1.2    kleink 
    184   1.3    kleink 				/* Abbreviated name. */
    185   1.3    kleink 				len = strlen(_ctloc(abday[i]));
    186  1.22  christos 				if (strncasecmp(_ctloc(abday[i]),
    187  1.22  christos 				    (const char *)bp, len) == 0)
    188   1.3    kleink 					break;
    189   1.3    kleink 			}
    190   1.3    kleink 
    191   1.3    kleink 			/* Nothing matched. */
    192   1.3    kleink 			if (i == 7)
    193   1.3    kleink 				return (0);
    194   1.3    kleink 
    195   1.3    kleink 			tm->tm_wday = i;
    196   1.3    kleink 			bp += len;
    197   1.2    kleink 			break;
    198   1.2    kleink 
    199   1.3    kleink 		case 'B':	/* The month, using the locale's form. */
    200   1.3    kleink 		case 'b':
    201   1.3    kleink 		case 'h':
    202  1.16    kleink 			LEGAL_ALT(0);
    203   1.3    kleink 			for (i = 0; i < 12; i++) {
    204   1.3    kleink 				/* Full name. */
    205   1.3    kleink 				len = strlen(_ctloc(mon[i]));
    206  1.22  christos 				if (strncasecmp(_ctloc(mon[i]),
    207  1.22  christos 				    (const char *)bp, len) == 0)
    208   1.3    kleink 					break;
    209   1.3    kleink 
    210   1.3    kleink 				/* Abbreviated name. */
    211   1.3    kleink 				len = strlen(_ctloc(abmon[i]));
    212  1.22  christos 				if (strncasecmp(_ctloc(abmon[i]),
    213  1.22  christos 				    (const char *)bp, len) == 0)
    214   1.3    kleink 					break;
    215   1.3    kleink 			}
    216   1.3    kleink 
    217   1.3    kleink 			/* Nothing matched. */
    218   1.3    kleink 			if (i == 12)
    219   1.3    kleink 				return (0);
    220   1.3    kleink 
    221   1.3    kleink 			tm->tm_mon = i;
    222   1.3    kleink 			bp += len;
    223   1.2    kleink 			break;
    224   1.2    kleink 
    225   1.3    kleink 		case 'C':	/* The century number. */
    226  1.16    kleink 			LEGAL_ALT(ALT_E);
    227  1.16    kleink 			if (!(conv_num(&bp, &i, 0, 99)))
    228   1.3    kleink 				return (0);
    229   1.2    kleink 
    230  1.13        tv 			if (split_year) {
    231  1.13        tv 				tm->tm_year = (tm->tm_year % 100) + (i * 100);
    232  1.13        tv 			} else {
    233  1.13        tv 				tm->tm_year = i * 100;
    234  1.13        tv 				split_year = 1;
    235  1.13        tv 			}
    236   1.2    kleink 			break;
    237   1.2    kleink 
    238   1.3    kleink 		case 'd':	/* The day of month. */
    239   1.3    kleink 		case 'e':
    240  1.16    kleink 			LEGAL_ALT(ALT_O);
    241  1.16    kleink 			if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))
    242   1.3    kleink 				return (0);
    243   1.2    kleink 			break;
    244   1.2    kleink 
    245   1.3    kleink 		case 'k':	/* The hour (24-hour clock representation). */
    246  1.16    kleink 			LEGAL_ALT(0);
    247   1.3    kleink 			/* FALLTHROUGH */
    248   1.3    kleink 		case 'H':
    249  1.16    kleink 			LEGAL_ALT(ALT_O);
    250  1.16    kleink 			if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))
    251   1.3    kleink 				return (0);
    252   1.2    kleink 			break;
    253   1.2    kleink 
    254   1.3    kleink 		case 'l':	/* The hour (12-hour clock representation). */
    255  1.16    kleink 			LEGAL_ALT(0);
    256   1.3    kleink 			/* FALLTHROUGH */
    257   1.3    kleink 		case 'I':
    258  1.16    kleink 			LEGAL_ALT(ALT_O);
    259  1.16    kleink 			if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))
    260   1.3    kleink 				return (0);
    261  1.13        tv 			if (tm->tm_hour == 12)
    262  1.13        tv 				tm->tm_hour = 0;
    263   1.2    kleink 			break;
    264   1.2    kleink 
    265   1.3    kleink 		case 'j':	/* The day of year. */
    266  1.16    kleink 			LEGAL_ALT(0);
    267  1.16    kleink 			if (!(conv_num(&bp, &i, 1, 366)))
    268   1.3    kleink 				return (0);
    269  1.13        tv 			tm->tm_yday = i - 1;
    270   1.2    kleink 			break;
    271   1.2    kleink 
    272   1.3    kleink 		case 'M':	/* The minute. */
    273  1.16    kleink 			LEGAL_ALT(ALT_O);
    274  1.16    kleink 			if (!(conv_num(&bp, &tm->tm_min, 0, 59)))
    275   1.3    kleink 				return (0);
    276   1.2    kleink 			break;
    277   1.2    kleink 
    278   1.3    kleink 		case 'm':	/* The month. */
    279  1.16    kleink 			LEGAL_ALT(ALT_O);
    280  1.16    kleink 			if (!(conv_num(&bp, &i, 1, 12)))
    281   1.3    kleink 				return (0);
    282  1.13        tv 			tm->tm_mon = i - 1;
    283   1.2    kleink 			break;
    284   1.2    kleink 
    285   1.3    kleink 		case 'p':	/* The locale's equivalent of AM/PM. */
    286  1.16    kleink 			LEGAL_ALT(0);
    287   1.3    kleink 			/* AM? */
    288  1.22  christos 			if (strcasecmp(_ctloc(am_pm[0]),
    289  1.22  christos 			    (const char *)bp) == 0) {
    290  1.13        tv 				if (tm->tm_hour > 11)
    291   1.3    kleink 					return (0);
    292   1.3    kleink 
    293   1.3    kleink 				bp += strlen(_ctloc(am_pm[0]));
    294   1.2    kleink 				break;
    295   1.2    kleink 			}
    296   1.3    kleink 			/* PM? */
    297  1.22  christos 			else if (strcasecmp(_ctloc(am_pm[1]),
    298  1.22  christos 			    (const char *)bp) == 0) {
    299  1.13        tv 				if (tm->tm_hour > 11)
    300   1.3    kleink 					return (0);
    301   1.2    kleink 
    302  1.13        tv 				tm->tm_hour += 12;
    303   1.3    kleink 				bp += strlen(_ctloc(am_pm[1]));
    304   1.2    kleink 				break;
    305   1.2    kleink 			}
    306   1.2    kleink 
    307   1.3    kleink 			/* Nothing matched. */
    308   1.3    kleink 			return (0);
    309   1.2    kleink 
    310   1.3    kleink 		case 'S':	/* The seconds. */
    311  1.16    kleink 			LEGAL_ALT(ALT_O);
    312  1.16    kleink 			if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))
    313   1.3    kleink 				return (0);
    314   1.3    kleink 			break;
    315   1.1       mrg 
    316   1.3    kleink 		case 'U':	/* The week of year, beginning on sunday. */
    317   1.3    kleink 		case 'W':	/* The week of year, beginning on monday. */
    318  1.16    kleink 			LEGAL_ALT(ALT_O);
    319   1.3    kleink 			/*
    320   1.3    kleink 			 * XXX This is bogus, as we can not assume any valid
    321   1.3    kleink 			 * information present in the tm structure at this
    322   1.3    kleink 			 * point to calculate a real value, so just check the
    323   1.3    kleink 			 * range for now.
    324   1.3    kleink 			 */
    325  1.16    kleink 			 if (!(conv_num(&bp, &i, 0, 53)))
    326   1.3    kleink 				return (0);
    327   1.3    kleink 			 break;
    328   1.2    kleink 
    329   1.3    kleink 		case 'w':	/* The day of week, beginning on sunday. */
    330  1.16    kleink 			LEGAL_ALT(ALT_O);
    331  1.16    kleink 			if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))
    332   1.3    kleink 				return (0);
    333   1.2    kleink 			break;
    334   1.2    kleink 
    335   1.3    kleink 		case 'Y':	/* The year. */
    336  1.16    kleink 			LEGAL_ALT(ALT_E);
    337  1.18        tv 			if (!(conv_num(&bp, &i, 0, 9999)))
    338   1.3    kleink 				return (0);
    339   1.2    kleink 
    340   1.8   mycroft 			tm->tm_year = i - TM_YEAR_BASE;
    341   1.3    kleink 			break;
    342   1.2    kleink 
    343   1.9   mycroft 		case 'y':	/* The year within 100 years of the epoch. */
    344  1.16    kleink 			LEGAL_ALT(ALT_E | ALT_O);
    345  1.16    kleink 			if (!(conv_num(&bp, &i, 0, 99)))
    346   1.3    kleink 				return (0);
    347   1.8   mycroft 
    348  1.13        tv 			if (split_year) {
    349  1.13        tv 				tm->tm_year = ((tm->tm_year / 100) * 100) + i;
    350  1.13        tv 				break;
    351  1.13        tv 			}
    352  1.13        tv 			split_year = 1;
    353  1.10   mycroft 			if (i <= 68)
    354   1.8   mycroft 				tm->tm_year = i + 2000 - TM_YEAR_BASE;
    355   1.8   mycroft 			else
    356   1.8   mycroft 				tm->tm_year = i + 1900 - TM_YEAR_BASE;
    357   1.3    kleink 			break;
    358   1.2    kleink 
    359   1.3    kleink 		/*
    360   1.3    kleink 		 * Miscellaneous conversions.
    361   1.3    kleink 		 */
    362   1.3    kleink 		case 'n':	/* Any kind of white-space. */
    363   1.3    kleink 		case 't':
    364  1.16    kleink 			LEGAL_ALT(0);
    365   1.3    kleink 			while (isspace(*bp))
    366   1.3    kleink 				bp++;
    367   1.2    kleink 			break;
    368   1.2    kleink 
    369   1.1       mrg 
    370   1.3    kleink 		default:	/* Unknown/unsupported conversion. */
    371   1.3    kleink 			return (0);
    372   1.3    kleink 		}
    373   1.2    kleink 
    374   1.2    kleink 
    375   1.3    kleink 	}
    376   1.2    kleink 
    377  1.17  christos 	/* LINTED functional specification */
    378   1.3    kleink 	return ((char *)bp);
    379   1.3    kleink }
    380   1.2    kleink 
    381   1.2    kleink 
    382   1.3    kleink static int
    383  1.16    kleink conv_num(buf, dest, llim, ulim)
    384  1.20     itohy 	const unsigned char **buf;
    385   1.3    kleink 	int *dest;
    386   1.3    kleink 	int llim, ulim;
    387   1.3    kleink {
    388  1.18        tv 	int result = 0;
    389  1.18        tv 
    390  1.18        tv 	/* The limit also determines the number of valid digits. */
    391  1.18        tv 	int rulim = ulim;
    392   1.2    kleink 
    393   1.3    kleink 	if (**buf < '0' || **buf > '9')
    394   1.3    kleink 		return (0);
    395   1.2    kleink 
    396   1.3    kleink 	do {
    397  1.18        tv 		result *= 10;
    398  1.18        tv 		result += *(*buf)++ - '0';
    399  1.18        tv 		rulim /= 10;
    400  1.18        tv 	} while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
    401   1.2    kleink 
    402  1.18        tv 	if (result < llim || result > ulim)
    403   1.3    kleink 		return (0);
    404   1.1       mrg 
    405  1.18        tv 	*dest = result;
    406   1.3    kleink 	return (1);
    407   1.1       mrg }
    408