Subversion Repositories Tewi

Rev

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