aboutsummaryrefslogtreecommitdiffstats
path: root/camel/broken-date-parser.c
blob: cba19208b47ecf2be7f10ec2d374c01572fcf3a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2000 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <time.h>

#include "broken-date-parser.h"
#include "e-time-utils.h"

#define d(x) x

#define NUMERIC_CHARS          "1234567890"
#define WEEKDAY_CHARS          "SundayMondayTuesdayWednesdayThursdayFridaySaturday"
#define MONTH_CHARS            "JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember"
#define TIMEZONE_ALPHA_CHARS   "UTCGMTESTEDTCSTCDTMSTPSTPDTZAMNY()"
#define TIMEZONE_NUMERIC_CHARS "-+1234567890"
#define TIME_CHARS             "1234567890:"

#define DATE_TOKEN_NON_NUMERIC          (1 << 0)
#define DATE_TOKEN_NON_WEEKDAY          (1 << 1)
#define DATE_TOKEN_NON_MONTH            (1 << 2)
#define DATE_TOKEN_NON_TIME             (1 << 3)
#define DATE_TOKEN_HAS_COLON            (1 << 4)
#define DATE_TOKEN_NON_TIMEZONE_ALPHA   (1 << 5)
#define DATE_TOKEN_NON_TIMEZONE_NUMERIC (1 << 6)
#define DATE_TOKEN_HAS_SIGN             (1 << 7)

static unsigned char datetok_table[256] = {
        128,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111, 79, 79,111,175,111,175,111,111,
         38, 38, 38, 38, 38, 38, 38, 38, 38, 38,119,111,111,111,111,111,
        111, 75,111, 79, 75, 79,105, 79,111,111,107,111,111, 73, 75,107,
         79,111,111, 73, 77, 79,111,109,111, 79, 79,111,111,111,111,111,
        111,105,107,107,109,105,111,107,105,105,111,111,107,107,105,105,
        107,111,105,105,105,105,107,111,111,105,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
        111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
};

/* hrm, is there a library for this shit? */
static struct {
    char *name;
    int offset;
} tz_offsets [] = {
    { "UT", 0 },
    { "GMT", 0 },
    { "EST", -500 },    /* these are all US timezones.  bloody yanks */
    { "EDT", -400 },
    { "CST", -600 },
    { "CDT", -500 },
    { "MST", -700 },
    { "MDT", -600 },
    { "PST", -800 },
    { "PDT", -700 },
    { "Z", 0 },
    { "A", -100 },
    { "M", -1200 },
    { "N", 100 },
    { "Y", 1200 },
};

static char *tm_months[] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

static char *tm_days[] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};


struct _date_token {
    struct _date_token *next;
    const unsigned char *start;
    unsigned int len;
    unsigned int mask;
};

/* This is where it gets ugly... */
static struct _date_token *
datetok (const char *date)
{
    struct _date_token *tokens = NULL, *token, *tail = (struct _date_token *) &tokens;
    const unsigned char *start, *end;
    unsigned int mask;
    
    start = date;
    while (*start) {
        /* kill leading whitespace */
        for ( ; *start && isspace ((int) *start); start++);
        
        mask = 0;
        
        /* find the end of this token */
        for (end = start; *end && !strchr ("-/,\t\r\n ", *end); end++) {
            mask |= datetok_table[*end];
        }
        
        if (end != start) {
            token = g_malloc (sizeof (struct _date_token));
            token->next = NULL;
            token->start = start;
            token->len = end - start;
            token->mask = mask;
            
            tail->next = token;
            tail = token;
        }
        
        if (*end)
            start = end + 1;
        else
            break;
    }
    
    return tokens;
}

static int
decode_int (const unsigned char *in, unsigned int inlen)
{
    register const unsigned char *inptr;
    const unsigned char *inend;
    int sign = 1, val = 0;
    
    inptr = in;
    inend = in + inlen;
    
    if (*inptr == '-') {
        sign = -1;
        inptr++;
    } else if (*inptr == '+')
        inptr++;
    
    for ( ; inptr < inend; inptr++) {
        if (!isdigit ((int) *inptr))
            return  -1;
        else
            val = (val * 10) + (*inptr - '0');
    }
    
    val *= sign;
    
    return val;
}

static int
get_wday (const unsigned char *in, unsigned int inlen)
{
    int wday;
    
    if (inlen < 3)
        return -1;
    
    for (wday = 0; wday < 7; wday++)
        if (!strncasecmp (in, tm_days[wday], 3))
            return wday;
    
    return -1;  /* unknown week day */
}

static int
get_mday (const unsigned char *in, unsigned int inlen)
{
    int mday;
    
    mday = decode_int (in, inlen);
    
    if (mday < 0 || mday > 31)
        mday = -1;
    
    return mday;
}

static int
get_month (const unsigned char *in, unsigned int inlen)
{
    int i;
    
    if (inlen < 3)
        return -1;
    
    for (i = 0; i < 12; i++)
        if (!strncasecmp (in, tm_months[i], 3))
            return i;
    
    return -1;  /* unknown month */
}

static int
get_year (const unsigned char *in, unsigned int inlen)
{
    int year;
    
    year = decode_int (in, inlen);
    if (year == -1)
        return -1;
    
    if (year < 100)
        year += (year < 70) ? 2000 : 1900;
    
    if (year < 1969)
        return -1;
    
    return year;
}

static gboolean
get_time (const unsigned char *in, unsigned int inlen, int *hour, int *min, int *sec)
{
    register const unsigned char *inptr;
    const unsigned char *inend;
    int *val, colons = 0;
    
    *hour = *min = *sec = 0;
    
    inend = in + inlen;
    val = hour;
    for (inptr = in; inptr < inend; inptr++) {
        if (*inptr == ':') {
            colons++;
            switch (colons) {
            case 1:
                val = min;
                break;
            case 2:
                val = sec;
                break;
            default:
                return FALSE;
            }
        } else if (!isdigit ((int) *inptr))
            return FALSE;
        else
            *val = (*val * 10) + (*inptr - '0');
    }
    
    return TRUE;
}

static int
get_tzone (struct _date_token **token)
{
    int i;
    
    for (i = 0; *token && i < 2; *token = (*token)->next, i++) {
        const unsigned char *inptr = (*token)->start;
        unsigned int inlen = (*token)->len;
        
        if (*inptr == '+' || *inptr == '-') {
            return decode_int (inptr, inlen);
        } else {
            int t;
            
            if (*inptr == '(')
                inptr++;
            
            for (t = 0; t < 15; t++) {
                unsigned int len = MIN (strlen (tz_offsets[t].name), inlen - 1);
                
                if (!strncmp (inptr, tz_offsets[t].name, len))
                    return tz_offsets[t].offset;
            }
        }
    }
    
    return -1;
}

/* This is where things get interesting... ;-) */

#define date_token_mask(t)  (((struct _date_token *) t)->mask)
#define is_numeric(t)       ((date_token_mask (t) & DATE_TOKEN_NON_NUMERIC) == 0)
#define is_weekday(t)       ((date_token_mask (t) & DATE_TOKEN_NON_WEEKDAY) == 0)
#define is_month(t)         ((date_token_mask (t) & DATE_TOKEN_NON_MONTH) == 0)
#define is_time(t)          (((date_token_mask (t) & DATE_TOKEN_NON_TIME) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_COLON))
#define is_tzone_alpha(t)   ((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_ALPHA) == 0)
#define is_tzone_numeric(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_NUMERIC) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_SIGN))
#define is_tzone(t)         (is_tzone_alpha (t) || is_tzone_numeric (t))

static time_t
decode_broken_date (struct _date_token *tokens, int *tzone)
{
    gboolean got_wday, got_month, got_tzone;
    int hour, min, sec, offset, n;
    struct _date_token *token;
    struct tm tm;
    time_t time;
    
    memset ((void *) &tm, 0, sizeof (struct tm));
    got_wday = got_month = got_tzone = FALSE;
    offset = 0;
    
    token = tokens;
    while (token) {
        if (is_weekday (token) && !got_wday) {
            if ((n = get_wday (token->start, token->len)) != -1) {
                d(printf ("weekday; "));
                got_wday = TRUE;
                tm.tm_wday = n;
                goto next_token;
            }
        }
        
        if (is_month (token) && !got_month) {
            if ((n = get_month (token->start, token->len)) != -1) {
                d(printf ("month; "));
                got_month = TRUE;
                tm.tm_mon = n;
                goto next_token;
            }
        }
        
        if (is_time (token) && !tm.tm_hour && !tm.tm_min && !tm.tm_sec) {
            if (get_time (token->start, token->len, &hour, &min, &sec)) {
                d(printf ("time; "));
                tm.tm_hour = hour;
                tm.tm_min = min;
                tm.tm_sec = sec;
                goto next_token;
            }
        }
        
        if (is_tzone (token) && !got_tzone) {
            struct _date_token *t = token;
            
            if ((n = get_tzone (&t)) != -1) {
                d(printf ("tzone; "));
                got_tzone = TRUE;
                offset = n;
                goto next_token;
            }
        }
        
        if (is_numeric (token)) {
            if (token->len == 4 && !tm.tm_year) {
                if ((n = get_year (token->start, token->len)) != -1) {
                    d(printf ("year; "));
                    tm.tm_year = n - 1900;
                    goto next_token;
                }
            } else {
                if (!got_month && !got_wday && token->next && is_numeric (token->next)) {
                    d(printf ("mon; "));
                    n = decode_int (token->start, token->len);
                    got_month = TRUE;
                    tm.tm_mon = n - 1;
                    goto next_token;
                } else if (!tm.tm_mday && (n = get_mday (token->start, token->len)) != -1) {
                    d(printf ("mday; "));
                    tm.tm_mday = n;
                    goto next_token;
                } else if (!tm.tm_year) {
                    d(printf ("2-digit year; "));
                    n = get_year (token->start, token->len);
                    tm.tm_year = n - 1900;
                    goto next_token;
                }
            }
        }
        
        d(printf ("???; "));
        
    next_token:
        
        token = token->next;
    }
    
    d(printf ("\n"));
    
    time = e_mktime_utc (&tm);
    
    /* t is now GMT of the time we want, but not offset by the timezone ... */
    
    /* this should convert the time to the GMT equiv time */
    time -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
    
    if (tzone)
        *tzone = offset;
    
    return time;
}


/**
 * parse_broken_date:
 * @datestr: input date string
 * @saveoffset:
 *
 * Decodes the rfc822/broken date string and saves the GMT offset into
 * @saveoffset if non-NULL.
 *
 * Returns the time_t representation of the date string specified by
 * @in. If 'saveoffset' is non-NULL, the value of the timezone offset
 * will be stored.
 **/
time_t
parse_broken_date (const char *datestr, int *saveoffset)
{
    struct _date_token *token, *tokens;
    time_t date;
    
    tokens = datetok (datestr);
    
    date = decode_broken_date (tokens, saveoffset);
    
    /* cleanup */
    while (tokens) {
        token = tokens;
        tokens = tokens->next;
        g_free (token);
    }
    
    return date;
}





#ifdef DATETOK_STANDALONE

static void
table_init ()
{
    int i;
    
    memset (datetok_table, 0, sizeof (datetok_table));
    
    for (i = 0; i < 256; i++) {
        if (!strchr (NUMERIC_CHARS, i))
            datetok_table[i] |= DATE_TOKEN_NON_NUMERIC;
        
        if (!strchr (WEEKDAY_CHARS, i))
            datetok_table[i] |= DATE_TOKEN_NON_WEEKDAY;
        
        if (!strchr (MONTH_CHARS, i))
            datetok_table[i] |= DATE_TOKEN_NON_MONTH;
        
        if (!strchr (TIME_CHARS, i))
            datetok_table[i] |= DATE_TOKEN_NON_TIME;
        
        if (!strchr (TIMEZONE_ALPHA_CHARS, i))
            datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_ALPHA;
        
        if (!strchr (TIMEZONE_NUMERIC_CHARS, i))
            datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_NUMERIC;
        
        if (((char) i) == ':')
            datetok_table[i] |= DATE_TOKEN_HAS_COLON;
        
        if (strchr ("+-", i))
            datetok_table[i] |= DATE_TOKEN_HAS_SIGN;
    }
    
    printf ("static unsigned int datetok_table[256] = {");
    for (i = 0; i < 256; i++) {
        if (i % 16 == 0)
            printf ("\n\t");
        printf ("%3d,", datetok_table[i]);
    }
    printf ("\n};\n");
}


int main (int argc, char **argv)
{
    time_t date;
    int offset;
    
    /*table_init ();*/
    
    date = parse_broken_date (argv[1], &offset);
    printf ("%d; %d\n", date, offset);
    
    return 0;
}

#endif /* DATETOK_STANDALONE */