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"
40 #define GMIME_FOLD_PREENCODED (GMIME_FOLD_LEN / 2)
42 /* date parser macros */
43 #define NUMERIC_CHARS "1234567890"
44 #define WEEKDAY_CHARS "SundayMondayTuesdayWednesdayThursdayFridaySaturday"
45 #define MONTH_CHARS "JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember"
46 #define TIMEZONE_ALPHA_CHARS "UTCGMTESTEDTCSTCDTMSTPSTPDTZAMNY()"
47 #define TIMEZONE_NUMERIC_CHARS "-+1234567890"
48 #define TIME_CHARS "1234567890:"
50 #define DATE_TOKEN_NON_NUMERIC (1 << 0)
51 #define DATE_TOKEN_NON_WEEKDAY (1 << 1)
52 #define DATE_TOKEN_NON_MONTH (1 << 2)
53 #define DATE_TOKEN_NON_TIME (1 << 3)
54 #define DATE_TOKEN_HAS_COLON (1 << 4)
55 #define DATE_TOKEN_NON_TIMEZONE_ALPHA (1 << 5)
56 #define DATE_TOKEN_NON_TIMEZONE_NUMERIC (1 << 6)
57 #define DATE_TOKEN_HAS_SIGN (1 << 7)
59 static unsigned char gmime_datetok_table[256] = {
60 128,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
61 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
62 111,111,111,111,111,111,111,111, 79, 79,111,175,111,175,111,111,
63 38, 38, 38, 38, 38, 38, 38, 38, 38, 38,119,111,111,111,111,111,
64 111, 75,111, 79, 75, 79,105, 79,111,111,107,111,111, 73, 75,107,
65 79,111,111, 73, 77, 79,111,109,111, 79, 79,111,111,111,111,111,
66 111,105,107,107,109,105,111,107,105,105,111,111,107,107,105,105,
67 107,111,105,105,105,105,107,111,111,105,111,111,111,111,111,111,
68 111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
69 111,111,111,111,111,111,111,111,111,111,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,
78 /* hrm, is there a library for this stuff? */
85 { "EST", -500 }, /* these are all US timezones. bloody yanks */
100 static char *tm_months[] = {
101 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
102 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
105 static char *tm_days[] = {
106 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
109 /* This is where it gets ugly... */
111 typedef struct _date_token {
112 struct _date_token *next;
118 #define date_token_free(tok) free (tok)
119 #define date_token_new() malloc (sizeof (date_token))
122 datetok (const char *date)
124 date_token *tokens = NULL, *token, *tail = (date_token *) &tokens;
125 const char *start, *end;
130 /* kill leading whitespace */
131 while (*start == ' ' || *start == '\t')
137 mask = gmime_datetok_table[(unsigned char) *start];
139 /* find the end of this token */
141 while (*end && !strchr ("-/,\t\r\n ", *end))
142 mask |= gmime_datetok_table[(unsigned char) *end++];
145 token = date_token_new ();
147 token->start = start;
148 token->len = end - start;
165 decode_int (const char *in, size_t inlen)
167 register const char *inptr;
168 int sign = 1, val = 0;
177 } else if (*inptr == '+')
180 for ( ; inptr < inend; inptr++) {
181 if (!(*inptr >= '0' && *inptr <= '9'))
184 val = (val * 10) + (*inptr - '0');
194 get_days_in_month (int month, int year)
211 if (g_date_is_leap_year (year))
222 get_wday (const char *in, size_t inlen)
232 for (wday = 0; wday < 7; wday++) {
233 if (!g_ascii_strncasecmp (in, tm_days[wday], 3))
237 return -1; /* unknown week day */
241 get_mday (const char *in, size_t inlen)
248 mday = decode_int (in, inlen);
250 if (mday < 0 || mday > 31)
257 get_month (const char *in, size_t inlen)
267 for (i = 0; i < 12; i++) {
268 if (!g_ascii_strncasecmp (in, tm_months[i], 3))
272 return -1; /* unknown month */
276 get_year (const char *in, size_t inlen)
283 if ((year = decode_int (in, inlen)) == -1)
287 year += (year < 70) ? 2000 : 1900;
296 get_time (const char *in, size_t inlen, int *hour, int *min, int *sec)
298 register const char *inptr;
299 int *val, colons = 0;
302 *hour = *min = *sec = 0;
306 for (inptr = in; inptr < inend; inptr++) {
319 } else if (!(*inptr >= '0' && *inptr <= '9'))
322 *val = (*val * 10) + (*inptr - '0');
329 get_tzone (date_token **token)
331 const char *inptr, *inend;
335 for (i = 0; *token && i < 2; *token = (*token)->next, i++) {
336 inptr = (*token)->start;
337 inlen = (*token)->len;
338 inend = inptr + inlen;
340 if (*inptr == '+' || *inptr == '-') {
341 return decode_int (inptr, inlen);
345 if (*(inend - 1) == ')')
351 for (t = 0; t < 15; t++) {
352 size_t len = strlen (tz_offsets[t].name);
357 if (!strncmp (inptr, tz_offsets[t].name, len))
358 return tz_offsets[t].offset;
366 #define HAVE_TIMEZONE
369 mktime_utc (struct tm *tm)
377 #if defined (G_OS_WIN32)
379 if (tm->tm_isdst > 0) {
385 #elif defined (HAVE_TM_GMTOFF)
387 #elif defined (HAVE_TIMEZONE)
388 if (tm->tm_isdst > 0) {
389 #if defined (HAVE_ALTZONE)
391 #else /* !defined (HAVE_ALTZONE) */
392 tz = (timezone - 3600);
397 #elif defined (HAVE__TIMEZONE)
400 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
407 parse_rfc822_date (date_token *tokens, int *tzone)
409 int hour, min, sec, offset, n;
419 memset ((void *) &tm, 0, sizeof (struct tm));
421 if ((n = get_wday (token->start, token->len)) != -1) {
422 /* not all dates may have this... */
428 if (!token || (n = get_mday (token->start, token->len)) == -1)
435 if (!token || (n = get_month (token->start, token->len)) == -1)
442 if (!token || (n = get_year (token->start, token->len)) == -1)
445 tm.tm_year = n - 1900;
448 /* get the hour/min/sec */
449 if (!token || !get_time (token->start, token->len, &hour, &min, &sec))
457 /* get the timezone */
458 if (!token || (n = get_tzone (&token)) == -1) {
459 /* I guess we assume tz is GMT? */
465 t = mktime_utc (&tm);
467 /* t is now GMT of the time we want, but not offset by the timezone ... */
469 /* this should convert the time to the GMT equiv time */
470 t -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
479 #define date_token_mask(t) (((date_token *) t)->mask)
480 #define is_numeric(t) ((date_token_mask (t) & DATE_TOKEN_NON_NUMERIC) == 0)
481 #define is_weekday(t) ((date_token_mask (t) & DATE_TOKEN_NON_WEEKDAY) == 0)
482 #define is_month(t) ((date_token_mask (t) & DATE_TOKEN_NON_MONTH) == 0)
483 #define is_time(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIME) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_COLON))
484 #define is_tzone_alpha(t) ((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_ALPHA) == 0)
485 #define is_tzone_numeric(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_NUMERIC) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_SIGN))
486 #define is_tzone(t) (is_tzone_alpha (t) || is_tzone_numeric (t))
489 parse_broken_date (date_token *tokens, int *tzone)
491 int got_wday, got_month, got_tzone;
492 int hour, min, sec, offset, n;
497 memset ((void *) &tm, 0, sizeof (struct tm));
498 got_wday = got_month = got_tzone = FALSE;
503 if (is_weekday (token) && !got_wday) {
504 if ((n = get_wday (token->start, token->len)) != -1) {
505 d(printf ("weekday; "));
512 if (is_month (token) && !got_month) {
513 if ((n = get_month (token->start, token->len)) != -1) {
514 d(printf ("month; "));
521 if (is_time (token) && !tm.tm_hour && !tm.tm_min && !tm.tm_sec) {
522 if (get_time (token->start, token->len, &hour, &min, &sec)) {
523 d(printf ("time; "));
531 if (is_tzone (token) && !got_tzone) {
532 date_token *t = token;
534 if ((n = get_tzone (&t)) != -1) {
535 d(printf ("tzone; "));
542 if (is_numeric (token)) {
543 if (token->len == 4 && !tm.tm_year) {
544 if ((n = get_year (token->start, token->len)) != -1) {
545 d(printf ("year; "));
546 tm.tm_year = n - 1900;
550 /* Note: assumes MM-DD-YY ordering if '0 < MM < 12' holds true */
551 if (!got_month && token->next && is_numeric (token->next)) {
552 if ((n = decode_int (token->start, token->len)) > 12) {
560 } else if (!tm.tm_mday && (n = get_mday (token->start, token->len)) != -1) {
562 d(printf ("mday; "));
565 } else if (!tm.tm_year) {
566 if ((n = get_year (token->start, token->len)) != -1) {
567 d(printf ("2-digit year; "));
568 tm.tm_year = n - 1900;
584 t = mktime_utc (&tm);
586 /* t is now GMT of the time we want, but not offset by the timezone ... */
588 /* this should convert the time to the GMT equiv time */
589 t -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
599 gmime_datetok_table_init (void)
603 memset (gmime_datetok_table, 0, sizeof (gmime_datetok_table));
605 for (i = 0; i < 256; i++) {
606 if (!strchr (NUMERIC_CHARS, i))
607 gmime_datetok_table[i] |= DATE_TOKEN_NON_NUMERIC;
609 if (!strchr (WEEKDAY_CHARS, i))
610 gmime_datetok_table[i] |= DATE_TOKEN_NON_WEEKDAY;
612 if (!strchr (MONTH_CHARS, i))
613 gmime_datetok_table[i] |= DATE_TOKEN_NON_MONTH;
615 if (!strchr (TIME_CHARS, i))
616 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIME;
618 if (!strchr (TIMEZONE_ALPHA_CHARS, i))
619 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_ALPHA;
621 if (!strchr (TIMEZONE_NUMERIC_CHARS, i))
622 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_NUMERIC;
624 if (((char) i) == ':')
625 gmime_datetok_table[i] |= DATE_TOKEN_HAS_COLON;
627 if (strchr ("+-", i))
628 gmime_datetok_table[i] |= DATE_TOKEN_HAS_SIGN;
631 printf ("static unsigned char gmime_datetok_table[256] = {");
632 for (i = 0; i < 256; i++) {
635 printf ("%3d,", gmime_datetok_table[i]);
642 notmuch_parse_date (const char *str, int *tz_offset)
644 date_token *token, *tokens;
650 if (!(tokens = datetok (str))) {
657 if (!(date = parse_rfc822_date (tokens, tz_offset)))
658 date = parse_broken_date (tokens, tz_offset);
663 tokens = tokens->next;
664 date_token_free (token);