strptime.c revision 1.29 1 1.29 christos /* $NetBSD: strptime.c,v 1.29 2008/11/04 18:37:28 christos 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.29 christos __RCSID("$NetBSD: strptime.c,v 1.29 2008/11/04 18:37:28 christos 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.29 christos #include "private.h"
45 1.7 jtc
46 1.7 jtc #ifdef __weak_alias
47 1.19 mycroft __weak_alias(strptime,_strptime)
48 1.7 jtc #endif
49 1.3 kleink
50 1.21 cgd #define _ctloc(x) (_CurrentTimeLocale->x)
51 1.3 kleink
52 1.3 kleink /*
53 1.3 kleink * We do not implement alternate representations. However, we always
54 1.3 kleink * check whether a given modifier is allowed for a certain conversion.
55 1.3 kleink */
56 1.16 kleink #define ALT_E 0x01
57 1.16 kleink #define ALT_O 0x02
58 1.24 dsl #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
59 1.3 kleink
60 1.29 christos static char gmt[] = { "GMT" };
61 1.29 christos static char utc[] = { "UTC" };
62 1.3 kleink
63 1.24 dsl static const u_char *conv_num(const unsigned char *, int *, uint, uint);
64 1.23 dsl static const u_char *find_string(const u_char *, int *, const char * const *,
65 1.23 dsl const char * const *, int);
66 1.3 kleink
67 1.1 mrg
68 1.2 kleink char *
69 1.23 dsl strptime(const char *buf, const char *fmt, struct tm *tm)
70 1.1 mrg {
71 1.20 itohy unsigned char c;
72 1.20 itohy const unsigned char *bp;
73 1.29 christos int alt_format, i, split_year = 0, neg, offs;
74 1.23 dsl const char *new_fmt;
75 1.3 kleink
76 1.22 christos bp = (const u_char *)buf;
77 1.3 kleink
78 1.24 dsl while (bp != NULL && (c = *fmt++) != '\0') {
79 1.3 kleink /* Clear `alternate' modifier prior to new conversion. */
80 1.3 kleink alt_format = 0;
81 1.23 dsl i = 0;
82 1.3 kleink
83 1.3 kleink /* Eat up white-space. */
84 1.3 kleink if (isspace(c)) {
85 1.3 kleink while (isspace(*bp))
86 1.3 kleink bp++;
87 1.2 kleink continue;
88 1.2 kleink }
89 1.23 dsl
90 1.24 dsl if (c != '%')
91 1.3 kleink goto literal;
92 1.3 kleink
93 1.3 kleink
94 1.3 kleink again: switch (c = *fmt++) {
95 1.3 kleink case '%': /* "%%" is converted to "%". */
96 1.3 kleink literal:
97 1.14 tv if (c != *bp++)
98 1.24 dsl return NULL;
99 1.23 dsl LEGAL_ALT(0);
100 1.23 dsl continue;
101 1.3 kleink
102 1.3 kleink /*
103 1.3 kleink * "Alternative" modifiers. Just set the appropriate flag
104 1.3 kleink * and start over again.
105 1.3 kleink */
106 1.3 kleink case 'E': /* "%E?" alternative conversion modifier. */
107 1.16 kleink LEGAL_ALT(0);
108 1.16 kleink alt_format |= ALT_E;
109 1.3 kleink goto again;
110 1.3 kleink
111 1.3 kleink case 'O': /* "%O?" alternative conversion modifier. */
112 1.16 kleink LEGAL_ALT(0);
113 1.16 kleink alt_format |= ALT_O;
114 1.3 kleink goto again;
115 1.23 dsl
116 1.3 kleink /*
117 1.3 kleink * "Complex" conversion rules, implemented through recursion.
118 1.3 kleink */
119 1.3 kleink case 'c': /* Date and time, using the locale's format. */
120 1.23 dsl new_fmt = _ctloc(d_t_fmt);
121 1.23 dsl goto recurse;
122 1.3 kleink
123 1.3 kleink case 'D': /* The date as "%m/%d/%y". */
124 1.23 dsl new_fmt = "%m/%d/%y";
125 1.16 kleink LEGAL_ALT(0);
126 1.23 dsl goto recurse;
127 1.18 tv
128 1.27 ginsbach case 'F': /* The date as "%Y-%m-%d". */
129 1.27 ginsbach new_fmt = "%Y-%m-%d";
130 1.27 ginsbach LEGAL_ALT(0);
131 1.27 ginsbach goto recurse;
132 1.27 ginsbach
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.24 dsl i = 20;
180 1.23 dsl bp = conv_num(bp, &i, 0, 99);
181 1.2 kleink
182 1.24 dsl i = i * 100 - TM_YEAR_BASE;
183 1.24 dsl if (split_year)
184 1.24 dsl i += tm->tm_year % 100;
185 1.24 dsl split_year = 1;
186 1.24 dsl tm->tm_year = i;
187 1.23 dsl LEGAL_ALT(ALT_E);
188 1.23 dsl continue;
189 1.2 kleink
190 1.3 kleink case 'd': /* The day of month. */
191 1.3 kleink case 'e':
192 1.23 dsl bp = conv_num(bp, &tm->tm_mday, 1, 31);
193 1.16 kleink LEGAL_ALT(ALT_O);
194 1.23 dsl continue;
195 1.2 kleink
196 1.3 kleink case 'k': /* The hour (24-hour clock representation). */
197 1.16 kleink LEGAL_ALT(0);
198 1.3 kleink /* FALLTHROUGH */
199 1.3 kleink case 'H':
200 1.23 dsl bp = conv_num(bp, &tm->tm_hour, 0, 23);
201 1.16 kleink LEGAL_ALT(ALT_O);
202 1.23 dsl continue;
203 1.2 kleink
204 1.3 kleink case 'l': /* The hour (12-hour clock representation). */
205 1.16 kleink LEGAL_ALT(0);
206 1.3 kleink /* FALLTHROUGH */
207 1.3 kleink case 'I':
208 1.23 dsl bp = conv_num(bp, &tm->tm_hour, 1, 12);
209 1.13 tv if (tm->tm_hour == 12)
210 1.13 tv tm->tm_hour = 0;
211 1.23 dsl LEGAL_ALT(ALT_O);
212 1.23 dsl continue;
213 1.2 kleink
214 1.3 kleink case 'j': /* The day of year. */
215 1.23 dsl i = 1;
216 1.23 dsl bp = conv_num(bp, &i, 1, 366);
217 1.23 dsl tm->tm_yday = i - 1;
218 1.16 kleink LEGAL_ALT(0);
219 1.23 dsl continue;
220 1.2 kleink
221 1.3 kleink case 'M': /* The minute. */
222 1.23 dsl bp = conv_num(bp, &tm->tm_min, 0, 59);
223 1.16 kleink LEGAL_ALT(ALT_O);
224 1.23 dsl continue;
225 1.2 kleink
226 1.3 kleink case 'm': /* The month. */
227 1.23 dsl i = 1;
228 1.23 dsl bp = conv_num(bp, &i, 1, 12);
229 1.23 dsl tm->tm_mon = i - 1;
230 1.16 kleink LEGAL_ALT(ALT_O);
231 1.23 dsl continue;
232 1.2 kleink
233 1.3 kleink case 'p': /* The locale's equivalent of AM/PM. */
234 1.24 dsl bp = find_string(bp, &i, _ctloc(am_pm), NULL, 2);
235 1.23 dsl if (tm->tm_hour > 11)
236 1.24 dsl return NULL;
237 1.23 dsl tm->tm_hour += i * 12;
238 1.16 kleink LEGAL_ALT(0);
239 1.23 dsl continue;
240 1.2 kleink
241 1.3 kleink case 'S': /* The seconds. */
242 1.23 dsl bp = conv_num(bp, &tm->tm_sec, 0, 61);
243 1.16 kleink LEGAL_ALT(ALT_O);
244 1.23 dsl continue;
245 1.1 mrg
246 1.3 kleink case 'U': /* The week of year, beginning on sunday. */
247 1.3 kleink case 'W': /* The week of year, beginning on monday. */
248 1.3 kleink /*
249 1.3 kleink * XXX This is bogus, as we can not assume any valid
250 1.3 kleink * information present in the tm structure at this
251 1.3 kleink * point to calculate a real value, so just check the
252 1.3 kleink * range for now.
253 1.3 kleink */
254 1.23 dsl bp = conv_num(bp, &i, 0, 53);
255 1.23 dsl LEGAL_ALT(ALT_O);
256 1.23 dsl continue;
257 1.2 kleink
258 1.3 kleink case 'w': /* The day of week, beginning on sunday. */
259 1.23 dsl bp = conv_num(bp, &tm->tm_wday, 0, 6);
260 1.16 kleink LEGAL_ALT(ALT_O);
261 1.23 dsl continue;
262 1.2 kleink
263 1.29 christos case 'u': /* The day of week, monday = 1. */
264 1.29 christos bp = conv_num(bp, &i, 1, 7);
265 1.29 christos tm->tm_wday = i % 7;
266 1.29 christos LEGAL_ALT(ALT_O);
267 1.29 christos continue;
268 1.29 christos
269 1.29 christos case 'g': /* The year corresponding to the ISO week
270 1.29 christos * number but without the century.
271 1.29 christos */
272 1.29 christos bp = conv_num(bp, &i, 0, 99);
273 1.29 christos continue;
274 1.29 christos
275 1.29 christos case 'G': /* The year corresponding to the ISO week
276 1.29 christos * number with century.
277 1.29 christos */
278 1.29 christos do
279 1.29 christos *bp++;
280 1.29 christos while (isdigit(*bp));
281 1.29 christos continue;
282 1.29 christos
283 1.29 christos case 'V': /* The ISO 8601:1988 week number as decimal */
284 1.29 christos bp = conv_num(bp, &i, 0, 53);
285 1.29 christos continue;
286 1.29 christos
287 1.3 kleink case 'Y': /* The year. */
288 1.24 dsl i = TM_YEAR_BASE; /* just for data sanity... */
289 1.23 dsl bp = conv_num(bp, &i, 0, 9999);
290 1.8 mycroft tm->tm_year = i - TM_YEAR_BASE;
291 1.23 dsl LEGAL_ALT(ALT_E);
292 1.23 dsl continue;
293 1.2 kleink
294 1.9 mycroft case 'y': /* The year within 100 years of the epoch. */
295 1.24 dsl /* LEGAL_ALT(ALT_E | ALT_O); */
296 1.23 dsl bp = conv_num(bp, &i, 0, 99);
297 1.8 mycroft
298 1.24 dsl if (split_year)
299 1.24 dsl /* preserve century */
300 1.24 dsl i += (tm->tm_year / 100) * 100;
301 1.24 dsl else {
302 1.24 dsl split_year = 1;
303 1.24 dsl if (i <= 68)
304 1.24 dsl i = i + 2000 - TM_YEAR_BASE;
305 1.24 dsl else
306 1.24 dsl i = i + 1900 - TM_YEAR_BASE;
307 1.13 tv }
308 1.24 dsl tm->tm_year = i;
309 1.23 dsl continue;
310 1.2 kleink
311 1.26 ginsbach case 'Z':
312 1.26 ginsbach tzset();
313 1.26 ginsbach if (strncmp((const char *)bp, gmt, 3) == 0) {
314 1.26 ginsbach tm->tm_isdst = 0;
315 1.26 ginsbach #ifdef TM_GMTOFF
316 1.26 ginsbach tm->TM_GMTOFF = 0;
317 1.26 ginsbach #endif
318 1.26 ginsbach #ifdef TM_ZONE
319 1.26 ginsbach tm->TM_ZONE = gmt;
320 1.26 ginsbach #endif
321 1.26 ginsbach bp += 3;
322 1.26 ginsbach } else {
323 1.26 ginsbach const unsigned char *ep;
324 1.26 ginsbach
325 1.26 ginsbach ep = find_string(bp, &i,
326 1.26 ginsbach (const char * const *)tzname,
327 1.26 ginsbach NULL, 2);
328 1.26 ginsbach if (ep != NULL) {
329 1.26 ginsbach tm->tm_isdst = i;
330 1.26 ginsbach #ifdef TM_GMTOFF
331 1.26 ginsbach tm->TM_GMTOFF = -(timezone);
332 1.26 ginsbach #endif
333 1.26 ginsbach #ifdef TM_ZONE
334 1.26 ginsbach tm->TM_ZONE = tzname[i];
335 1.26 ginsbach #endif
336 1.26 ginsbach }
337 1.26 ginsbach bp = ep;
338 1.26 ginsbach }
339 1.26 ginsbach continue;
340 1.26 ginsbach
341 1.29 christos case 'z':
342 1.29 christos /*
343 1.29 christos * We recognize all ISO 8601 formats:
344 1.29 christos * Z = Zulu time/UTC
345 1.29 christos * [+-]hhmm
346 1.29 christos * [+-]hh:mm
347 1.29 christos * [+-]hh
348 1.29 christos */
349 1.29 christos while (isspace(*bp))
350 1.29 christos bp++;
351 1.29 christos
352 1.29 christos switch (*bp++) {
353 1.29 christos case 'Z':
354 1.29 christos tm->tm_isdst = 0;
355 1.29 christos #ifdef TM_GMTOFF
356 1.29 christos tm->TM_GMTOFF = 0;
357 1.29 christos #endif
358 1.29 christos #ifdef TM_ZONE
359 1.29 christos tm->TM_ZONE = utc;
360 1.29 christos #endif
361 1.29 christos continue;
362 1.29 christos case '+':
363 1.29 christos neg = 0;
364 1.29 christos break;
365 1.29 christos case '-':
366 1.29 christos neg = 1;
367 1.29 christos break;
368 1.29 christos default:
369 1.29 christos return NULL;
370 1.29 christos }
371 1.29 christos offs = 0;
372 1.29 christos for (i = 0; i < 4; ) {
373 1.29 christos if (isdigit(*bp)) {
374 1.29 christos offs = offs * 10 + (*bp++ - '0');
375 1.29 christos i++;
376 1.29 christos continue;
377 1.29 christos }
378 1.29 christos if (i == 2 && *bp == ':') {
379 1.29 christos bp++;
380 1.29 christos continue;
381 1.29 christos }
382 1.29 christos break;
383 1.29 christos }
384 1.29 christos switch (i) {
385 1.29 christos case 2:
386 1.29 christos offs *= 100;
387 1.29 christos break;
388 1.29 christos case 4:
389 1.29 christos i = offs % 100;
390 1.29 christos if (i >= 60)
391 1.29 christos return NULL;
392 1.29 christos /* Convert minutes into decimal */
393 1.29 christos offs = (offs / 100) * 100 + (i * 50) / 30;
394 1.29 christos break;
395 1.29 christos default:
396 1.29 christos return NULL;
397 1.29 christos }
398 1.29 christos tm->tm_isdst = 0; /* XXX */
399 1.29 christos #ifdef TM_GMTOFF
400 1.29 christos tm->TM_GMTOFF = offs;
401 1.29 christos #endif
402 1.29 christos #ifdef TM_ZONE
403 1.29 christos tm->TM_ZONE = NULL; /* XXX */
404 1.29 christos #endif
405 1.29 christos continue;
406 1.29 christos
407 1.3 kleink /*
408 1.3 kleink * Miscellaneous conversions.
409 1.3 kleink */
410 1.3 kleink case 'n': /* Any kind of white-space. */
411 1.3 kleink case 't':
412 1.3 kleink while (isspace(*bp))
413 1.3 kleink bp++;
414 1.23 dsl LEGAL_ALT(0);
415 1.23 dsl continue;
416 1.2 kleink
417 1.1 mrg
418 1.3 kleink default: /* Unknown/unsupported conversion. */
419 1.24 dsl return NULL;
420 1.3 kleink }
421 1.3 kleink }
422 1.2 kleink
423 1.25 christos return __UNCONST(bp);
424 1.3 kleink }
425 1.2 kleink
426 1.2 kleink
427 1.23 dsl static const u_char *
428 1.24 dsl conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
429 1.3 kleink {
430 1.24 dsl uint result = 0;
431 1.23 dsl unsigned char ch;
432 1.18 tv
433 1.18 tv /* The limit also determines the number of valid digits. */
434 1.24 dsl uint rulim = ulim;
435 1.2 kleink
436 1.23 dsl ch = *buf;
437 1.23 dsl if (ch < '0' || ch > '9')
438 1.24 dsl return NULL;
439 1.2 kleink
440 1.3 kleink do {
441 1.18 tv result *= 10;
442 1.23 dsl result += ch - '0';
443 1.18 tv rulim /= 10;
444 1.23 dsl ch = *++buf;
445 1.23 dsl } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
446 1.2 kleink
447 1.18 tv if (result < llim || result > ulim)
448 1.24 dsl return NULL;
449 1.1 mrg
450 1.18 tv *dest = result;
451 1.23 dsl return buf;
452 1.23 dsl }
453 1.23 dsl
454 1.23 dsl static const u_char *
455 1.23 dsl find_string(const u_char *bp, int *tgt, const char * const *n1,
456 1.23 dsl const char * const *n2, int c)
457 1.23 dsl {
458 1.23 dsl int i;
459 1.23 dsl unsigned int len;
460 1.23 dsl
461 1.24 dsl /* check full name - then abbreviated ones */
462 1.24 dsl for (; n1 != NULL; n1 = n2, n2 = NULL) {
463 1.24 dsl for (i = 0; i < c; i++, n1++) {
464 1.24 dsl len = strlen(*n1);
465 1.24 dsl if (strncasecmp(*n1, (const char *)bp, len) == 0) {
466 1.24 dsl *tgt = i;
467 1.24 dsl return bp + len;
468 1.24 dsl }
469 1.24 dsl }
470 1.23 dsl }
471 1.24 dsl
472 1.24 dsl /* Nothing matched */
473 1.24 dsl return NULL;
474 1.1 mrg }
475