source: Main/trunk/Server/strptime.c@ 309

Last change on this file since 309 was 243, checked in by Nishi, on Oct 3, 2024 at 3:33:30 PM

fix include

  • Property svn:keywords set to Id
File size: 25.4 KB
Line 
1/* $NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $ */
2/* http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/lib/libc/time/strptime.c?only_with_tag=HEAD
3 * NetBSD implementation strptime().
4 * Format description: https://netbsd.gw.com/cgi-bin/man-cgi?strptime+3+NetBSD-current
5*/
6/*-
7 * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code was contributed to The NetBSD Foundation by Klaus Klein.
11 * Heavily optimised by David Laight
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35//#include <sys/cdefs.h>
36//__RCSID("$NetBSD: strptime.c,v 1.62 2017/08/24 01:01:09 ginsbach Exp $");
37
38#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
39
40#include <ctype.h>
41#include <string.h>
42#include <time.h>
43#include <stdint.h>
44
45#ifdef __WATCOMC__
46#include <strings.h>
47#endif
48
49static const unsigned char *conv_num(const unsigned char *, int *, unsigned int, unsigned int);
50static const unsigned char *find_string(const unsigned char *, int *, const char * const *, const char * const *, int);
51
52/*
53 * We do not implement alternate representations. However, we always
54 * check whether a given modifier is allowed for a certain conversion.
55 */
56#define ALT_E 0x01
57#define ALT_O 0x02
58#define LEGAL_ALT(x) { if (alt_format & ~(x)) return NULL; }
59
60#define TM_YEAR_BASE 1900
61
62#define TM_SUNDAY 0
63#define TM_MONDAY 1
64#define TM_TUESDAY 2
65#define TM_WEDNESDAY 3
66#define TM_THURSDAY 4
67#define TM_FRIDAY 5
68#define TM_SATURDAY 6
69
70#define S_YEAR (1 << 0)
71#define S_MON (1 << 1)
72#define S_YDAY (1 << 2)
73#define S_MDAY (1 << 3)
74#define S_WDAY (1 << 4)
75#define S_HOUR (1 << 5)
76
77#define HAVE_MDAY(s) (s & S_MDAY)
78#define HAVE_MON(s) (s & S_MON)
79#define HAVE_WDAY(s) (s & S_WDAY)
80#define HAVE_YDAY(s) (s & S_YDAY)
81#define HAVE_YEAR(s) (s & S_YEAR)
82#define HAVE_HOUR(s) (s & S_HOUR)
83
84#define SECSPERMIN 60
85#define MINSPERHOUR 60
86#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
87#define HOURSPERDAY 24
88
89#define HERE_D_T_FMT "%a %b %e %H:%M:%S %Y"
90#define HERE_D_FMT "%y/%m/%d"
91#define HERE_T_FMT_AMPM "%I:%M:%S %p"
92#define HERE_T_FMT "%H:%M:%S"
93
94#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
95
96/*
97** Since everything in isleap is modulo 400 (or a factor of 400), we know that
98** isleap(y) == isleap(y % 400)
99** and so
100** isleap(a + b) == isleap((a + b) % 400)
101** or
102** isleap(a + b) == isleap(a % 400 + b % 400)
103** This is true even if % means modulo rather than Fortran remainder
104** (which is allowed by C89 but not by C99 or later).
105** We use this to avoid addition overflow problems.
106*/
107
108#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
109
110#if defined(_MSC_VER) || defined(__BORLANDC__)
111#define tzname _tzname
112#define strncasecmp _strnicmp
113#endif
114
115#ifdef __WATCOMC__
116#define _tzset tzset
117#endif
118
119#ifdef __BORLANDC__
120char* cm_strdup(const char* str);
121
122int _strnicmp(const char* _a, const char* _b, int len){
123 char* a = cm_strdup(_a);
124 char* b = cm_strdup(_b);
125 int i;
126 char* r;
127 for(i = 0; a[i] != 0; i++){
128 a[i] = tolower(a[i]);
129 }
130 for(i = 0; b[i] != 0; i++){
131 b[i] = tolower(b[i]);
132 }
133 r = strncmp(a, b, len);
134 free(a);
135 free(b);
136 return r;
137}
138#endif
139
140#ifdef TM_ZONE
141static char* utc = "UTC";
142#endif
143/* RFC-822/RFC-2822 */
144static const char* const nast[] = {
145 "EST", "CST", "MST", "PST", "\0\0\0"
146};
147static const char* const nadt[] = {
148 "EDT", "CDT", "MDT", "PDT", "\0\0\0"
149};
150static const char* weekday_name[] =
151{
152 "Sunday", "Monday", "Tuesday", "Wednesday",
153 "Thursday", "Friday", "Saturday"
154};
155static const char* ab_weekday_name[] =
156{
157 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
158};
159static const char* month_name[] =
160{
161 "January", "February", "March", "April", "May", "June",
162 "July", "August", "September", "October", "November", "December"
163};
164static const char* ab_month_name[] =
165{
166 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
167 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
168};
169static const char* am_pm[] = {"AM", "PM"};
170
171
172/*
173 * Table to determine the ordinal date for the start of a month.
174 * Ref: http://en.wikipedia.org/wiki/ISO_week_date
175 */
176static const int start_of_month[2][13] = {
177 /* non-leap year */
178 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
179 /* leap year */
180 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
181};
182
183/*
184 * Calculate the week day of the first day of a year. Valid for
185 * the Gregorian calendar, which began Sept 14, 1752 in the UK
186 * and its colonies. Ref:
187 * http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
188 */
189
190static int
191first_wday_of(int yr)
192{
193 return ((2 * (3 - (yr / 100) % 4)) + (yr % 100) + ((yr % 100) / 4) +
194 (isleap(yr) ? 6 : 0) + 1) % 7;
195}
196
197#define delim(p) ((p) == '\0' || isspace((unsigned char)(p)))
198
199static int
200fromzone(const unsigned char **bp, struct tm *tm, int mandatory)
201{
202// timezone_t tz;
203 char buf[512], *p;
204 const unsigned char *rp;
205
206 for (p = buf, rp = *bp; !delim(*rp) && p < &buf[sizeof(buf) - 1]; rp++)
207 *p++ = *rp;
208 *p = '\0';
209
210 if (mandatory)
211 *bp = rp;
212 if (!isalnum((unsigned char)*buf))
213 return 0;
214// tz = tzalloc(buf);
215// if (tz == NULL)
216// return 0;
217
218 *bp = rp;
219 tm->tm_isdst = 0; /* XXX */
220#ifdef TM_GMTOFF
221 tm->TM_GMTOFF = tzgetgmtoff(tz, tm->tm_isdst);
222#endif
223#ifdef TM_ZONE
224 // Can't use tzgetname() here because we are going to free()
225 tm->TM_ZONE = NULL; /* XXX */
226#endif
227// tzfree(tz);
228 return 1;
229}
230
231char* strptime(const char *buf, const char *fmt, struct tm *tm)
232{
233 unsigned char c;
234 const unsigned char *bp, *ep, *zname;
235 int alt_format, i, split_year = 0, neg = 0, state = 0,
236 day_offset = -1, week_offset = 0, offs, mandatory;
237 const char *new_fmt;
238
239 bp = (const unsigned char *)buf;
240
241 while (bp != NULL && (c = *fmt++) != '\0') {
242 /* Clear `alternate' modifier prior to new conversion. */
243 alt_format = 0;
244 i = 0;
245
246 /* Eat up white-space. */
247 if (isspace(c)) {
248 while (isspace(*bp))
249 bp++;
250 continue;
251 }
252
253 if (c != '%')
254 goto literal;
255
256
257again: switch (c = *fmt++) {
258 case '%': /* "%%" is converted to "%". */
259literal:
260 if (c != *bp++)
261 return NULL;
262 LEGAL_ALT(0);
263 continue;
264
265 /*
266 * "Alternative" modifiers. Just set the appropriate flag
267 * and start over again.
268 */
269 case 'E': /* "%E?" alternative conversion modifier. */
270 LEGAL_ALT(0);
271 alt_format |= ALT_E;
272 goto again;
273
274 case 'O': /* "%O?" alternative conversion modifier. */
275 LEGAL_ALT(0);
276 alt_format |= ALT_O;
277 goto again;
278
279 /*
280 * "Complex" conversion rules, implemented through recursion.
281 */
282 case 'c': /* Date and time, using the locale's format. */
283// new_fmt = _TIME_LOCALE(loc)->d_t_fmt;
284 new_fmt = HERE_D_T_FMT;
285 state |= S_WDAY | S_MON | S_MDAY | S_YEAR;
286 goto recurse;
287
288 case 'F': /* The date as "%Y-%m-%d". */
289 new_fmt = "%Y-%m-%d";
290 LEGAL_ALT(0);
291 state |= S_MON | S_MDAY | S_YEAR;
292 goto recurse;
293
294 case 'R': /* The time as "%H:%M". */
295 new_fmt = "%H:%M";
296 LEGAL_ALT(0);
297 goto recurse;
298
299 case 'r': /* The time in 12-hour clock representation. */
300// new_fmt = _TIME_LOCALE(loc)->t_fmt_ampm;
301 new_fmt = HERE_T_FMT_AMPM;
302 LEGAL_ALT(0);
303 goto recurse;
304
305 case 'X': /* The time, using the locale's format. */
306 /* fall through */
307
308 case 'T': /* The time as "%H:%M:%S". */
309 new_fmt = HERE_T_FMT;
310 LEGAL_ALT(0);
311
312recurse:
313 bp = (const unsigned char *)strptime((const char *)bp,
314 new_fmt, tm);
315 LEGAL_ALT(ALT_E);
316 continue;
317
318 case 'x': /* The date, using the locale's format. */
319 /* fall throug */
320
321 case 'D': /* The date as "%y/%m/%d". */
322 {
323 int year;
324 new_fmt = HERE_D_FMT;
325 LEGAL_ALT(0);
326 state |= S_MON | S_MDAY | S_YEAR;
327 year = split_year ? tm->tm_year : 0;
328
329 bp = (const unsigned char *)strptime((const char *)bp,
330 new_fmt, tm);
331 LEGAL_ALT(ALT_E);
332 tm->tm_year += year;
333 if (split_year && tm->tm_year % (2000 - TM_YEAR_BASE) <= 68)
334 tm->tm_year -= 2000 - TM_YEAR_BASE;
335 split_year = 1;
336 continue;
337 }
338 /*
339 * "Elementary" conversion rules.
340 */
341 case 'A': /* The day of week, using the locale's form. */
342 case 'a':
343 bp = find_string(bp, &tm->tm_wday, weekday_name, ab_weekday_name, 7);
344 LEGAL_ALT(0);
345 state |= S_WDAY;
346 continue;
347
348 case 'B': /* The month, using the locale's form. */
349 case 'b':
350 case 'h':
351 bp = find_string(bp, &tm->tm_mon, month_name, ab_month_name, 12);
352 LEGAL_ALT(0);
353 state |= S_MON;
354 continue;
355
356 case 'C': /* The century number. */
357 i = 20;
358 bp = conv_num(bp, &i, 0, 99);
359
360 i = i * 100 - TM_YEAR_BASE;
361 if (split_year)
362 i += tm->tm_year % 100;
363 split_year = 1;
364 tm->tm_year = i;
365 LEGAL_ALT(ALT_E);
366 state |= S_YEAR;
367 continue;
368
369 case 'd': /* The day of month. */
370 case 'e':
371 bp = conv_num(bp, &tm->tm_mday, 1, 31);
372 LEGAL_ALT(ALT_O);
373 state |= S_MDAY;
374 continue;
375
376 case 'k': /* The hour (24-hour clock representation). */
377 LEGAL_ALT(0);
378 /* FALLTHROUGH */
379 case 'H':
380 bp = conv_num(bp, &tm->tm_hour, 0, 23);
381 LEGAL_ALT(ALT_O);
382 state |= S_HOUR;
383 continue;
384
385 case 'l': /* The hour (12-hour clock representation). */
386 LEGAL_ALT(0);
387 /* FALLTHROUGH */
388 case 'I':
389 bp = conv_num(bp, &tm->tm_hour, 1, 12);
390 if (tm->tm_hour == 12)
391 tm->tm_hour = 0;
392 LEGAL_ALT(ALT_O);
393 state |= S_HOUR;
394 continue;
395
396 case 'j': /* The day of year. */
397 i = 1;
398 bp = conv_num(bp, &i, 1, 366);
399 tm->tm_yday = i - 1;
400 LEGAL_ALT(0);
401 state |= S_YDAY;
402 continue;
403
404 case 'M': /* The minute. */
405 bp = conv_num(bp, &tm->tm_min, 0, 59);
406 LEGAL_ALT(ALT_O);
407 continue;
408
409 case 'm': /* The month. */
410 i = 1;
411 bp = conv_num(bp, &i, 1, 12);
412 tm->tm_mon = i - 1;
413 LEGAL_ALT(ALT_O);
414 state |= S_MON;
415 continue;
416
417 case 'p': /* The locale's equivalent of AM/PM. */
418 bp = find_string(bp, &i, am_pm, NULL, 2);
419 if (HAVE_HOUR(state) && tm->tm_hour > 11)
420 return NULL;
421 tm->tm_hour += i * 12;
422 LEGAL_ALT(0);
423 continue;
424
425 case 'S': /* The seconds. */
426 bp = conv_num(bp, &tm->tm_sec, 0, 61);
427 LEGAL_ALT(ALT_O);
428 continue;
429
430#ifndef TIME_MAX
431#if defined(_MSC_VER) || defined(__BORLANDC__)
432#define TIME_MAX INT32_MAX
433#else
434#define TIME_MAX INT64_MAX
435#endif
436#endif
437 case 's': /* seconds since the epoch */
438 {
439 time_t sse = 0;
440#if defined(_MSC_VER) || defined(__BORLANDC__)
441 uint32_t rulim = TIME_MAX;
442#else
443 uint64_t rulim = TIME_MAX;
444#endif
445
446 if (*bp < '0' || *bp > '9') {
447 bp = NULL;
448 continue;
449 }
450
451 do {
452 sse *= 10;
453 sse += *bp++ - '0';
454 rulim /= 10;
455 } while ((sse * 10 <= TIME_MAX) &&
456 rulim && *bp >= '0' && *bp <= '9');
457#if defined(_MSC_VER) || defined(__BORLANDC__)
458 if (sse < 0 || (uint32_t)sse > TIME_MAX) {
459#else
460 if (sse < 0 || (uint64_t)sse > TIME_MAX) {
461#endif
462 bp = NULL;
463 continue;
464 }
465#ifdef _WIN32
466#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
467 if (1)
468#else
469 if (localtime_s(tm, &sse) == 0)
470#endif
471#else
472 if (localtime_r(&sse, tm))
473#endif
474 state |= S_YDAY | S_WDAY | S_MON | S_MDAY | S_YEAR;
475 else
476 bp = NULL;
477 }
478 continue;
479
480 case 'U': /* The week of year, beginning on sunday. */
481 case 'W': /* The week of year, beginning on monday. */
482 /*
483 * This is bogus, as we can not assume any valid
484 * information present in the tm structure at this
485 * point to calculate a real value, so save the
486 * week for now in case it can be used later.
487 */
488 bp = conv_num(bp, &i, 0, 53);
489 LEGAL_ALT(ALT_O);
490 if (c == 'U')
491 day_offset = TM_SUNDAY;
492 else
493 day_offset = TM_MONDAY;
494 week_offset = i;
495 continue;
496
497 case 'w': /* The day of week, beginning on sunday. */
498 bp = conv_num(bp, &tm->tm_wday, 0, 6);
499 LEGAL_ALT(ALT_O);
500 state |= S_WDAY;
501 continue;
502
503 case 'u': /* The day of week, monday = 1. */
504 bp = conv_num(bp, &i, 1, 7);
505 tm->tm_wday = i % 7;
506 LEGAL_ALT(ALT_O);
507 state |= S_WDAY;
508 continue;
509
510 case 'g': /* The year corresponding to the ISO week
511 * number but without the century.
512 */
513 bp = conv_num(bp, &i, 0, 99);
514 continue;
515
516 case 'G': /* The year corresponding to the ISO week
517 * number with century.
518 */
519 do
520 bp++;
521 while (isdigit(*bp));
522 continue;
523
524 case 'V': /* The ISO 8601:1988 week number as decimal */
525 bp = conv_num(bp, &i, 0, 53);
526 continue;
527
528 case 'Y': /* The year. */
529 i = TM_YEAR_BASE; /* just for data sanity... */
530 bp = conv_num(bp, &i, 0, 9999);
531 tm->tm_year = i - TM_YEAR_BASE;
532 LEGAL_ALT(ALT_E);
533 state |= S_YEAR;
534 continue;
535
536 case 'y': /* The year within 100 years of the epoch. */
537 /* LEGAL_ALT(ALT_E | ALT_O); */
538 bp = conv_num(bp, &i, 0, 99);
539
540 if (split_year)
541 /* preserve century */
542 i += (tm->tm_year / 100) * 100;
543 else {
544 split_year = 1;
545 if (i <= 68)
546 i = i + 2000 - TM_YEAR_BASE;
547 }
548 tm->tm_year = i;
549 state |= S_YEAR;
550 continue;
551
552 case 'Z': // time zone name
553 case 'z': //
554#ifdef _WIN32
555 _tzset();
556#else
557 tzset();
558#endif
559 mandatory = c == 'z';
560 /*
561 * We recognize all ISO 8601 formats:
562 * Z = Zulu time/UTC
563 * [+-]hhmm
564 * [+-]hh:mm
565 * [+-]hh
566 * We recognize all RFC-822/RFC-2822 formats:
567 * UT|GMT
568 * North American : UTC offsets
569 * E[DS]T = Eastern : -4 | -5
570 * C[DS]T = Central : -5 | -6
571 * M[DS]T = Mountain: -6 | -7
572 * P[DS]T = Pacific : -7 | -8
573 * Nautical/Military
574 * [A-IL-M] = -1 ... -9 (J not used)
575 * [N-Y] = +1 ... +12
576 * Note: J maybe used to denote non-nautical
577 * local time
578 */
579 if (mandatory)
580 while (isspace(*bp))
581 bp++;
582
583 zname = bp;
584 switch (*bp++) {
585 case 'G':
586 if (*bp++ != 'M')
587 goto namedzone;
588 /*FALLTHROUGH*/
589 case 'U':
590 if (*bp++ != 'T')
591 goto namedzone;
592 else if (!delim(*bp) && *bp++ != 'C')
593 goto namedzone;
594 /*FALLTHROUGH*/
595 case 'Z':
596 if (!delim(*bp))
597 goto namedzone;
598 tm->tm_isdst = 0;
599#ifdef TM_GMTOFF
600 tm->TM_GMTOFF = 0;
601#endif
602#ifdef TM_ZONE
603 tm->TM_ZONE = utc;
604#endif
605 continue;
606 case '+':
607 neg = 0;
608 break;
609 case '-':
610 neg = 1;
611 break;
612 default:
613namedzone:
614 bp = zname;
615
616 /* Nautical / Military style */
617 if (delim(bp[1]) &&
618 ((*bp >= 'A' && *bp <= 'I') ||
619 (*bp >= 'L' && *bp <= 'Y'))) {
620#ifdef TM_GMTOFF
621 /* Argh! No 'J'! */
622 if (*bp >= 'A' && *bp <= 'I')
623 tm->TM_GMTOFF =
624 (int)*bp - ('A' - 1);
625 else if (*bp >= 'L' && *bp <= 'M')
626 tm->TM_GMTOFF = (int)*bp - 'A';
627 else if (*bp >= 'N' && *bp <= 'Y')
628 tm->TM_GMTOFF = 'M' - (int)*bp;
629 tm->TM_GMTOFF *= SECSPERHOUR;
630#endif
631#ifdef TM_ZONE
632 tm->TM_ZONE = NULL; /* XXX */
633#endif
634 bp++;
635 continue;
636 }
637 /* 'J' is local time */
638 if (delim(bp[1]) && *bp == 'J') {
639#ifdef TM_GMTOFF
640 tm->TM_GMTOFF = -timezone;
641#endif
642#ifdef TM_ZONE
643 tm->TM_ZONE = NULL; /* XXX */
644#endif
645 bp++;
646 continue;
647 }
648
649 /*
650 * From our 3 letter hard-coded table
651 * XXX: Can be removed, handled by tzload()
652 */
653 if (delim(bp[0]) || delim(bp[1]) ||
654 delim(bp[2]) || !delim(bp[3]))
655 goto loadzone;
656 ep = find_string(bp, &i, nast, NULL, 4);
657 if (ep != NULL) {
658#ifdef TM_GMTOFF
659 tm->TM_GMTOFF = (-5 - i) * SECSPERHOUR;
660#endif
661#ifdef TM_ZONE
662 tm->TM_ZONE = __UNCONST(nast[i]);
663#endif
664 bp = ep;
665 continue;
666 }
667 ep = find_string(bp, &i, nadt, NULL, 4);
668 if (ep != NULL) {
669 tm->tm_isdst = 1;
670#ifdef TM_GMTOFF
671 tm->TM_GMTOFF = (-4 - i) * SECSPERHOUR;
672#endif
673#ifdef TM_ZONE
674 tm->TM_ZONE = __UNCONST(nadt[i]);
675#endif
676 bp = ep;
677 continue;
678 }
679 /*
680 * Our current timezone
681 */
682 ep = find_string(bp, &i,
683 (const char * const *)NULL,
684 NULL, 2);
685 if (ep != NULL) {
686 tm->tm_isdst = i;
687#ifdef TM_GMTOFF
688 tm->TM_GMTOFF = -timezone;
689#endif
690#ifdef TM_ZONE
691 tm->TM_ZONE = tzname[i];
692#endif
693 bp = ep;
694 continue;
695 }
696loadzone:
697 /*
698 * The hard way, load the zone!
699 */
700 if (fromzone(&bp, tm, mandatory))
701 continue;
702 goto out;
703 }
704 offs = 0;
705 for (i = 0; i < 4; ) {
706 if (isdigit(*bp)) {
707 offs = offs * 10 + (*bp++ - '0');
708 i++;
709 continue;
710 }
711 if (i == 2 && *bp == ':') {
712 bp++;
713 continue;
714 }
715 break;
716 }
717 if (isdigit(*bp))
718 goto out;
719 switch (i) {
720 case 2:
721 offs *= SECSPERHOUR;
722 break;
723 case 4:
724 i = offs % 100;
725 offs /= 100;
726 if (i >= SECSPERMIN)
727 goto out;
728 /* Convert minutes into decimal */
729 offs = offs * SECSPERHOUR + i * SECSPERMIN;
730 break;
731 default:
732out:
733 if (mandatory)
734 return NULL;
735 bp = zname;
736 continue;
737 }
738 /* ISO 8601 & RFC 3339 limit to 23:59 max */
739 if (offs >= (HOURSPERDAY * SECSPERHOUR))
740 goto out;
741 if (neg)
742 offs = -offs;
743 tm->tm_isdst = 0; /* XXX */
744#ifdef TM_GMTOFF
745 tm->TM_GMTOFF = offs;
746#endif
747#ifdef TM_ZONE
748 tm->TM_ZONE = NULL; /* XXX */
749#endif
750 continue;
751
752 /*
753 * Miscellaneous conversions.
754 */
755 case 'n': /* Any kind of white-space. */
756 case 't':
757 while (isspace(*bp))
758 bp++;
759 LEGAL_ALT(0);
760 continue;
761
762
763 default: /* Unknown/unsupported conversion. */
764 return NULL;
765 }
766 }
767
768 if (!HAVE_YDAY(state) && HAVE_YEAR(state)) {
769 if (HAVE_MON(state) && HAVE_MDAY(state)) {
770 /* calculate day of year (ordinal date) */
771 tm->tm_yday = start_of_month[isleap_sum(tm->tm_year,
772 TM_YEAR_BASE)][tm->tm_mon] + (tm->tm_mday - 1);
773 state |= S_YDAY;
774 } else if (day_offset != -1) {
775 /*
776 * Set the date to the first Sunday (or Monday)
777 * of the specified week of the year.
778 */
779 if (!HAVE_WDAY(state)) {
780 tm->tm_wday = day_offset;
781 state |= S_WDAY;
782 }
783 tm->tm_yday = (7 -
784 first_wday_of(tm->tm_year + TM_YEAR_BASE) +
785 day_offset) % 7 + (week_offset - 1) * 7 +
786 tm->tm_wday - day_offset;
787 state |= S_YDAY;
788 }
789 }
790
791 if (HAVE_YDAY(state) && HAVE_YEAR(state)) {
792 int isleap;
793
794 if (!HAVE_MON(state)) {
795 /* calculate month of day of year */
796 i = 0;
797 isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
798 while (tm->tm_yday >= start_of_month[isleap][i])
799 i++;
800 if (i > 12) {
801 i = 1;
802 tm->tm_yday -= start_of_month[isleap][12];
803 tm->tm_year++;
804 }
805 tm->tm_mon = i - 1;
806 state |= S_MON;
807 }
808
809 if (!HAVE_MDAY(state)) {
810 /* calculate day of month */
811 isleap = isleap_sum(tm->tm_year, TM_YEAR_BASE);
812 tm->tm_mday = tm->tm_yday -
813 start_of_month[isleap][tm->tm_mon] + 1;
814 state |= S_MDAY;
815 }
816
817 if (!HAVE_WDAY(state)) {
818 /* calculate day of week */
819 i = 0;
820 week_offset = first_wday_of(tm->tm_year);
821 while (i++ <= tm->tm_yday) {
822 if (week_offset++ >= 6)
823 week_offset = 0;
824 }
825 tm->tm_wday = week_offset;
826 state |= S_WDAY;
827 }
828 }
829
830 return (char*)bp;
831}
832
833
834static const unsigned char *
835conv_num(const unsigned char *buf, int *dest, unsigned int llim, unsigned int ulim)
836{
837 unsigned int result = 0;
838 unsigned char ch;
839
840 /* The limit also determines the number of valid digits. */
841 unsigned int rulim = ulim;
842
843 ch = *buf;
844 if (ch < '0' || ch > '9')
845 return NULL;
846
847 do {
848 result *= 10;
849 result += ch - '0';
850 rulim /= 10;
851 ch = *++buf;
852 } while ((result <= ulim) && rulim && ch >= '0' && ch <= '9');
853
854 if (result < llim || result > ulim)
855 return NULL;
856
857 *dest = result;
858 return buf;
859}
860
861static const unsigned char *
862find_string(const unsigned char *bp, int *tgt, const char * const *n1,
863 const char * const *n2, int c)
864{
865 int i;
866 size_t len;
867
868 /* check full name - then abbreviated ones */
869 for (; n1 != NULL; n1 = n2, n2 = NULL) {
870 for (i = 0; i < c; i++, n1++) {
871 len = strlen(*n1);
872 if (strncasecmp(*n1, (const char *)bp, len) == 0) {
873 *tgt = i;
874 return bp + len;
875 }
876 }
877 }
878
879 /* Nothing matched */
880 return NULL;
881}
882
883#endif
Note: See TracBrowser for help on using the repository browser.