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

Last change on this file since 241 was 241, checked in by Nishi, on Oct 3, 2024 at 3:13:22 PM

fix some warning

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