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