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) g_slice_free (date_token, tok)
119 #define date_token_new() g_slice_new (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;
367 mktime_utc (struct tm *tm)
375 #if defined (G_OS_WIN32)
377 if (tm->tm_isdst > 0) {
383 #elif defined (HAVE_TM_GMTOFF)
385 #elif defined (HAVE_TIMEZONE)
386 if (tm->tm_isdst > 0) {
387 #if defined (HAVE_ALTZONE)
389 #else /* !defined (HAVE_ALTZONE) */
390 tz = (timezone - 3600);
395 #elif defined (HAVE__TIMEZONE)
398 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
405 parse_rfc822_date (date_token *tokens, int *tzone)
407 int hour, min, sec, offset, n;
417 memset ((void *) &tm, 0, sizeof (struct tm));
419 if ((n = get_wday (token->start, token->len)) != -1) {
420 /* not all dates may have this... */
426 if (!token || (n = get_mday (token->start, token->len)) == -1)
433 if (!token || (n = get_month (token->start, token->len)) == -1)
440 if (!token || (n = get_year (token->start, token->len)) == -1)
443 tm.tm_year = n - 1900;
446 /* get the hour/min/sec */
447 if (!token || !get_time (token->start, token->len, &hour, &min, &sec))
455 /* get the timezone */
456 if (!token || (n = get_tzone (&token)) == -1) {
457 /* I guess we assume tz is GMT? */
463 t = mktime_utc (&tm);
465 /* t is now GMT of the time we want, but not offset by the timezone ... */
467 /* this should convert the time to the GMT equiv time */
468 t -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
477 #define date_token_mask(t) (((date_token *) t)->mask)
478 #define is_numeric(t) ((date_token_mask (t) & DATE_TOKEN_NON_NUMERIC) == 0)
479 #define is_weekday(t) ((date_token_mask (t) & DATE_TOKEN_NON_WEEKDAY) == 0)
480 #define is_month(t) ((date_token_mask (t) & DATE_TOKEN_NON_MONTH) == 0)
481 #define is_time(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIME) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_COLON))
482 #define is_tzone_alpha(t) ((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_ALPHA) == 0)
483 #define is_tzone_numeric(t) (((date_token_mask (t) & DATE_TOKEN_NON_TIMEZONE_NUMERIC) == 0) && (date_token_mask (t) & DATE_TOKEN_HAS_SIGN))
484 #define is_tzone(t) (is_tzone_alpha (t) || is_tzone_numeric (t))
487 parse_broken_date (date_token *tokens, int *tzone)
489 gboolean got_wday, got_month, got_tzone;
490 int hour, min, sec, offset, n;
495 memset ((void *) &tm, 0, sizeof (struct tm));
496 got_wday = got_month = got_tzone = FALSE;
501 if (is_weekday (token) && !got_wday) {
502 if ((n = get_wday (token->start, token->len)) != -1) {
503 d(printf ("weekday; "));
510 if (is_month (token) && !got_month) {
511 if ((n = get_month (token->start, token->len)) != -1) {
512 d(printf ("month; "));
519 if (is_time (token) && !tm.tm_hour && !tm.tm_min && !tm.tm_sec) {
520 if (get_time (token->start, token->len, &hour, &min, &sec)) {
521 d(printf ("time; "));
529 if (is_tzone (token) && !got_tzone) {
530 date_token *t = token;
532 if ((n = get_tzone (&t)) != -1) {
533 d(printf ("tzone; "));
540 if (is_numeric (token)) {
541 if (token->len == 4 && !tm.tm_year) {
542 if ((n = get_year (token->start, token->len)) != -1) {
543 d(printf ("year; "));
544 tm.tm_year = n - 1900;
548 /* Note: assumes MM-DD-YY ordering if '0 < MM < 12' holds true */
549 if (!got_month && token->next && is_numeric (token->next)) {
550 if ((n = decode_int (token->start, token->len)) > 12) {
558 } else if (!tm.tm_mday && (n = get_mday (token->start, token->len)) != -1) {
560 d(printf ("mday; "));
563 } else if (!tm.tm_year) {
564 if ((n = get_year (token->start, token->len)) != -1) {
565 d(printf ("2-digit year; "));
566 tm.tm_year = n - 1900;
582 t = mktime_utc (&tm);
584 /* t is now GMT of the time we want, but not offset by the timezone ... */
586 /* this should convert the time to the GMT equiv time */
587 t -= ((offset / 100) * 60 * 60) + (offset % 100) * 60;
597 gmime_datetok_table_init (void)
601 memset (gmime_datetok_table, 0, sizeof (gmime_datetok_table));
603 for (i = 0; i < 256; i++) {
604 if (!strchr (NUMERIC_CHARS, i))
605 gmime_datetok_table[i] |= DATE_TOKEN_NON_NUMERIC;
607 if (!strchr (WEEKDAY_CHARS, i))
608 gmime_datetok_table[i] |= DATE_TOKEN_NON_WEEKDAY;
610 if (!strchr (MONTH_CHARS, i))
611 gmime_datetok_table[i] |= DATE_TOKEN_NON_MONTH;
613 if (!strchr (TIME_CHARS, i))
614 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIME;
616 if (!strchr (TIMEZONE_ALPHA_CHARS, i))
617 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_ALPHA;
619 if (!strchr (TIMEZONE_NUMERIC_CHARS, i))
620 gmime_datetok_table[i] |= DATE_TOKEN_NON_TIMEZONE_NUMERIC;
622 if (((char) i) == ':')
623 gmime_datetok_table[i] |= DATE_TOKEN_HAS_COLON;
625 if (strchr ("+-", i))
626 gmime_datetok_table[i] |= DATE_TOKEN_HAS_SIGN;
629 printf ("static unsigned char gmime_datetok_table[256] = {");
630 for (i = 0; i < 256; i++) {
633 printf ("%3d,", gmime_datetok_table[i]);
641 * g_mime_utils_header_decode_date:
642 * @str: input date string
643 * @tz_offset: timezone offset
645 * Decodes the rfc822 date string and saves the GMT offset into
646 * @tz_offset if non-NULL.
648 * Returns: the time_t representation of the date string specified by
649 * @str or (time_t) %0 on error. If @tz_offset is non-NULL, the value
650 * of the timezone offset will be stored.
653 g_mime_utils_header_decode_date (const char *str, int *tz_offset)
655 date_token *token, *tokens;
658 if (!(tokens = datetok (str))) {
665 if (!(date = parse_rfc822_date (tokens, tz_offset)))
666 date = parse_broken_date (tokens, tz_offset);
671 tokens = tokens->next;
672 date_token_free (token);