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