1 /* date.c - Date-parsing utility for the notmuch mail system.
3 * Copyright © 2000-2009 Jeffrey Stedfast
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/
19 /* This code was originally written by from Jeffrey Stedfast
20 * as part of his GMime library (http://spruce.sourceforge.net/gmime/)
22 * Carl Worth <cworth@cworth.org> imported it into notmuch and removed
26 #include "notmuch-private.h"
30 #include <glib.h> /* For g_ascii_strncasecmp only. */
42 #define GMIME_FOLD_PREENCODED (GMIME_FOLD_LEN / 2)
44 /* date parser macros */
45 #define NUMERIC_CHARS "1234567890"
46 #define WEEKDAY_CHARS "SundayMondayTuesdayWednesdayThursdayFridaySaturday"
47 #define MONTH_CHARS "JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember"
48 #define TIMEZONE_ALPHA_CHARS "UTCGMTESTEDTCSTCDTMSTPSTPDTZAMNY()"
49 #define TIMEZONE_NUMERIC_CHARS "-+1234567890"
50 #define TIME_CHARS "1234567890:"
52 #define DATE_TOKEN_NON_NUMERIC (1 << 0)
53 #define DATE_TOKEN_NON_WEEKDAY (1 << 1)
54 #define DATE_TOKEN_NON_MONTH (1 << 2)
55 #define DATE_TOKEN_NON_TIME (1 << 3)
56 #define DATE_TOKEN_HAS_COLON (1 << 4)
57 #define DATE_TOKEN_NON_TIMEZONE_ALPHA (1 << 5)
58 #define DATE_TOKEN_NON_TIMEZONE_NUMERIC (1 << 6)
59 #define DATE_TOKEN_HAS_SIGN (1 << 7)
61 static unsigned char gmime_datetok_table[256] = {
62 128,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
63 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
64 111,111,111,111,111,111,111,111, 79, 79,111,175,111,175,111,111,
65 38, 38, 38, 38, 38, 38, 38, 38, 38, 38,119,111,111,111,111,111,
66 111, 75,111, 79, 75, 79,105, 79,111,111,107,111,111, 73, 75,107,
67 79,111,111, 73, 77, 79,111,109,111, 79, 79,111,111,111,111,111,
68 111,105,107,107,109,105,111,107,105,105,111,111,107,107,105,105,
69 107,111,105,105,105,105,107,111,111,105,111,111,111,111,111,111,
70 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
71 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
72 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
73 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
74 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
75 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
76 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
77 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
80 /* hrm, is there a library for this stuff? */
87 { "EST", -500 }, /* these are all US timezones. bloody yanks */
102 static char *tm_months[] = {
103 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
104 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
107 static char *tm_days[] = {
108 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
111 /* This is where it gets ugly... */
113 typedef struct _date_token {
114 struct _date_token *next;
120 #define date_token_free(tok) free (tok)
121 #define date_token_new() malloc (sizeof (date_token))
124 datetok (const char *date)
126 date_token *tokens = NULL, *token, *tail = (date_token *) &tokens;
127 const char *start, *end;
132 /* kill leading whitespace */
133 while (*start == ' ' || *start == '\t')
139 mask = gmime_datetok_table[(unsigned char) *start];
141 /* find the end of this token */
143 while (*end && !strchr ("-/,\t\r\n ", *end))
144 mask |= gmime_datetok_table[(unsigned char) *end++];
147 token = date_token_new ();
149 token->start = start;
150 token->len = end - start;
167 decode_int (const char *in, size_t inlen)
169 register const char *inptr;
170 int sign = 1, val = 0;
179 } else if (*inptr == '+')
182 for ( ; inptr < inend; inptr++) {
183 if (!(*inptr >= '0' && *inptr <= '9'))
186 val = (val * 10) + (*inptr - '0');
196 get_days_in_month (int month, int year)
213 if (g_date_is_leap_year (year))
224 get_wday (const char *in, size_t inlen)
234 for (wday = 0; wday < 7; wday++) {
235 if (!g_ascii_strncasecmp (in, tm_days[wday], 3))
239 return -1; /* unknown week day */
243 get_mday (const char *in, size_t inlen)
250 mday = decode_int (in, inlen);
252 if (mday < 0 || mday > 31)
259 get_month (const char *in, size_t inlen)
269 for (i = 0; i < 12; i++) {
270 if (!g_ascii_strncasecmp (in, tm_months[i], 3))
274 return -1; /* unknown month */
278 get_year (const char *in, size_t inlen)
285 if ((year = decode_int (in, inlen)) == -1)
289 year += (year < 70) ? 2000 : 1900;
298 get_time (const char *in, size_t inlen, int *hour, int *min, int *sec)
300 register const char *inptr;
301 int *val, colons = 0;
304 *hour = *min = *sec = 0;
308 for (inptr = in; inptr < inend; inptr++) {
321 } else if (!(*inptr >= '0' && *inptr <= '9'))
324 *val = (*val * 10) + (*inptr - '0');
331 get_tzone (date_token **token)
333 const char *inptr, *inend;
337 for (i = 0; *token && i < 2; *token = (*token)->next, i++) {
338 inptr = (*token)->start;
339 inlen = (*token)->len;
340 inend = inptr + inlen;
342 if (*inptr == '+' || *inptr == '-') {
343 return decode_int (inptr, inlen);
347 if (*(inend - 1) == ')')
353 for (t = 0; t < 15; t++) {
354 size_t len = strlen (tz_offsets[t].name);
359 if (!strncmp (inptr, tz_offsets[t].name, len))
360 return tz_offsets[t].offset;
368 #define HAVE_TIMEZONE
371 mktime_utc (struct tm *tm)
379 #if defined (G_OS_WIN32)
381 if (tm->tm_isdst > 0) {
387 #elif defined (HAVE_TM_GMTOFF)
389 #elif defined (HAVE_TIMEZONE)
390 if (tm->tm_isdst > 0) {
391 #if defined (HAVE_ALTZONE)
393 #else /* !defined (HAVE_ALTZONE) */
394 tz = (timezone - 3600);
399 #elif defined (HAVE__TIMEZONE)
402 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
409 parse_rfc822_date (date_token *tokens, int *tzone)
411 int hour, min, sec, offset, n;
421 memset ((void *) &tm, 0, sizeof (struct tm));
423 if ((n = get_wday (token->start, token->len)) != -1) {
424 /* not all dates may have this... */
430 if (!token || (n = get_mday (token->start, token->len)) == -1)
437 if (!token || (n = get_month (token->start, token->len)) == -1)
444 if (!token || (n = get_year (token->start, token->len)) == -1)
447 tm.tm_year = n - 1900;
450 /* get the hour/min/sec */
451 if (!token || !get_time (token->start, token->len, &hour, &min, &sec))
459 /* get the timezone */
460 if (!token || (n = get_tzone (&token)) == -1) {
461 /* I guess we assume tz is GMT? */
467 t = mktime_utc (&tm);
469 /* t is now GMT of the time we want, but not offset by the timezone ... */
471 /* this should convert the time to the GMT equiv time */
472 t -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
481 #define date_token_mask(t) (((date_token *) t)->mask)
482 #define is_numeric(t) ((date_token_mask (t) & DATE_TOKEN_NON_NUMERIC) == 0)
483 #define is_weekday(t) ((date_token_mask (t) & DATE_TOKEN_NON_WEEKDAY) == 0)
484 #define is_month(t) ((date_token_mask (t) & DATE_TOKEN_NON_MONTH) == 0)
485 #define is_time(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIME) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_COLON))
486 #define is_tzone_alpha(t) ((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_ALPHA) == 0)
487 #define is_tzone_numeric(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_NUMERIC) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_SIGN))
488 #define is_tzone(t) (is_tzone_alpha (t) || is_tzone_numeric (t))
491 parse_broken_date (date_token *tokens, int *tzone)
493 int got_wday, got_month, got_tzone;
494 int hour, min, sec, offset, n;
499 memset ((void *) &tm, 0, sizeof (struct tm));
500 got_wday = got_month = got_tzone = FALSE;
505 if (is_weekday (token) && !got_wday) {
506 if ((n = get_wday (token->start, token->len)) != -1) {
507 d(printf ("weekday; "));
514 if (is_month (token) && !got_month) {
515 if ((n = get_month (token->start, token->len)) != -1) {
516 d(printf ("month; "));
523 if (is_time (token) && !tm.tm_hour && !tm.tm_min && !tm.tm_sec) {
524 if (get_time (token->start, token->len, &hour, &min, &sec)) {
525 d(printf ("time; "));
533 if (is_tzone (token) && !got_tzone) {
534 date_token *t = token;
536 if ((n = get_tzone (&t)) != -1) {
537 d(printf ("tzone; "));
544 if (is_numeric (token)) {
545 if (token->len == 4 && !tm.tm_year) {
546 if ((n = get_year (token->start, token->len)) != -1) {
547 d(printf ("year; "));
548 tm.tm_year = n - 1900;
552 /* Note: assumes MM-DD-YY ordering if '0 < MM < 12' holds true */
553 if (!got_month && token->next && is_numeric (token->next)) {
554 if ((n = decode_int (token->start, token->len)) > 12) {
562 } else if (!tm.tm_mday && (n = get_mday (token->start, token->len)) != -1) {
564 d(printf ("mday; "));
567 } else if (!tm.tm_year) {
568 if ((n = get_year (token->start, token->len)) != -1) {
569 d(printf ("2-digit year; "));
570 tm.tm_year = n - 1900;
586 t = mktime_utc (&tm);
588 /* t is now GMT of the time we want, but not offset by the timezone ... */
590 /* this should convert the time to the GMT equiv time */
591 t -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
601 gmime_datetok_table_init (void)
605 memset (gmime_datetok_table, 0, sizeof (gmime_datetok_table));
607 for (i = 0; i < 256; i++) {
608 if (!strchr (NUMERIC_CHARS, i))
609 gmime_datetok_table[i] |= DATE_TOKEN_NON_NUMERIC;
611 if (!strchr (WEEKDAY_CHARS, i))
612 gmime_datetok_table[i] |= DATE_TOKEN_NON_WEEKDAY;
614 if (!strchr (MONTH_CHARS, i))
615 gmime_datetok_table[i] |= DATE_TOKEN_NON_MONTH;
617 if (!strchr (TIME_CHARS, i))
618 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIME;
620 if (!strchr (TIMEZONE_ALPHA_CHARS, i))
621 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_ALPHA;
623 if (!strchr (TIMEZONE_NUMERIC_CHARS, i))
624 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_NUMERIC;
626 if (((char) i) == ':')
627 gmime_datetok_table[i] |= DATE_TOKEN_HAS_COLON;
629 if (strchr ("+-", i))
630 gmime_datetok_table[i] |= DATE_TOKEN_HAS_SIGN;
633 printf ("static unsigned char gmime_datetok_table[256] = {");
634 for (i = 0; i < 256; i++) {
637 printf ("%3d,", gmime_datetok_table[i]);
644 notmuch_parse_date (const char *str, int *tz_offset)
646 date_token *token, *tokens;
652 if (!(tokens = datetok (str))) {
659 if (!(date = parse_rfc822_date (tokens, tz_offset)))
660 date = parse_broken_date (tokens, tz_offset);
665 tokens = tokens->next;
666 date_token_free (token);