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