Subversion Repositories Tewi

Rev

Rev 212 | Rev 219 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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