strptime.c revision 1.58 1 1.58 ginsbach /* $NetBSD: strptime.c,v 1.58 2015/10/31 03:42:00 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.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.58 ginsbach __RCSID("$NetBSD: strptime.c,v 1.58 2015/10/31 03:42:00 ginsbach 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.40 christos #include <sys/types.h>
40 1.3 kleink #include <ctype.h>
41 1.1 mrg #include <locale.h>
42 1.3 kleink #include <string.h>
43 1.1 mrg #include <time.h>
44 1.12 mycroft #include <tzfile.h>
45 1.29 christos #include "private.h"
46 1.37 joerg #include "setlocale_local.h"
47 1.7 jtc
48 1.7 jtc #ifdef __weak_alias
49 1.19 mycroft __weak_alias(strptime,_strptime)
50 1.37 joerg __weak_alias(strptime_l, _strptime_l)
51 1.7 jtc #endif
52 1.3 kleink
53 1.46 ginsbach static const u_char *conv_num(const unsigned char *, int *, uint, uint);
54 1.46 ginsbach static const u_char *find_string(const u_char *, int *, const char * const *,
55 1.46 ginsbach const char * const *, int);
56 1.46 ginsbach
57 1.37 joerg #define _TIME_LOCALE(loc) \
58 1.37 joerg ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
59 1.3 kleink
60 1.3 kleink /*
61 1.3 kleink * We do not implement alternate representations. However, we always
62 1.3 kleink * check whether a given modifier is allowed for a certain conversion.
63 1.3 kleink */
64 1.16 kleink #define ALT_E 0x01
65 1.16 kleink #define ALT_O 0x02
66 1.47 ginsbach #define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
67 1.3 kleink
68 1.47 ginsbach #define S_YEAR (1 << 0)
69 1.47 ginsbach #define S_MON (1 << 1)
70 1.47 ginsbach #define S_YDAY (1 << 2)
71 1.47 ginsbach #define S_MDAY (1 << 3)
72 1.47 ginsbach #define S_WDAY (1 << 4)
73 1.48 ginsbach #define S_HOUR (1 << 5)
74 1.47 ginsbach
75 1.47 ginsbach #define HAVE_MDAY(s) (s & S_MDAY)
76 1.47 ginsbach #define HAVE_MON(s) (s & S_MON)
77 1.47 ginsbach #define HAVE_WDAY(s) (s & S_WDAY)
78 1.47 ginsbach #define HAVE_YDAY(s) (s & S_YDAY)
79 1.47 ginsbach #define HAVE_YEAR(s) (s & S_YEAR)
80 1.48 ginsbach #define HAVE_HOUR(s) (s & S_HOUR)
81 1.42 ginsbach
82 1.29 christos static char utc[] = { "UTC" };
83 1.32 ginsbach /* RFC-822/RFC-2822 */
84 1.32 ginsbach static const char * const nast[5] = {
85 1.32 ginsbach "EST", "CST", "MST", "PST", "\0\0\0"
86 1.32 ginsbach };
87 1.32 ginsbach static const char * const nadt[5] = {
88 1.32 ginsbach "EDT", "CDT", "MDT", "PDT", "\0\0\0"
89 1.32 ginsbach };
90 1.3 kleink
91 1.46 ginsbach /*
92 1.46 ginsbach * Table to determine the ordinal date for the start of a month.
93 1.46 ginsbach * Ref: http://en.wikipedia.org/wiki/ISO_week_date
94 1.46 ginsbach */
95 1.40 christos static const int start_of_month[2][13] = {
96 1.46 ginsbach /* non-leap year */
97 1.40 christos { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
98 1.46 ginsbach /* leap year */
99 1.40 christos { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
100 1.40 christos };
101 1.40 christos
102 1.40 christos /*
103 1.40 christos * Calculate the week day of the first day of a year. Valid for
104 1.40 christos * the Gregorian calendar, which began Sept 14, 1752 in the UK
105 1.40 christos * and its colonies. Ref:
106 1.40 christos * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
107 1.40 christos */
108 1.40 christos
109 1.40 christos static int
110 1.40 christos first_wday_of(int yr)
111 1.40 christos {
112 1.40 christos return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) / 4) +
113 1.43 ginsbach (isleap(yr) ? 6 : 0) + 1) % 7;
114 1.40 christos }
115 1.40 christos
116 1.50 christos #define delim(p) ((p) == '\0' || isspace((unsigned char)(p)))
117 1.50 christos
118 1.50 christos static int
119 1.57 christos fromzone(const unsigned char **bp, struct tm *tm, int mandatory)
120 1.50 christos {
121 1.50 christos timezone_t tz;
122 1.50 christos char buf[512], *p;
123 1.57 christos const unsigned char *rp;
124 1.50 christos
125 1.57 christos for (p = buf, rp = *bp; !delim(*rp) && p < &buf[sizeof(buf) - 1]; rp++)
126 1.57 christos *p++ = *rp;
127 1.50 christos *p = '\0';
128 1.50 christos
129 1.57 christos if (mandatory)
130 1.57 christos *bp = rp;
131 1.50 christos tz = tzalloc(buf);
132 1.50 christos if (tz == NULL)
133 1.50 christos return 0;
134 1.50 christos
135 1.57 christos *bp = rp;
136 1.50 christos tm->tm_isdst = 0; /* XXX */
137 1.50 christos #ifdef TM_GMTOFF
138 1.50 christos tm->TM_GMTOFF = tzgetgmtoff(tz, tm->tm_isdst);
139 1.50 christos #endif
140 1.50 christos #ifdef TM_ZONE
141 1.50 christos // Can't use tzgetname() here because we are going to free()
142 1.51 christos tm->TM_ZONE = NULL; /* XXX */
143 1.50 christos #endif
144 1.50 christos tzfree(tz);
145 1.50 christos return 1;
146 1.50 christos }
147 1.50 christos
148 1.37 joerg char *
149 1.37 joerg strptime(const char *buf, const char *fmt, struct tm *tm)
150 1.37 joerg {
151 1.38 joerg return strptime_l(buf, fmt, tm, _current_locale());
152 1.37 joerg }
153 1.1 mrg
154 1.2 kleink char *
155 1.37 joerg strptime_l(const char *buf, const char *fmt, struct tm *tm, locale_t loc)
156 1.1 mrg {
157 1.20 itohy unsigned char c;
158 1.50 christos const unsigned char *bp, *ep, *zname;
159 1.41 ginsbach int alt_format, i, split_year = 0, neg = 0, state = 0,
160 1.57 christos day_offset = -1, week_offset = 0, offs, mandatory;
161 1.23 dsl const char *new_fmt;
162 1.3 kleink
163 1.22 christos bp = (const u_char *)buf;
164 1.3 kleink
165 1.24 dsl while (bp != NULL && (c = *fmt++) != '\0') {
166 1.3 kleink /* Clear `alternate' modifier prior to new conversion. */
167 1.3 kleink alt_format = 0;
168 1.23 dsl i = 0;
169 1.3 kleink
170 1.3 kleink /* Eat up white-space. */
171 1.3 kleink if (isspace(c)) {
172 1.3 kleink while (isspace(*bp))
173 1.3 kleink bp++;
174 1.2 kleink continue;
175 1.2 kleink }
176 1.23 dsl
177 1.24 dsl if (c != '%')
178 1.3 kleink goto literal;
179 1.3 kleink
180 1.3 kleink
181 1.3 kleink again: switch (c = *fmt++) {
182 1.3 kleink case '%': /* "%%" is converted to "%". */
183 1.3 kleink literal:
184 1.14 tv if (c != *bp++)
185 1.24 dsl return NULL;
186 1.23 dsl LEGAL_ALT(0);
187 1.23 dsl continue;
188 1.3 kleink
189 1.3 kleink /*
190 1.3 kleink * "Alternative" modifiers. Just set the appropriate flag
191 1.3 kleink * and start over again.
192 1.3 kleink */
193 1.3 kleink case 'E': /* "%E?" alternative conversion modifier. */
194 1.16 kleink LEGAL_ALT(0);
195 1.16 kleink alt_format |= ALT_E;
196 1.3 kleink goto again;
197 1.3 kleink
198 1.3 kleink case 'O': /* "%O?" alternative conversion modifier. */
199 1.16 kleink LEGAL_ALT(0);
200 1.16 kleink alt_format |= ALT_O;
201 1.3 kleink goto again;
202 1.23 dsl
203 1.3 kleink /*
204 1.3 kleink * "Complex" conversion rules, implemented through recursion.
205 1.3 kleink */
206 1.3 kleink case 'c': /* Date and time, using the locale's format. */
207 1.37 joerg new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
208 1.42 ginsbach state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
209 1.23 dsl goto recurse;
210 1.3 kleink
211 1.3 kleink case 'D': /* The date as "%m/%d/%y". */
212 1.23 dsl new_fmt = "%m/%d/%y";
213 1.16 kleink LEGAL_ALT(0);
214 1.41 ginsbach state |= S_MON | S_MDAY | S_YEAR;
215 1.23 dsl goto recurse;
216 1.18 tv
217 1.27 ginsbach case 'F': /* The date as "%Y-%m-%d". */
218 1.27 ginsbach new_fmt = "%Y-%m-%d";
219 1.27 ginsbach LEGAL_ALT(0);
220 1.41 ginsbach state |= S_MON | S_MDAY | S_YEAR;
221 1.27 ginsbach goto recurse;
222 1.27 ginsbach
223 1.3 kleink case 'R': /* The time as "%H:%M". */
224 1.23 dsl new_fmt = "%H:%M";
225 1.16 kleink LEGAL_ALT(0);
226 1.23 dsl goto recurse;
227 1.3 kleink
228 1.3 kleink case 'r': /* The time in 12-hour clock representation. */
229 1.37 joerg new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
230 1.16 kleink LEGAL_ALT(0);
231 1.23 dsl goto recurse;
232 1.3 kleink
233 1.3 kleink case 'T': /* The time as "%H:%M:%S". */
234 1.23 dsl new_fmt = "%H:%M:%S";
235 1.16 kleink LEGAL_ALT(0);
236 1.23 dsl goto recurse;
237 1.3 kleink
238 1.3 kleink case 'X': /* The time, using the locale's format. */
239 1.37 joerg new_fmt = _TIME_LOCALE(loc)->t_fmt;
240 1.23 dsl goto recurse;
241 1.3 kleink
242 1.3 kleink case 'x': /* The date, using the locale's format. */
243 1.37 joerg new_fmt = _TIME_LOCALE(loc)->d_fmt;
244 1.41 ginsbach state |= S_MON | S_MDAY | S_YEAR;
245 1.23 dsl recurse:
246 1.23 dsl bp = (const u_char *)strptime((const char *)bp,
247 1.23 dsl new_fmt, tm);
248 1.16 kleink LEGAL_ALT(ALT_E);
249 1.23 dsl continue;
250 1.3 kleink
251 1.3 kleink /*
252 1.3 kleink * "Elementary" conversion rules.
253 1.3 kleink */
254 1.3 kleink case 'A': /* The day of week, using the locale's form. */
255 1.3 kleink case 'a':
256 1.37 joerg bp = find_string(bp, &tm->tm_wday,
257 1.37 joerg _TIME_LOCALE(loc)->day, _TIME_LOCALE(loc)->abday, 7);
258 1.16 kleink LEGAL_ALT(0);
259 1.41 ginsbach state |= S_WDAY;
260 1.23 dsl continue;
261 1.2 kleink
262 1.3 kleink case 'B': /* The month, using the locale's form. */
263 1.3 kleink case 'b':
264 1.3 kleink case 'h':
265 1.37 joerg bp = find_string(bp, &tm->tm_mon,
266 1.37 joerg _TIME_LOCALE(loc)->mon, _TIME_LOCALE(loc)->abmon,
267 1.37 joerg 12);
268 1.16 kleink LEGAL_ALT(0);
269 1.41 ginsbach state |= S_MON;
270 1.23 dsl continue;
271 1.2 kleink
272 1.3 kleink case 'C': /* The century number. */
273 1.24 dsl i = 20;
274 1.23 dsl bp = conv_num(bp, &i, 0, 99);
275 1.2 kleink
276 1.24 dsl i = i * 100 - TM_YEAR_BASE;
277 1.24 dsl if (split_year)
278 1.24 dsl i += tm->tm_year % 100;
279 1.24 dsl split_year = 1;
280 1.24 dsl tm->tm_year = i;
281 1.23 dsl LEGAL_ALT(ALT_E);
282 1.41 ginsbach state |= S_YEAR;
283 1.23 dsl continue;
284 1.2 kleink
285 1.3 kleink case 'd': /* The day of month. */
286 1.3 kleink case 'e':
287 1.23 dsl bp = conv_num(bp, &tm->tm_mday, 1, 31);
288 1.16 kleink LEGAL_ALT(ALT_O);
289 1.41 ginsbach state |= S_MDAY;
290 1.23 dsl continue;
291 1.2 kleink
292 1.3 kleink case 'k': /* The hour (24-hour clock representation). */
293 1.16 kleink LEGAL_ALT(0);
294 1.3 kleink /* FALLTHROUGH */
295 1.3 kleink case 'H':
296 1.23 dsl bp = conv_num(bp, &tm->tm_hour, 0, 23);
297 1.16 kleink LEGAL_ALT(ALT_O);
298 1.48 ginsbach state |= S_HOUR;
299 1.23 dsl continue;
300 1.2 kleink
301 1.3 kleink case 'l': /* The hour (12-hour clock representation). */
302 1.16 kleink LEGAL_ALT(0);
303 1.3 kleink /* FALLTHROUGH */
304 1.3 kleink case 'I':
305 1.23 dsl bp = conv_num(bp, &tm->tm_hour, 1, 12);
306 1.13 tv if (tm->tm_hour == 12)
307 1.13 tv tm->tm_hour = 0;
308 1.23 dsl LEGAL_ALT(ALT_O);
309 1.48 ginsbach state |= S_HOUR;
310 1.23 dsl continue;
311 1.2 kleink
312 1.3 kleink case 'j': /* The day of year. */
313 1.23 dsl i = 1;
314 1.23 dsl bp = conv_num(bp, &i, 1, 366);
315 1.23 dsl tm->tm_yday = i - 1;
316 1.16 kleink LEGAL_ALT(0);
317 1.41 ginsbach state |= S_YDAY;
318 1.23 dsl continue;
319 1.2 kleink
320 1.3 kleink case 'M': /* The minute. */
321 1.23 dsl bp = conv_num(bp, &tm->tm_min, 0, 59);
322 1.16 kleink LEGAL_ALT(ALT_O);
323 1.23 dsl continue;
324 1.2 kleink
325 1.3 kleink case 'm': /* The month. */
326 1.23 dsl i = 1;
327 1.23 dsl bp = conv_num(bp, &i, 1, 12);
328 1.23 dsl tm->tm_mon = i - 1;
329 1.16 kleink LEGAL_ALT(ALT_O);
330 1.41 ginsbach state |= S_MON;
331 1.23 dsl continue;
332 1.2 kleink
333 1.3 kleink case 'p': /* The locale's equivalent of AM/PM. */
334 1.37 joerg bp = find_string(bp, &i, _TIME_LOCALE(loc)->am_pm,
335 1.37 joerg NULL, 2);
336 1.48 ginsbach if (HAVE_HOUR(state) && tm->tm_hour > 11)
337 1.24 dsl return NULL;
338 1.23 dsl tm->tm_hour += i * 12;
339 1.16 kleink LEGAL_ALT(0);
340 1.23 dsl continue;
341 1.2 kleink
342 1.3 kleink case 'S': /* The seconds. */
343 1.23 dsl bp = conv_num(bp, &tm->tm_sec, 0, 61);
344 1.16 kleink LEGAL_ALT(ALT_O);
345 1.23 dsl continue;
346 1.1 mrg
347 1.33 ginsbach #ifndef TIME_MAX
348 1.33 ginsbach #define TIME_MAX INT64_MAX
349 1.33 ginsbach #endif
350 1.33 ginsbach case 's': /* seconds since the epoch */
351 1.33 ginsbach {
352 1.33 ginsbach time_t sse = 0;
353 1.33 ginsbach uint64_t rulim = TIME_MAX;
354 1.33 ginsbach
355 1.33 ginsbach if (*bp < '0' || *bp > '9') {
356 1.33 ginsbach bp = NULL;
357 1.33 ginsbach continue;
358 1.33 ginsbach }
359 1.33 ginsbach
360 1.33 ginsbach do {
361 1.33 ginsbach sse *= 10;
362 1.33 ginsbach sse += *bp++ - '0';
363 1.33 ginsbach rulim /= 10;
364 1.35 matt } while ((sse * 10 <= TIME_MAX) &&
365 1.33 ginsbach rulim && *bp >= '0' && *bp <= '9');
366 1.33 ginsbach
367 1.33 ginsbach if (sse < 0 || (uint64_t)sse > TIME_MAX) {
368 1.33 ginsbach bp = NULL;
369 1.33 ginsbach continue;
370 1.33 ginsbach }
371 1.33 ginsbach
372 1.33 ginsbach if (localtime_r(&sse, tm) == NULL)
373 1.33 ginsbach bp = NULL;
374 1.40 christos else
375 1.41 ginsbach state |= S_YDAY | S_WDAY |
376 1.41 ginsbach S_MON | S_MDAY | S_YEAR;
377 1.33 ginsbach }
378 1.33 ginsbach continue;
379 1.33 ginsbach
380 1.3 kleink case 'U': /* The week of year, beginning on sunday. */
381 1.3 kleink case 'W': /* The week of year, beginning on monday. */
382 1.3 kleink /*
383 1.3 kleink * XXX This is bogus, as we can not assume any valid
384 1.3 kleink * information present in the tm structure at this
385 1.3 kleink * point to calculate a real value, so just check the
386 1.3 kleink * range for now.
387 1.3 kleink */
388 1.40 christos bp = conv_num(bp, &i, 0, 53);
389 1.40 christos LEGAL_ALT(ALT_O);
390 1.40 christos if (c == 'U')
391 1.40 christos day_offset = TM_SUNDAY;
392 1.40 christos else
393 1.40 christos day_offset = TM_MONDAY;
394 1.40 christos week_offset = i;
395 1.40 christos continue;
396 1.2 kleink
397 1.3 kleink case 'w': /* The day of week, beginning on sunday. */
398 1.23 dsl bp = conv_num(bp, &tm->tm_wday, 0, 6);
399 1.16 kleink LEGAL_ALT(ALT_O);
400 1.41 ginsbach state |= S_WDAY;
401 1.23 dsl continue;
402 1.2 kleink
403 1.29 christos case 'u': /* The day of week, monday = 1. */
404 1.29 christos bp = conv_num(bp, &i, 1, 7);
405 1.29 christos tm->tm_wday = i % 7;
406 1.29 christos LEGAL_ALT(ALT_O);
407 1.44 ginsbach state |= S_WDAY;
408 1.29 christos continue;
409 1.29 christos
410 1.29 christos case 'g': /* The year corresponding to the ISO week
411 1.29 christos * number but without the century.
412 1.29 christos */
413 1.29 christos bp = conv_num(bp, &i, 0, 99);
414 1.29 christos continue;
415 1.29 christos
416 1.29 christos case 'G': /* The year corresponding to the ISO week
417 1.29 christos * number with century.
418 1.29 christos */
419 1.29 christos do
420 1.30 christos bp++;
421 1.29 christos while (isdigit(*bp));
422 1.29 christos continue;
423 1.29 christos
424 1.29 christos case 'V': /* The ISO 8601:1988 week number as decimal */
425 1.29 christos bp = conv_num(bp, &i, 0, 53);
426 1.29 christos continue;
427 1.29 christos
428 1.3 kleink case 'Y': /* The year. */
429 1.24 dsl i = TM_YEAR_BASE; /* just for data sanity... */
430 1.23 dsl bp = conv_num(bp, &i, 0, 9999);
431 1.8 mycroft tm->tm_year = i - TM_YEAR_BASE;
432 1.23 dsl LEGAL_ALT(ALT_E);
433 1.41 ginsbach state |= S_YEAR;
434 1.23 dsl continue;
435 1.2 kleink
436 1.9 mycroft case 'y': /* The year within 100 years of the epoch. */
437 1.24 dsl /* LEGAL_ALT(ALT_E | ALT_O); */
438 1.23 dsl bp = conv_num(bp, &i, 0, 99);
439 1.8 mycroft
440 1.24 dsl if (split_year)
441 1.24 dsl /* preserve century */
442 1.24 dsl i += (tm->tm_year / 100) * 100;
443 1.24 dsl else {
444 1.24 dsl split_year = 1;
445 1.24 dsl if (i <= 68)
446 1.24 dsl i = i + 2000 - TM_YEAR_BASE;
447 1.24 dsl else
448 1.24 dsl i = i + 1900 - TM_YEAR_BASE;
449 1.13 tv }
450 1.24 dsl tm->tm_year = i;
451 1.41 ginsbach state |= S_YEAR;
452 1.23 dsl continue;
453 1.2 kleink
454 1.26 ginsbach case 'Z':
455 1.57 christos case 'z':
456 1.26 ginsbach tzset();
457 1.57 christos mandatory = c == 'z';
458 1.29 christos /*
459 1.29 christos * We recognize all ISO 8601 formats:
460 1.29 christos * Z = Zulu time/UTC
461 1.29 christos * [+-]hhmm
462 1.29 christos * [+-]hh:mm
463 1.29 christos * [+-]hh
464 1.32 ginsbach * We recognize all RFC-822/RFC-2822 formats:
465 1.32 ginsbach * UT|GMT
466 1.32 ginsbach * North American : UTC offsets
467 1.32 ginsbach * E[DS]T = Eastern : -4 | -5
468 1.32 ginsbach * C[DS]T = Central : -5 | -6
469 1.32 ginsbach * M[DS]T = Mountain: -6 | -7
470 1.32 ginsbach * P[DS]T = Pacific : -7 | -8
471 1.56 ginsbach * Nautical/Military
472 1.32 ginsbach * [A-IL-M] = -1 ... -9 (J not used)
473 1.32 ginsbach * [N-Y] = +1 ... +12
474 1.56 ginsbach * Note: J maybe used to denote non-nautical
475 1.56 ginsbach * local time
476 1.29 christos */
477 1.57 christos if (mandatory)
478 1.57 christos while (isspace(*bp))
479 1.57 christos bp++;
480 1.29 christos
481 1.50 christos zname = bp;
482 1.29 christos switch (*bp++) {
483 1.32 ginsbach case 'G':
484 1.32 ginsbach if (*bp++ != 'M')
485 1.50 christos goto namedzone;
486 1.32 ginsbach /*FALLTHROUGH*/
487 1.32 ginsbach case 'U':
488 1.32 ginsbach if (*bp++ != 'T')
489 1.50 christos goto namedzone;
490 1.50 christos else if (!delim(*bp) && *bp++ != 'C')
491 1.50 christos goto namedzone;
492 1.32 ginsbach /*FALLTHROUGH*/
493 1.29 christos case 'Z':
494 1.50 christos if (!delim(*bp))
495 1.50 christos goto namedzone;
496 1.29 christos tm->tm_isdst = 0;
497 1.29 christos #ifdef TM_GMTOFF
498 1.29 christos tm->TM_GMTOFF = 0;
499 1.29 christos #endif
500 1.29 christos #ifdef TM_ZONE
501 1.29 christos tm->TM_ZONE = utc;
502 1.29 christos #endif
503 1.29 christos continue;
504 1.29 christos case '+':
505 1.29 christos neg = 0;
506 1.29 christos break;
507 1.29 christos case '-':
508 1.29 christos neg = 1;
509 1.29 christos break;
510 1.29 christos default:
511 1.50 christos namedzone:
512 1.50 christos bp = zname;
513 1.50 christos
514 1.56 ginsbach /* Nautical / Military style */
515 1.50 christos if (delim(bp[1]) &&
516 1.50 christos ((*bp >= 'A' && *bp <= 'I') ||
517 1.50 christos (*bp >= 'L' && *bp <= 'Y'))) {
518 1.50 christos #ifdef TM_GMTOFF
519 1.50 christos /* Argh! No 'J'! */
520 1.50 christos if (*bp >= 'A' && *bp <= 'I')
521 1.50 christos tm->TM_GMTOFF =
522 1.50 christos ('A' - 1) - (int)*bp;
523 1.50 christos else if (*bp >= 'L' && *bp <= 'M')
524 1.50 christos tm->TM_GMTOFF = 'A' - (int)*bp;
525 1.50 christos else if (*bp >= 'N' && *bp <= 'Y')
526 1.50 christos tm->TM_GMTOFF = (int)*bp - 'M';
527 1.51 christos tm->TM_GMTOFF *= SECSPERHOUR;
528 1.50 christos #endif
529 1.50 christos #ifdef TM_ZONE
530 1.51 christos tm->TM_ZONE = NULL; /* XXX */
531 1.50 christos #endif
532 1.50 christos bp++;
533 1.50 christos continue;
534 1.50 christos }
535 1.56 ginsbach /* 'J' is local time */
536 1.56 ginsbach if (delim(bp[1]) && *bp == 'J') {
537 1.56 ginsbach #ifdef TM_GMTOFF
538 1.56 ginsbach tm->TM_GMTOFF = -timezone;
539 1.56 ginsbach #endif
540 1.56 ginsbach #ifdef TM_ZONE
541 1.58 ginsbach tm->TM_ZONE = NULL; /* XXX */
542 1.56 ginsbach #endif
543 1.56 ginsbach bp++;
544 1.56 ginsbach continue;
545 1.56 ginsbach }
546 1.50 christos
547 1.50 christos /*
548 1.50 christos * From our 3 letter hard-coded table
549 1.50 christos * XXX: Can be removed, handled by tzload()
550 1.50 christos */
551 1.50 christos if (delim(bp[0]) || delim(bp[1]) ||
552 1.50 christos delim(bp[2]) || !delim(bp[3]))
553 1.50 christos goto loadzone;
554 1.32 ginsbach ep = find_string(bp, &i, nast, NULL, 4);
555 1.32 ginsbach if (ep != NULL) {
556 1.32 ginsbach #ifdef TM_GMTOFF
557 1.51 christos tm->TM_GMTOFF = (-5 - i) * SECSPERHOUR;
558 1.32 ginsbach #endif
559 1.32 ginsbach #ifdef TM_ZONE
560 1.32 ginsbach tm->TM_ZONE = __UNCONST(nast[i]);
561 1.32 ginsbach #endif
562 1.32 ginsbach bp = ep;
563 1.32 ginsbach continue;
564 1.32 ginsbach }
565 1.32 ginsbach ep = find_string(bp, &i, nadt, NULL, 4);
566 1.32 ginsbach if (ep != NULL) {
567 1.32 ginsbach tm->tm_isdst = 1;
568 1.32 ginsbach #ifdef TM_GMTOFF
569 1.51 christos tm->TM_GMTOFF = (-4 - i) * SECSPERHOUR;
570 1.32 ginsbach #endif
571 1.32 ginsbach #ifdef TM_ZONE
572 1.32 ginsbach tm->TM_ZONE = __UNCONST(nadt[i]);
573 1.32 ginsbach #endif
574 1.32 ginsbach bp = ep;
575 1.32 ginsbach continue;
576 1.32 ginsbach }
577 1.57 christos /*
578 1.57 christos * Our current timezone
579 1.57 christos */
580 1.57 christos ep = find_string(bp, &i,
581 1.57 christos (const char * const *)tzname,
582 1.57 christos NULL, 2);
583 1.57 christos if (ep != NULL) {
584 1.57 christos tm->tm_isdst = i;
585 1.57 christos #ifdef TM_GMTOFF
586 1.57 christos tm->TM_GMTOFF = -timezone;
587 1.57 christos #endif
588 1.57 christos #ifdef TM_ZONE
589 1.57 christos tm->TM_ZONE = tzname[i];
590 1.57 christos #endif
591 1.57 christos bp = ep;
592 1.57 christos continue;
593 1.57 christos }
594 1.50 christos loadzone:
595 1.50 christos /*
596 1.50 christos * The hard way, load the zone!
597 1.50 christos */
598 1.57 christos if (fromzone(&bp, tm, mandatory))
599 1.32 ginsbach continue;
600 1.57 christos goto out;
601 1.29 christos }
602 1.29 christos offs = 0;
603 1.29 christos for (i = 0; i < 4; ) {
604 1.29 christos if (isdigit(*bp)) {
605 1.29 christos offs = offs * 10 + (*bp++ - '0');
606 1.29 christos i++;
607 1.29 christos continue;
608 1.29 christos }
609 1.29 christos if (i == 2 && *bp == ':') {
610 1.29 christos bp++;
611 1.29 christos continue;
612 1.29 christos }
613 1.29 christos break;
614 1.29 christos }
615 1.50 christos if (isdigit(*bp))
616 1.57 christos goto out;
617 1.29 christos switch (i) {
618 1.29 christos case 2:
619 1.51 christos offs *= SECSPERHOUR;
620 1.29 christos break;
621 1.29 christos case 4:
622 1.29 christos i = offs % 100;
623 1.57 christos offs /= 100;
624 1.51 christos if (i >= SECSPERMIN)
625 1.57 christos goto out;
626 1.29 christos /* Convert minutes into decimal */
627 1.57 christos offs = offs * SECSPERHOUR + i * SECSPERMIN;
628 1.29 christos break;
629 1.29 christos default:
630 1.57 christos out:
631 1.57 christos if (mandatory)
632 1.57 christos return NULL;
633 1.57 christos bp = zname;
634 1.57 christos continue;
635 1.29 christos }
636 1.53 ginsbach if (offs >= (HOURSPERDAY * SECSPERHOUR))
637 1.57 christos goto out;
638 1.31 christos if (neg)
639 1.31 christos offs = -offs;
640 1.29 christos tm->tm_isdst = 0; /* XXX */
641 1.29 christos #ifdef TM_GMTOFF
642 1.29 christos tm->TM_GMTOFF = offs;
643 1.29 christos #endif
644 1.29 christos #ifdef TM_ZONE
645 1.51 christos tm->TM_ZONE = NULL; /* XXX */
646 1.29 christos #endif
647 1.29 christos continue;
648 1.29 christos
649 1.3 kleink /*
650 1.3 kleink * Miscellaneous conversions.
651 1.3 kleink */
652 1.3 kleink case 'n': /* Any kind of white-space. */
653 1.3 kleink case 't':
654 1.3 kleink while (isspace(*bp))
655 1.3 kleink bp++;
656 1.23 dsl LEGAL_ALT(0);
657 1.23 dsl continue;
658 1.2 kleink
659 1.1 mrg
660 1.3 kleink default: /* Unknown/unsupported conversion. */
661 1.24 dsl return NULL;
662 1.3 kleink }
663 1.3 kleink }
664 1.2 kleink
665 1.42 ginsbach if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
666 1.42 ginsbach if (HAVE_MON(state) && HAVE_MDAY(state)) {
667 1.46 ginsbach /* calculate day of year (ordinal date) */
668 1.43 ginsbach tm->tm_yday = start_of_month[isleap_sum(tm->tm_year,
669 1.40 christos TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
670 1.41 ginsbach state |= S_YDAY;
671 1.40 christos } else if (day_offset != -1) {
672 1.46 ginsbach /*
673 1.46 ginsbach * Set the date to the first Sunday (or Monday)
674 1.40 christos * of the specified week of the year.
675 1.40 christos */
676 1.42 ginsbach if (!HAVE_WDAY(state)) {
677 1.40 christos tm->tm_wday = day_offset;
678 1.41 ginsbach state |= S_WDAY;
679 1.40 christos }
680 1.40 christos tm->tm_yday = (7 -
681 1.40 christos first_wday_of(tm->tm_year + TM_YEAR_BASE) +
682 1.40 christos day_offset) % 7 + (week_offset - 1) * 7 +
683 1.40 christos tm->tm_wday - day_offset;
684 1.41 ginsbach state |= S_YDAY;
685 1.40 christos }
686 1.40 christos }
687 1.40 christos
688 1.42 ginsbach if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
689 1.40 christos int isleap;
690 1.46 ginsbach
691 1.42 ginsbach if (!HAVE_MON(state)) {
692 1.46 ginsbach /* calculate month of day of year */
693 1.40 christos i = 0;
694 1.43 ginsbach isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
695 1.40 christos while (tm->tm_yday >= start_of_month[isleap][i])
696 1.40 christos i++;
697 1.40 christos if (i > 12) {
698 1.40 christos i = 1;
699 1.40 christos tm->tm_yday -= start_of_month[isleap][12];
700 1.40 christos tm->tm_year++;
701 1.40 christos }
702 1.40 christos tm->tm_mon = i - 1;
703 1.41 ginsbach state |= S_MON;
704 1.40 christos }
705 1.46 ginsbach
706 1.42 ginsbach if (!HAVE_MDAY(state)) {
707 1.46 ginsbach /* calculate day of month */
708 1.43 ginsbach isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
709 1.40 christos tm->tm_mday = tm->tm_yday -
710 1.40 christos start_of_month[isleap][tm->tm_mon] + 1;
711 1.41 ginsbach state |= S_MDAY;
712 1.40 christos }
713 1.46 ginsbach
714 1.42 ginsbach if (!HAVE_WDAY(state)) {
715 1.46 ginsbach /* calculate day of week */
716 1.40 christos i = 0;
717 1.40 christos week_offset = first_wday_of(tm->tm_year);
718 1.40 christos while (i++ <= tm->tm_yday) {
719 1.40 christos if (week_offset++ >= 6)
720 1.40 christos week_offset = 0;
721 1.40 christos }
722 1.40 christos tm->tm_wday = week_offset;
723 1.41 ginsbach state |= S_WDAY;
724 1.40 christos }
725 1.40 christos }
726 1.40 christos
727 1.25 christos return __UNCONST(bp);
728 1.3 kleink }
729 1.2 kleink
730 1.2 kleink
731 1.23 dsl static const u_char *
732 1.24 dsl conv_num(const unsigned char *buf, int *dest, uint llim, uint ulim)
733 1.3 kleink {
734 1.24 dsl uint result = 0;
735 1.23 dsl unsigned char ch;
736 1.18 tv
737 1.18 tv /* The limit also determines the number of valid digits. */
738 1.24 dsl uint rulim = ulim;
739 1.2 kleink
740 1.23 dsl ch = *buf;
741 1.23 dsl if (ch < '0' || ch > '9')
742 1.24 dsl return NULL;
743 1.2 kleink
744 1.3 kleink do {
745 1.18 tv result *= 10;
746 1.23 dsl result += ch - '0';
747 1.18 tv rulim /= 10;
748 1.23 dsl ch = *++buf;
749 1.23 dsl } while ((result * 10 <= ulim) && rulim && ch >= '0' && ch <= '9');
750 1.2 kleink
751 1.18 tv if (result < llim || result > ulim)
752 1.24 dsl return NULL;
753 1.1 mrg
754 1.18 tv *dest = result;
755 1.23 dsl return buf;
756 1.23 dsl }
757 1.23 dsl
758 1.23 dsl static const u_char *
759 1.23 dsl find_string(const u_char *bp, int *tgt, const char * const *n1,
760 1.23 dsl const char * const *n2, int c)
761 1.23 dsl {
762 1.23 dsl int i;
763 1.36 christos size_t len;
764 1.23 dsl
765 1.24 dsl /* check full name - then abbreviated ones */
766 1.24 dsl for (; n1 != NULL; n1 = n2, n2 = NULL) {
767 1.24 dsl for (i = 0; i < c; i++, n1++) {
768 1.24 dsl len = strlen(*n1);
769 1.24 dsl if (strncasecmp(*n1, (const char *)bp, len) == 0) {
770 1.24 dsl *tgt = i;
771 1.24 dsl return bp + len;
772 1.24 dsl }
773 1.24 dsl }
774 1.23 dsl }
775 1.24 dsl
776 1.24 dsl /* Nothing matched */
777 1.24 dsl return NULL;
778 1.1 mrg }
779