strptime.c revision 1.37 1 1.37 joerg /* $NetBSD: strptime.c,v 1.37 2013/04/21 17:45:47 joerg 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.37 joerg __RCSID("$NetBSD: strptime.c,v 1.37 2013/04/21 17:45:47 joerg 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.37 joerg #include "setlocale_local.h"
46 1.7 jtc
47 1.7 jtc #ifdef __weak_alias
48 1.19 mycroft __weak_alias(strptime,_strptime)
49 1.37 joerg __weak_alias(strptime_l, _strptime_l)
50 1.7 jtc #endif
51 1.3 kleink
52 1.37 joerg #define _TIME_LOCALE(loc) \
53 1.37 joerg ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
54 1.3 kleink
55 1.3 kleink /*
56 1.3 kleink * We do not implement alternate representations. However, we always
57 1.3 kleink * check whether a given modifier is allowed for a certain conversion.
58 1.3 kleink */
59 1.16 kleink #define ALT_E 0x01
60 1.16 kleink #define ALT_O 0x02
61 1.24 dsl #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
62 1.3 kleink
63 1.29 christos static char gmt[] = { "GMT" };
64 1.29 christos static char utc[] = { "UTC" };
65 1.32 ginsbach /* RFC-822/RFC-2822 */
66 1.32 ginsbach static const char * const nast[5] = {
67 1.32 ginsbach "EST", "CST", "MST", "PST", "\0\0\0"
68 1.32 ginsbach };
69 1.32 ginsbach static const char * const nadt[5] = {
70 1.32 ginsbach "EDT", "CDT", "MDT", "PDT", "\0\0\0"
71 1.32 ginsbach };
72 1.3 kleink
73 1.24 dsl static const u_char *conv_num(const unsigned char *, int *, uint, uint);
74 1.23 dsl static const u_char *find_string(const u_char *, int *, const char * const *,
75 1.23 dsl const char * const *, int);
76 1.3 kleink
77 1.37 joerg char *
78 1.37 joerg strptime(const char *buf, const char *fmt, struct tm *tm)
79 1.37 joerg {
80 1.37 joerg return strptime_l(buf, fmt, tm, *_current_locale());
81 1.37 joerg }
82 1.1 mrg
83 1.2 kleink char *
84 1.37 joerg strptime_l(const char *buf, const char *fmt, struct tm *tm, locale_t loc)
85 1.1 mrg {
86 1.20 itohy unsigned char c;
87 1.32 ginsbach const unsigned char *bp, *ep;
88 1.32 ginsbach int alt_format, i, split_year = 0, neg = 0, offs;
89 1.23 dsl const char *new_fmt;
90 1.3 kleink
91 1.37 joerg if (loc == NULL)
92 1.37 joerg loc = _C_locale;
93 1.37 joerg
94 1.22 christos bp = (const u_char *)buf;
95 1.3 kleink
96 1.24 dsl while (bp != NULL && (c = *fmt++) != '\0') {
97 1.3 kleink /* Clear `alternate' modifier prior to new conversion. */
98 1.3 kleink alt_format = 0;
99 1.23 dsl i = 0;
100 1.3 kleink
101 1.3 kleink /* Eat up white-space. */
102 1.3 kleink if (isspace(c)) {
103 1.3 kleink while (isspace(*bp))
104 1.3 kleink bp++;
105 1.2 kleink continue;
106 1.2 kleink }
107 1.23 dsl
108 1.24 dsl if (c != '%')
109 1.3 kleink goto literal;
110 1.3 kleink
111 1.3 kleink
112 1.3 kleink again: switch (c = *fmt++) {
113 1.3 kleink case '%': /* "%%" is converted to "%". */
114 1.3 kleink literal:
115 1.14 tv if (c != *bp++)
116 1.24 dsl return NULL;
117 1.23 dsl LEGAL_ALT(0);
118 1.23 dsl continue;
119 1.3 kleink
120 1.3 kleink /*
121 1.3 kleink * "Alternative" modifiers. Just set the appropriate flag
122 1.3 kleink * and start over again.
123 1.3 kleink */
124 1.3 kleink case 'E': /* "%E?" alternative conversion modifier. */
125 1.16 kleink LEGAL_ALT(0);
126 1.16 kleink alt_format |= ALT_E;
127 1.3 kleink goto again;
128 1.3 kleink
129 1.3 kleink case 'O': /* "%O?" alternative conversion modifier. */
130 1.16 kleink LEGAL_ALT(0);
131 1.16 kleink alt_format |= ALT_O;
132 1.3 kleink goto again;
133 1.23 dsl
134 1.3 kleink /*
135 1.3 kleink * "Complex" conversion rules, implemented through recursion.
136 1.3 kleink */
137 1.3 kleink case 'c': /* Date and time, using the locale's format. */
138 1.37 joerg new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
139 1.23 dsl goto recurse;
140 1.3 kleink
141 1.3 kleink case 'D': /* The date as "%m/%d/%y". */
142 1.23 dsl new_fmt = "%m/%d/%y";
143 1.16 kleink LEGAL_ALT(0);
144 1.23 dsl goto recurse;
145 1.18 tv
146 1.27 ginsbach case 'F': /* The date as "%Y-%m-%d". */
147 1.27 ginsbach new_fmt = "%Y-%m-%d";
148 1.27 ginsbach LEGAL_ALT(0);
149 1.27 ginsbach goto recurse;
150 1.27 ginsbach
151 1.3 kleink case 'R': /* The time as "%H:%M". */
152 1.23 dsl new_fmt = "%H:%M";
153 1.16 kleink LEGAL_ALT(0);
154 1.23 dsl goto recurse;
155 1.3 kleink
156 1.3 kleink case 'r': /* The time in 12-hour clock representation. */
157 1.37 joerg new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
158 1.16 kleink LEGAL_ALT(0);
159 1.23 dsl goto recurse;
160 1.3 kleink
161 1.3 kleink case 'T': /* The time as "%H:%M:%S". */
162 1.23 dsl new_fmt = "%H:%M:%S";
163 1.16 kleink LEGAL_ALT(0);
164 1.23 dsl goto recurse;
165 1.3 kleink
166 1.3 kleink case 'X': /* The time, using the locale's format. */
167 1.37 joerg new_fmt = _TIME_LOCALE(loc)->t_fmt;
168 1.23 dsl goto recurse;
169 1.3 kleink
170 1.3 kleink case 'x': /* The date, using the locale's format. */
171 1.37 joerg new_fmt = _TIME_LOCALE(loc)->d_fmt;
172 1.23 dsl recurse:
173 1.23 dsl bp = (const u_char *)strptime((const char *)bp,
174 1.23 dsl new_fmt, tm);
175 1.16 kleink LEGAL_ALT(ALT_E);
176 1.23 dsl continue;
177 1.3 kleink
178 1.3 kleink /*
179 1.3 kleink * "Elementary" conversion rules.
180 1.3 kleink */
181 1.3 kleink case 'A': /* The day of week, using the locale's form. */
182 1.3 kleink case 'a':
183 1.37 joerg bp = find_string(bp, &tm->tm_wday,
184 1.37 joerg _TIME_LOCALE(loc)->day, _TIME_LOCALE(loc)->abday, 7);
185 1.16 kleink LEGAL_ALT(0);
186 1.23 dsl continue;
187 1.2 kleink
188 1.3 kleink case 'B': /* The month, using the locale's form. */
189 1.3 kleink case 'b':
190 1.3 kleink case 'h':
191 1.37 joerg bp = find_string(bp, &tm->tm_mon,
192 1.37 joerg _TIME_LOCALE(loc)->mon, _TIME_LOCALE(loc)->abmon,
193 1.37 joerg 12);
194 1.16 kleink LEGAL_ALT(0);
195 1.23 dsl continue;
196 1.2 kleink
197 1.3 kleink case 'C': /* The century number. */
198 1.24 dsl i = 20;
199 1.23 dsl bp = conv_num(bp, &i, 0, 99);
200 1.2 kleink
201 1.24 dsl i = i * 100 - TM_YEAR_BASE;
202 1.24 dsl if (split_year)
203 1.24 dsl i += tm->tm_year % 100;
204 1.24 dsl split_year = 1;
205 1.24 dsl tm->tm_year = i;
206 1.23 dsl LEGAL_ALT(ALT_E);
207 1.23 dsl continue;
208 1.2 kleink
209 1.3 kleink case 'd': /* The day of month. */
210 1.3 kleink case 'e':
211 1.23 dsl bp = conv_num(bp, &tm->tm_mday, 1, 31);
212 1.16 kleink LEGAL_ALT(ALT_O);
213 1.23 dsl continue;
214 1.2 kleink
215 1.3 kleink case 'k': /* The hour (24-hour clock representation). */
216 1.16 kleink LEGAL_ALT(0);
217 1.3 kleink /* FALLTHROUGH */
218 1.3 kleink case 'H':
219 1.23 dsl bp = conv_num(bp, &tm->tm_hour, 0, 23);
220 1.16 kleink LEGAL_ALT(ALT_O);
221 1.23 dsl continue;
222 1.2 kleink
223 1.3 kleink case 'l': /* The hour (12-hour clock representation). */
224 1.16 kleink LEGAL_ALT(0);
225 1.3 kleink /* FALLTHROUGH */
226 1.3 kleink case 'I':
227 1.23 dsl bp = conv_num(bp, &tm->tm_hour, 1, 12);
228 1.13 tv if (tm->tm_hour == 12)
229 1.13 tv tm->tm_hour = 0;
230 1.23 dsl LEGAL_ALT(ALT_O);
231 1.23 dsl continue;
232 1.2 kleink
233 1.3 kleink case 'j': /* The day of year. */
234 1.23 dsl i = 1;
235 1.23 dsl bp = conv_num(bp, &i, 1, 366);
236 1.23 dsl tm->tm_yday = i - 1;
237 1.16 kleink LEGAL_ALT(0);
238 1.23 dsl continue;
239 1.2 kleink
240 1.3 kleink case 'M': /* The minute. */
241 1.23 dsl bp = conv_num(bp, &tm->tm_min, 0, 59);
242 1.16 kleink LEGAL_ALT(ALT_O);
243 1.23 dsl continue;
244 1.2 kleink
245 1.3 kleink case 'm': /* The month. */
246 1.23 dsl i = 1;
247 1.23 dsl bp = conv_num(bp, &i, 1, 12);
248 1.23 dsl tm->tm_mon = i - 1;
249 1.16 kleink LEGAL_ALT(ALT_O);
250 1.23 dsl continue;
251 1.2 kleink
252 1.3 kleink case 'p': /* The locale's equivalent of AM/PM. */
253 1.37 joerg bp = find_string(bp, &i, _TIME_LOCALE(loc)->am_pm,
254 1.37 joerg NULL, 2);
255 1.23 dsl if (tm->tm_hour > 11)
256 1.24 dsl return NULL;
257 1.23 dsl tm->tm_hour += i * 12;
258 1.16 kleink LEGAL_ALT(0);
259 1.23 dsl continue;
260 1.2 kleink
261 1.3 kleink case 'S': /* The seconds. */
262 1.23 dsl bp = conv_num(bp, &tm->tm_sec, 0, 61);
263 1.16 kleink LEGAL_ALT(ALT_O);
264 1.23 dsl continue;
265 1.1 mrg
266 1.33 ginsbach #ifndef TIME_MAX
267 1.33 ginsbach #define TIME_MAX INT64_MAX
268 1.33 ginsbach #endif
269 1.33 ginsbach case 's': /* seconds since the epoch */
270 1.33 ginsbach {
271 1.33 ginsbach time_t sse = 0;
272 1.33 ginsbach uint64_t rulim = TIME_MAX;
273 1.33 ginsbach
274 1.33 ginsbach if (*bp < '0' || *bp > '9') {
275 1.33 ginsbach bp = NULL;
276 1.33 ginsbach continue;
277 1.33 ginsbach }
278 1.33 ginsbach
279 1.33 ginsbach do {
280 1.33 ginsbach sse *= 10;
281 1.33 ginsbach sse += *bp++ - '0';
282 1.33 ginsbach rulim /= 10;
283 1.35 matt } while ((sse * 10 <= TIME_MAX) &&
284 1.33 ginsbach rulim && *bp >= '0' && *bp <= '9');
285 1.33 ginsbach
286 1.33 ginsbach if (sse < 0 || (uint64_t)sse > TIME_MAX) {
287 1.33 ginsbach bp = NULL;
288 1.33 ginsbach continue;
289 1.33 ginsbach }
290 1.33 ginsbach
291 1.33 ginsbach if (localtime_r(&sse, tm) == NULL)
292 1.33 ginsbach bp = NULL;
293 1.33 ginsbach }
294 1.33 ginsbach continue;
295 1.33 ginsbach
296 1.3 kleink case 'U': /* The week of year, beginning on sunday. */
297 1.3 kleink case 'W': /* The week of year, beginning on monday. */
298 1.3 kleink /*
299 1.3 kleink * XXX This is bogus, as we can not assume any valid
300 1.3 kleink * information present in the tm structure at this
301 1.3 kleink * point to calculate a real value, so just check the
302 1.3 kleink * range for now.
303 1.3 kleink */
304 1.23 dsl bp = conv_num(bp, &i, 0, 53);
305 1.23 dsl LEGAL_ALT(ALT_O);
306 1.23 dsl continue;
307 1.2 kleink
308 1.3 kleink case 'w': /* The day of week, beginning on sunday. */
309 1.23 dsl bp = conv_num(bp, &tm->tm_wday, 0, 6);
310 1.16 kleink LEGAL_ALT(ALT_O);
311 1.23 dsl continue;
312 1.2 kleink
313 1.29 christos case 'u': /* The day of week, monday = 1. */
314 1.29 christos bp = conv_num(bp, &i, 1, 7);
315 1.29 christos tm->tm_wday = i % 7;
316 1.29 christos LEGAL_ALT(ALT_O);
317 1.29 christos continue;
318 1.29 christos
319 1.29 christos case 'g': /* The year corresponding to the ISO week
320 1.29 christos * number but without the century.
321 1.29 christos */
322 1.29 christos bp = conv_num(bp, &i, 0, 99);
323 1.29 christos continue;
324 1.29 christos
325 1.29 christos case 'G': /* The year corresponding to the ISO week
326 1.29 christos * number with century.
327 1.29 christos */
328 1.29 christos do
329 1.30 christos bp++;
330 1.29 christos while (isdigit(*bp));
331 1.29 christos continue;
332 1.29 christos
333 1.29 christos case 'V': /* The ISO 8601:1988 week number as decimal */
334 1.29 christos bp = conv_num(bp, &i, 0, 53);
335 1.29 christos continue;
336 1.29 christos
337 1.3 kleink case 'Y': /* The year. */
338 1.24 dsl i = TM_YEAR_BASE; /* just for data sanity... */
339 1.23 dsl bp = conv_num(bp, &i, 0, 9999);
340 1.8 mycroft tm->tm_year = i - TM_YEAR_BASE;
341 1.23 dsl LEGAL_ALT(ALT_E);
342 1.23 dsl continue;
343 1.2 kleink
344 1.9 mycroft case 'y': /* The year within 100 years of the epoch. */
345 1.24 dsl /* LEGAL_ALT(ALT_E | ALT_O); */
346 1.23 dsl bp = conv_num(bp, &i, 0, 99);
347 1.8 mycroft
348 1.24 dsl if (split_year)
349 1.24 dsl /* preserve century */
350 1.24 dsl i += (tm->tm_year / 100) * 100;
351 1.24 dsl else {
352 1.24 dsl split_year = 1;
353 1.24 dsl if (i <= 68)
354 1.24 dsl i = i + 2000 - TM_YEAR_BASE;
355 1.24 dsl else
356 1.24 dsl i = i + 1900 - TM_YEAR_BASE;
357 1.13 tv }
358 1.24 dsl tm->tm_year = i;
359 1.23 dsl continue;
360 1.2 kleink
361 1.26 ginsbach case 'Z':
362 1.26 ginsbach tzset();
363 1.26 ginsbach if (strncmp((const char *)bp, gmt, 3) == 0) {
364 1.26 ginsbach tm->tm_isdst = 0;
365 1.26 ginsbach #ifdef TM_GMTOFF
366 1.26 ginsbach tm->TM_GMTOFF = 0;
367 1.26 ginsbach #endif
368 1.26 ginsbach #ifdef TM_ZONE
369 1.26 ginsbach tm->TM_ZONE = gmt;
370 1.26 ginsbach #endif
371 1.26 ginsbach bp += 3;
372 1.26 ginsbach } else {
373 1.26 ginsbach ep = find_string(bp, &i,
374 1.26 ginsbach (const char * const *)tzname,
375 1.26 ginsbach NULL, 2);
376 1.26 ginsbach if (ep != NULL) {
377 1.26 ginsbach tm->tm_isdst = i;
378 1.26 ginsbach #ifdef TM_GMTOFF
379 1.26 ginsbach tm->TM_GMTOFF = -(timezone);
380 1.26 ginsbach #endif
381 1.26 ginsbach #ifdef TM_ZONE
382 1.26 ginsbach tm->TM_ZONE = tzname[i];
383 1.26 ginsbach #endif
384 1.26 ginsbach }
385 1.26 ginsbach bp = ep;
386 1.26 ginsbach }
387 1.26 ginsbach continue;
388 1.26 ginsbach
389 1.29 christos case 'z':
390 1.29 christos /*
391 1.29 christos * We recognize all ISO 8601 formats:
392 1.29 christos * Z = Zulu time/UTC
393 1.29 christos * [+-]hhmm
394 1.29 christos * [+-]hh:mm
395 1.29 christos * [+-]hh
396 1.32 ginsbach * We recognize all RFC-822/RFC-2822 formats:
397 1.32 ginsbach * UT|GMT
398 1.32 ginsbach * North American : UTC offsets
399 1.32 ginsbach * E[DS]T = Eastern : -4 | -5
400 1.32 ginsbach * C[DS]T = Central : -5 | -6
401 1.32 ginsbach * M[DS]T = Mountain: -6 | -7
402 1.32 ginsbach * P[DS]T = Pacific : -7 | -8
403 1.32 ginsbach * Military
404 1.32 ginsbach * [A-IL-M] = -1 ... -9 (J not used)
405 1.32 ginsbach * [N-Y] = +1 ... +12
406 1.29 christos */
407 1.29 christos while (isspace(*bp))
408 1.29 christos bp++;
409 1.29 christos
410 1.29 christos switch (*bp++) {
411 1.32 ginsbach case 'G':
412 1.32 ginsbach if (*bp++ != 'M')
413 1.32 ginsbach return NULL;
414 1.32 ginsbach /*FALLTHROUGH*/
415 1.32 ginsbach case 'U':
416 1.32 ginsbach if (*bp++ != 'T')
417 1.32 ginsbach return NULL;
418 1.32 ginsbach /*FALLTHROUGH*/
419 1.29 christos case 'Z':
420 1.29 christos tm->tm_isdst = 0;
421 1.29 christos #ifdef TM_GMTOFF
422 1.29 christos tm->TM_GMTOFF = 0;
423 1.29 christos #endif
424 1.29 christos #ifdef TM_ZONE
425 1.29 christos tm->TM_ZONE = utc;
426 1.29 christos #endif
427 1.29 christos continue;
428 1.29 christos case '+':
429 1.29 christos neg = 0;
430 1.29 christos break;
431 1.29 christos case '-':
432 1.29 christos neg = 1;
433 1.29 christos break;
434 1.29 christos default:
435 1.32 ginsbach --bp;
436 1.32 ginsbach ep = find_string(bp, &i, nast, NULL, 4);
437 1.32 ginsbach if (ep != NULL) {
438 1.32 ginsbach #ifdef TM_GMTOFF
439 1.32 ginsbach tm->TM_GMTOFF = -5 - i;
440 1.32 ginsbach #endif
441 1.32 ginsbach #ifdef TM_ZONE
442 1.32 ginsbach tm->TM_ZONE = __UNCONST(nast[i]);
443 1.32 ginsbach #endif
444 1.32 ginsbach bp = ep;
445 1.32 ginsbach continue;
446 1.32 ginsbach }
447 1.32 ginsbach ep = find_string(bp, &i, nadt, NULL, 4);
448 1.32 ginsbach if (ep != NULL) {
449 1.32 ginsbach tm->tm_isdst = 1;
450 1.32 ginsbach #ifdef TM_GMTOFF
451 1.32 ginsbach tm->TM_GMTOFF = -4 - i;
452 1.32 ginsbach #endif
453 1.32 ginsbach #ifdef TM_ZONE
454 1.32 ginsbach tm->TM_ZONE = __UNCONST(nadt[i]);
455 1.32 ginsbach #endif
456 1.32 ginsbach bp = ep;
457 1.32 ginsbach continue;
458 1.32 ginsbach }
459 1.32 ginsbach
460 1.32 ginsbach if ((*bp >= 'A' && *bp <= 'I') ||
461 1.32 ginsbach (*bp >= 'L' && *bp <= 'Y')) {
462 1.32 ginsbach #ifdef TM_GMTOFF
463 1.32 ginsbach /* Argh! No 'J'! */
464 1.32 ginsbach if (*bp >= 'A' && *bp <= 'I')
465 1.32 ginsbach tm->TM_GMTOFF =
466 1.32 ginsbach ('A' - 1) - (int)*bp;
467 1.32 ginsbach else if (*bp >= 'L' && *bp <= 'M')
468 1.32 ginsbach tm->TM_GMTOFF = 'A' - (int)*bp;
469 1.32 ginsbach else if (*bp >= 'N' && *bp <= 'Y')
470 1.32 ginsbach tm->TM_GMTOFF = (int)*bp - 'M';
471 1.32 ginsbach #endif
472 1.32 ginsbach #ifdef TM_ZONE
473 1.32 ginsbach tm->TM_ZONE = NULL; /* XXX */
474 1.32 ginsbach #endif
475 1.32 ginsbach bp++;
476 1.32 ginsbach continue;
477 1.32 ginsbach }
478 1.29 christos return NULL;
479 1.29 christos }
480 1.29 christos offs = 0;
481 1.29 christos for (i = 0; i < 4; ) {
482 1.29 christos if (isdigit(*bp)) {
483 1.29 christos offs = offs * 10 + (*bp++ - '0');
484 1.29 christos i++;
485 1.29 christos continue;
486 1.29 christos }
487 1.29 christos if (i == 2 && *bp == ':') {
488 1.29 christos bp++;
489 1.29 christos continue;
490 1.29 christos }
491 1.29 christos break;
492 1.29 christos }
493 1.29 christos switch (i) {
494 1.29 christos case 2:
495 1.29 christos offs *= 100;
496 1.29 christos break;
497 1.29 christos case 4:
498 1.29 christos i = offs % 100;
499 1.29 christos if (i >= 60)
500 1.29 christos return NULL;
501 1.29 christos /* Convert minutes into decimal */
502 1.29 christos offs = (offs / 100) * 100 + (i * 50) / 30;
503 1.29 christos break;
504 1.29 christos default:
505 1.29 christos return NULL;
506 1.29 christos }
507 1.31 christos if (neg)
508 1.31 christos offs = -offs;
509 1.29 christos tm->tm_isdst = 0; /* XXX */
510 1.29 christos #ifdef TM_GMTOFF
511 1.29 christos tm->TM_GMTOFF = offs;
512 1.29 christos #endif
513 1.29 christos #ifdef TM_ZONE
514 1.29 christos tm->TM_ZONE = NULL; /* XXX */
515 1.29 christos #endif
516 1.29 christos continue;
517 1.29 christos
518 1.3 kleink /*
519 1.3 kleink * Miscellaneous conversions.
520 1.3 kleink */
521 1.3 kleink case 'n': /* Any kind of white-space. */
522 1.3 kleink case 't':
523 1.3 kleink while (isspace(*bp))
524 1.3 kleink bp++;
525 1.23 dsl LEGAL_ALT(0);
526 1.23 dsl continue;
527 1.2 kleink
528 1.1 mrg
529 1.3 kleink default: /* Unknown/unsupported conversion. */
530 1.24 dsl return NULL;
531 1.3 kleink }
532 1.3 kleink }
533 1.2 kleink
534 1.25 christos return __UNCONST(bp);
535 1.3 kleink }
536 1.2 kleink
537 1.2 kleink
538 1.23 dsl static const u_char *
539 1.24 dsl conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
540 1.3 kleink {
541 1.24 dsl uint result = 0;
542 1.23 dsl unsigned char ch;
543 1.18 tv
544 1.18 tv /* The limit also determines the number of valid digits. */
545 1.24 dsl uint rulim = ulim;
546 1.2 kleink
547 1.23 dsl ch = *buf;
548 1.23 dsl if (ch < '0' || ch > '9')
549 1.24 dsl return NULL;
550 1.2 kleink
551 1.3 kleink do {
552 1.18 tv result *= 10;
553 1.23 dsl result += ch - '0';
554 1.18 tv rulim /= 10;
555 1.23 dsl ch = *++buf;
556 1.23 dsl } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
557 1.2 kleink
558 1.18 tv if (result < llim || result > ulim)
559 1.24 dsl return NULL;
560 1.1 mrg
561 1.18 tv *dest = result;
562 1.23 dsl return buf;
563 1.23 dsl }
564 1.23 dsl
565 1.23 dsl static const u_char *
566 1.23 dsl find_string(const u_char *bp, int *tgt, const char * const *n1,
567 1.23 dsl const char * const *n2, int c)
568 1.23 dsl {
569 1.23 dsl int i;
570 1.36 christos size_t len;
571 1.23 dsl
572 1.24 dsl /* check full name - then abbreviated ones */
573 1.24 dsl for (; n1 != NULL; n1 = n2, n2 = NULL) {
574 1.24 dsl for (i = 0; i < c; i++, n1++) {
575 1.24 dsl len = strlen(*n1);
576 1.24 dsl if (strncasecmp(*n1, (const char *)bp, len) == 0) {
577 1.24 dsl *tgt = i;
578 1.24 dsl return bp + len;
579 1.24 dsl }
580 1.24 dsl }
581 1.23 dsl }
582 1.24 dsl
583 1.24 dsl /* Nothing matched */
584 1.24 dsl return NULL;
585 1.1 mrg }
586