strptime.c revision 1.21 1 1.21 cgd /* $NetBSD: strptime.c,v 1.21 2000/12/19 22:20:48 cgd 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.21 cgd __RCSID("$NetBSD: strptime.c,v 1.21 2000/12/19 22:20:48 cgd 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.3 kleink bp = 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.3 kleink if (!(bp = strptime(bp, _ctloc(d_t_fmt), tm)))
125 1.3 kleink return (0);
126 1.3 kleink break;
127 1.3 kleink
128 1.3 kleink case 'D': /* The date as "%m/%d/%y". */
129 1.16 kleink LEGAL_ALT(0);
130 1.3 kleink if (!(bp = strptime(bp, "%m/%d/%y", tm)))
131 1.3 kleink return (0);
132 1.3 kleink break;
133 1.18 tv
134 1.3 kleink case 'R': /* The time as "%H:%M". */
135 1.16 kleink LEGAL_ALT(0);
136 1.3 kleink if (!(bp = strptime(bp, "%H:%M", tm)))
137 1.3 kleink return (0);
138 1.3 kleink break;
139 1.3 kleink
140 1.3 kleink case 'r': /* The time in 12-hour clock representation. */
141 1.16 kleink LEGAL_ALT(0);
142 1.3 kleink if (!(bp = strptime(bp, _ctloc(t_fmt_ampm), tm)))
143 1.3 kleink return (0);
144 1.3 kleink break;
145 1.3 kleink
146 1.3 kleink case 'T': /* The time as "%H:%M:%S". */
147 1.16 kleink LEGAL_ALT(0);
148 1.3 kleink if (!(bp = strptime(bp, "%H:%M:%S", tm)))
149 1.3 kleink return (0);
150 1.3 kleink break;
151 1.3 kleink
152 1.3 kleink case 'X': /* The time, using the locale's format. */
153 1.16 kleink LEGAL_ALT(ALT_E);
154 1.3 kleink if (!(bp = strptime(bp, _ctloc(t_fmt), tm)))
155 1.3 kleink return (0);
156 1.3 kleink break;
157 1.3 kleink
158 1.3 kleink case 'x': /* The date, using the locale's format. */
159 1.16 kleink LEGAL_ALT(ALT_E);
160 1.3 kleink if (!(bp = strptime(bp, _ctloc(d_fmt), tm)))
161 1.3 kleink return (0);
162 1.3 kleink break;
163 1.3 kleink
164 1.3 kleink /*
165 1.3 kleink * "Elementary" conversion rules.
166 1.3 kleink */
167 1.3 kleink case 'A': /* The day of week, using the locale's form. */
168 1.3 kleink case 'a':
169 1.16 kleink LEGAL_ALT(0);
170 1.3 kleink for (i = 0; i < 7; i++) {
171 1.3 kleink /* Full name. */
172 1.3 kleink len = strlen(_ctloc(day[i]));
173 1.14 tv if (strncasecmp(_ctloc(day[i]), bp, len) == 0)
174 1.3 kleink break;
175 1.2 kleink
176 1.3 kleink /* Abbreviated name. */
177 1.3 kleink len = strlen(_ctloc(abday[i]));
178 1.14 tv if (strncasecmp(_ctloc(abday[i]), bp, len) == 0)
179 1.3 kleink break;
180 1.3 kleink }
181 1.3 kleink
182 1.3 kleink /* Nothing matched. */
183 1.3 kleink if (i == 7)
184 1.3 kleink return (0);
185 1.3 kleink
186 1.3 kleink tm->tm_wday = i;
187 1.3 kleink bp += len;
188 1.2 kleink break;
189 1.2 kleink
190 1.3 kleink case 'B': /* The month, using the locale's form. */
191 1.3 kleink case 'b':
192 1.3 kleink case 'h':
193 1.16 kleink LEGAL_ALT(0);
194 1.3 kleink for (i = 0; i < 12; i++) {
195 1.3 kleink /* Full name. */
196 1.3 kleink len = strlen(_ctloc(mon[i]));
197 1.14 tv if (strncasecmp(_ctloc(mon[i]), bp, len) == 0)
198 1.3 kleink break;
199 1.3 kleink
200 1.3 kleink /* Abbreviated name. */
201 1.3 kleink len = strlen(_ctloc(abmon[i]));
202 1.14 tv if (strncasecmp(_ctloc(abmon[i]), bp, len) == 0)
203 1.3 kleink break;
204 1.3 kleink }
205 1.3 kleink
206 1.3 kleink /* Nothing matched. */
207 1.3 kleink if (i == 12)
208 1.3 kleink return (0);
209 1.3 kleink
210 1.3 kleink tm->tm_mon = i;
211 1.3 kleink bp += len;
212 1.2 kleink break;
213 1.2 kleink
214 1.3 kleink case 'C': /* The century number. */
215 1.16 kleink LEGAL_ALT(ALT_E);
216 1.16 kleink if (!(conv_num(&bp, &i, 0, 99)))
217 1.3 kleink return (0);
218 1.2 kleink
219 1.13 tv if (split_year) {
220 1.13 tv tm->tm_year = (tm->tm_year % 100) + (i * 100);
221 1.13 tv } else {
222 1.13 tv tm->tm_year = i * 100;
223 1.13 tv split_year = 1;
224 1.13 tv }
225 1.2 kleink break;
226 1.2 kleink
227 1.3 kleink case 'd': /* The day of month. */
228 1.3 kleink case 'e':
229 1.16 kleink LEGAL_ALT(ALT_O);
230 1.16 kleink if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))
231 1.3 kleink return (0);
232 1.2 kleink break;
233 1.2 kleink
234 1.3 kleink case 'k': /* The hour (24-hour clock representation). */
235 1.16 kleink LEGAL_ALT(0);
236 1.3 kleink /* FALLTHROUGH */
237 1.3 kleink case 'H':
238 1.16 kleink LEGAL_ALT(ALT_O);
239 1.16 kleink if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))
240 1.3 kleink return (0);
241 1.2 kleink break;
242 1.2 kleink
243 1.3 kleink case 'l': /* The hour (12-hour clock representation). */
244 1.16 kleink LEGAL_ALT(0);
245 1.3 kleink /* FALLTHROUGH */
246 1.3 kleink case 'I':
247 1.16 kleink LEGAL_ALT(ALT_O);
248 1.16 kleink if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))
249 1.3 kleink return (0);
250 1.13 tv if (tm->tm_hour == 12)
251 1.13 tv tm->tm_hour = 0;
252 1.2 kleink break;
253 1.2 kleink
254 1.3 kleink case 'j': /* The day of year. */
255 1.16 kleink LEGAL_ALT(0);
256 1.16 kleink if (!(conv_num(&bp, &i, 1, 366)))
257 1.3 kleink return (0);
258 1.13 tv tm->tm_yday = i - 1;
259 1.2 kleink break;
260 1.2 kleink
261 1.3 kleink case 'M': /* The minute. */
262 1.16 kleink LEGAL_ALT(ALT_O);
263 1.16 kleink if (!(conv_num(&bp, &tm->tm_min, 0, 59)))
264 1.3 kleink return (0);
265 1.2 kleink break;
266 1.2 kleink
267 1.3 kleink case 'm': /* The month. */
268 1.16 kleink LEGAL_ALT(ALT_O);
269 1.16 kleink if (!(conv_num(&bp, &i, 1, 12)))
270 1.3 kleink return (0);
271 1.13 tv tm->tm_mon = i - 1;
272 1.2 kleink break;
273 1.2 kleink
274 1.3 kleink case 'p': /* The locale's equivalent of AM/PM. */
275 1.16 kleink LEGAL_ALT(0);
276 1.3 kleink /* AM? */
277 1.14 tv if (strcasecmp(_ctloc(am_pm[0]), bp) == 0) {
278 1.13 tv if (tm->tm_hour > 11)
279 1.3 kleink return (0);
280 1.3 kleink
281 1.3 kleink bp += strlen(_ctloc(am_pm[0]));
282 1.2 kleink break;
283 1.2 kleink }
284 1.3 kleink /* PM? */
285 1.14 tv else if (strcasecmp(_ctloc(am_pm[1]), bp) == 0) {
286 1.13 tv if (tm->tm_hour > 11)
287 1.3 kleink return (0);
288 1.2 kleink
289 1.13 tv tm->tm_hour += 12;
290 1.3 kleink bp += strlen(_ctloc(am_pm[1]));
291 1.2 kleink break;
292 1.2 kleink }
293 1.2 kleink
294 1.3 kleink /* Nothing matched. */
295 1.3 kleink return (0);
296 1.2 kleink
297 1.3 kleink case 'S': /* The seconds. */
298 1.16 kleink LEGAL_ALT(ALT_O);
299 1.16 kleink if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))
300 1.3 kleink return (0);
301 1.3 kleink break;
302 1.1 mrg
303 1.3 kleink case 'U': /* The week of year, beginning on sunday. */
304 1.3 kleink case 'W': /* The week of year, beginning on monday. */
305 1.16 kleink LEGAL_ALT(ALT_O);
306 1.3 kleink /*
307 1.3 kleink * XXX This is bogus, as we can not assume any valid
308 1.3 kleink * information present in the tm structure at this
309 1.3 kleink * point to calculate a real value, so just check the
310 1.3 kleink * range for now.
311 1.3 kleink */
312 1.16 kleink if (!(conv_num(&bp, &i, 0, 53)))
313 1.3 kleink return (0);
314 1.3 kleink break;
315 1.2 kleink
316 1.3 kleink case 'w': /* The day of week, beginning on sunday. */
317 1.16 kleink LEGAL_ALT(ALT_O);
318 1.16 kleink if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))
319 1.3 kleink return (0);
320 1.2 kleink break;
321 1.2 kleink
322 1.3 kleink case 'Y': /* The year. */
323 1.16 kleink LEGAL_ALT(ALT_E);
324 1.18 tv if (!(conv_num(&bp, &i, 0, 9999)))
325 1.3 kleink return (0);
326 1.2 kleink
327 1.8 mycroft tm->tm_year = i - TM_YEAR_BASE;
328 1.3 kleink break;
329 1.2 kleink
330 1.9 mycroft case 'y': /* The year within 100 years of the epoch. */
331 1.16 kleink LEGAL_ALT(ALT_E | ALT_O);
332 1.16 kleink if (!(conv_num(&bp, &i, 0, 99)))
333 1.3 kleink return (0);
334 1.8 mycroft
335 1.13 tv if (split_year) {
336 1.13 tv tm->tm_year = ((tm->tm_year / 100) * 100) + i;
337 1.13 tv break;
338 1.13 tv }
339 1.13 tv split_year = 1;
340 1.10 mycroft if (i <= 68)
341 1.8 mycroft tm->tm_year = i + 2000 - TM_YEAR_BASE;
342 1.8 mycroft else
343 1.8 mycroft tm->tm_year = i + 1900 - TM_YEAR_BASE;
344 1.3 kleink break;
345 1.2 kleink
346 1.3 kleink /*
347 1.3 kleink * Miscellaneous conversions.
348 1.3 kleink */
349 1.3 kleink case 'n': /* Any kind of white-space. */
350 1.3 kleink case 't':
351 1.16 kleink LEGAL_ALT(0);
352 1.3 kleink while (isspace(*bp))
353 1.3 kleink bp++;
354 1.2 kleink break;
355 1.2 kleink
356 1.1 mrg
357 1.3 kleink default: /* Unknown/unsupported conversion. */
358 1.3 kleink return (0);
359 1.3 kleink }
360 1.2 kleink
361 1.2 kleink
362 1.3 kleink }
363 1.2 kleink
364 1.17 christos /* LINTED functional specification */
365 1.3 kleink return ((char *)bp);
366 1.3 kleink }
367 1.2 kleink
368 1.2 kleink
369 1.3 kleink static int
370 1.16 kleink conv_num(buf, dest, llim, ulim)
371 1.20 itohy const unsigned char **buf;
372 1.3 kleink int *dest;
373 1.3 kleink int llim, ulim;
374 1.3 kleink {
375 1.18 tv int result = 0;
376 1.18 tv
377 1.18 tv /* The limit also determines the number of valid digits. */
378 1.18 tv int rulim = ulim;
379 1.2 kleink
380 1.3 kleink if (**buf < '0' || **buf > '9')
381 1.3 kleink return (0);
382 1.2 kleink
383 1.3 kleink do {
384 1.18 tv result *= 10;
385 1.18 tv result += *(*buf)++ - '0';
386 1.18 tv rulim /= 10;
387 1.18 tv } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
388 1.2 kleink
389 1.18 tv if (result < llim || result > ulim)
390 1.3 kleink return (0);
391 1.1 mrg
392 1.18 tv *dest = result;
393 1.3 kleink return (1);
394 1.1 mrg }
395