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