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