Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 7A3AF431FBC for ; Mon, 25 Jan 2010 22:36:44 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.641 X-Spam-Level: X-Spam-Status: No, score=-2.641 tagged_above=-999 required=5 tests=[AWL=-0.042, BAYES_00=-2.599] autolearn=ham Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CUL383QKqCZQ for ; Mon, 25 Jan 2010 22:36:42 -0800 (PST) Received: from keithp.com (home.keithp.com [63.227.221.253]) by olra.theworths.org (Postfix) with ESMTP id 77971431FAE for ; Mon, 25 Jan 2010 22:36:41 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by keithp.com (Postfix) with ESMTP id C6F7C760219; Mon, 25 Jan 2010 22:36:40 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at keithp.com Received: from keithp.com ([127.0.0.1]) by localhost (keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id lzHzAfmObWpm; Mon, 25 Jan 2010 22:36:36 -0800 (PST) Received: by keithp.com (Postfix, from userid 1033) id A2011B7C00E; Mon, 25 Jan 2010 22:36:36 -0800 (PST) Received: from koto.keithp.com (localhost [127.0.0.1]) by keithp.com (Postfix) with ESMTP id 997CD760196; Mon, 25 Jan 2010 22:36:36 -0800 (PST) Received: by koto.keithp.com (Postfix, from userid 1488) id 5A75C11C066; Tue, 26 Jan 2010 19:36:36 +1300 (NZDT) From: Keith Packard To: Sebastian Spaeth , notmuch@notmuchmail.org In-Reply-To: <1264173971-11879-1-git-send-email-Sebastian@SSpaeth.de> References: <1264173971-11879-1-git-send-email-Sebastian@SSpaeth.de> Date: Mon, 25 Jan 2010 22:36:35 -0800 Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha1; protocol="application/pgp-signature" Subject: Re: [notmuch] [PATCH] Make the date parser nicer X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Jan 2010 06:36:44 -0000 --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Fri, 22 Jan 2010 16:26:11 +0100, Sebastian Spaeth = wrote: > Currently we have to enter mail dates as timestamps. This approach > does 2 things: it requires the prefix 'date:' and it allows timestamps > to be specified as YYYY, YYYYMM or YYYYMMDD. So a notmuch show > date:2005..20060512 will find all mails from 2005-01-01 until > 2006-05-12. The code is probably not in a proper location yet and > needs to be shoved around by someone more knowledgable than me. My C++ > skills are somewhat,... lacking... Here's some code which further improves date parsing by allowing lots of date formats, including things like "today", "thisweek", ISO and US date formats and month names. You can separate two dates with .. to make a range, or you can just use the default range ("lastmonth" is everything From=20the 1st of the previous month to the 1st of the current month). I think this fits nicely with your code. From=20432b210a6218a809ebcddbb0e5b94a1ddbd34207 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 25 Jan 2010 22:35:30 -0800 Subject: [PATCH] Add date.c =2D-- lib/date.c | 455 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 files changed, 455 insertions(+), 0 deletions(-) create mode 100644 lib/date.c diff --git a/lib/date.c b/lib/date.c new file mode 100644 index 0000000..611ae15 =2D-- /dev/null +++ b/lib/date.c @@ -0,0 +1,455 @@ +/* + * Copyright =C2=A9 2009 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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. + */ + +#include "notmuch.h" +#include +#include +#include +#include + +#define DAY (24 * 60 * 60) + +static void +today(struct tm *result, time_t after) { + time_t t; + + if (after) + t =3D after; + else + time(&t); + localtime_r(&t, result); + result->tm_sec =3D result->tm_min =3D result->tm_hour =3D 0; +} + +static int parse_today(const char *text, time_t *first, time_t *last, time= _t after) { + if (strcasecmp(text, "today") =3D=3D 0) { + struct tm n; + today(&n, 0); + *first =3D mktime(&n); + *last =3D *first + DAY; + return 0; + } + return 1; +} + +static int parse_yesterday(const char *text, time_t *first, time_t *last, = time_t after) { + if (strcasecmp(text, "yesterday") =3D=3D 0) { + struct tm n; + today(&n, 0); + *last =3D mktime(&n); + *first =3D *last - DAY; + return 0; + } + return 1; +} + +static int parse_thisweek(const char *text, time_t *first, time_t *last, t= ime_t after) { + if (strcasecmp(text, "thisweek") =3D=3D 0) { + struct tm n; + today(&n, 0); + *first =3D mktime(&n) - (n.tm_wday * DAY); + *last =3D *first + DAY * 7; + return 0; + } + return 1; +} + +static int parse_lastweek(const char *text, time_t *first, time_t *last, t= ime_t after) { + if (strcasecmp(text, "lastweek") =3D=3D 0) { + struct tm n; + today(&n, 0); + *last =3D mktime(&n) - (n.tm_wday * DAY); + *first =3D *last - DAY * 7; + return 0; + } + return 1; +} + +static int parse_thismonth(const char *text, time_t *first, time_t *last, = time_t after) { + if (strcasecmp(text, "thismonth") =3D=3D 0) { + struct tm n; + today(&n, 0); + n.tm_mday =3D 1; + *first =3D mktime(&n); + if (n.tm_mon++ =3D=3D 12) { + n.tm_mon =3D 0; + n.tm_year++; + } + *last =3D mktime(&n); + return 0; + } + return 1; +} + +static int parse_lastmonth(const char *text, time_t *first, time_t *last, = time_t after) { + if (strcasecmp(text, "lastmonth") =3D=3D 0) { + struct tm n; + today(&n, 0); + n.tm_mday =3D 1; + if (n.tm_mon =3D=3D 0) { + n.tm_year--; + n.tm_mon =3D 11; + } else + n.tm_mon--; + *first =3D mktime(&n); + if (n.tm_mon++ =3D=3D 12) { + n.tm_mon =3D 0; + n.tm_year++; + } + *last =3D mktime(&n); + return 0; + } + return 1; +} + +static const char *months[12][2] =3D { + { "January", "Jan" }, + { "February", "Feb" }, + { "March", "Mar" }, + { "April", "Apr" }, + { "May", "May" }, + { "June", "Jun" }, + { "July", "Jul" }, + { "August", "Aug" }, + { "September", "Sep" }, + { "October", "Oct" }, + { "November", "Nov" }, + { "December", "Dec" }, +}; + +static int year(const char *text, int *y) { + char *end; + *y =3D strtol(text, &end, 10); + if (end =3D=3D text) + return 1; + if (*end !=3D '\0') + return 1; + if (*y < 1970 || *y > 2038) + return 1; + *y -=3D 1900; + return 0; +} + +static int month(const char *text, int *m) { + char *end; + int i; + for (i =3D 0; i < 12; i++) { + if (strcasecmp(text, months[i][0]) =3D=3D 0 || + strcasecmp(text, months[i][1]) =3D=3D 0) + { + *m =3D i; + return 0; + } + } + *m =3D strtol(text, &end, 10); + if (end =3D=3D text) + return 1; + if (*end !=3D '\0') + return 1; + if (*m < 1 || *m > 12) + return 1; + *m -=3D 1; + return 0; +} + +static int day(const char *text, int *d) { + char *end; + *d =3D strtol(text, &end, 10); + if (end =3D=3D text) + return 1; + if (*end !=3D '\0') + return 1; + if (*d < 1 || *d > 31) + return 1; + return 0; +} + +/* month[-day] */ +static int parse_month(const char *text, time_t *first, time_t *last, time= _t after) { + int m =3D 0, d =3D 0; + int i; + struct tm n; + char tmp[80]; + char *t; + char *save; + char *token; + + if(strlen (text) >=3D sizeof (tmp)) + return 1; + strcpy(tmp, text); +=20=20=20=20 + t =3D tmp; + save =3D NULL; + i =3D 0; + while ((token =3D strtok_r(t, "-", &save)) !=3D NULL) { + i++; + switch(i) { + case 1: + if (month(token, &m) !=3D 0) + return 1; + break; + case 2: + if (day(token, &d) !=3D 0) + return 1; + break; + default: + return 1; + } + t =3D NULL; + } + today(&n, after); + if (after) { + if (m < n.tm_mon) + n.tm_year++; + } else { + if (m > n.tm_mon) + n.tm_year--; + } + switch (i) { + case 1: + n.tm_mday =3D 1; + n.tm_mon =3D m; + *first =3D mktime(&n); + if (++n.tm_mon > 11) { + n.tm_mon =3D 0; + n.tm_year++; + } + *last =3D mktime(&n); + return 0; + case 2: + n.tm_mday =3D d; + n.tm_mon =3D m; + *first =3D mktime(&n); + *last =3D *first + DAY; + return 0; + } + return 1; +} + +/* year[-month[-day]] */ +static int parse_iso(const char *text, time_t *first, time_t *last, time_t= after) { + int y =3D 0, m =3D 0, d =3D 0; + int i; + struct tm n; + char tmp[80]; + char *t; + char *save; + char *token; + + if(strlen (text) >=3D sizeof (tmp)) + return 1; + strcpy(tmp, text); +=20=20=20=20 + t =3D tmp; + save =3D NULL; + i =3D 0; + while ((token =3D strtok_r(t, "-", &save)) !=3D NULL) { + i++; + switch(i) { + case 1: + if (year(token, &y) !=3D 0) + return 1; + break; + case 2: + if (month(token, &m) !=3D 0) + return 1; + break; + case 3: + if (day(token, &d) !=3D 0) + return 1; + break; + default: + return 1; + } + t =3D NULL; + } + today(&n, 0); + switch (i) { + case 1: + n.tm_mday =3D 1; + n.tm_mon =3D 0; + n.tm_year =3D y; + *first =3D mktime(&n); + n.tm_year =3D y + 1; + *last =3D mktime(&n); + return 0; + case 2: + n.tm_mday =3D 1; + n.tm_mon =3D m; + n.tm_year =3D y; + *first =3D mktime(&n); + if (++n.tm_mon > 11) { + n.tm_mon =3D 0; + n.tm_year++; + } + *last =3D mktime(&n); + return 0; + case 3: + n.tm_mday =3D d; + n.tm_mon =3D m; + n.tm_year =3D y; + *first =3D mktime(&n); + *last =3D *first + DAY; + return 0; + } + return 1; +} + +/* month[/day[/year]] */ +static int parse_us(const char *text, time_t *first, time_t *last, time_t = after) { + int y =3D 0, m =3D 0, d =3D 0; + int i; + struct tm n; + char tmp[80]; + char *t; + char *save; + char *token; + + if(strlen (text) >=3D sizeof (tmp)) + return 1; + strcpy(tmp, text); +=20=20=20=20 + t =3D tmp; + save =3D NULL; + i =3D 0; + while ((token =3D strtok_r(t, "/", &save)) !=3D NULL) { + i++; + switch(i) { + case 1: + if (month(token, &m) !=3D 0) + return 1; + break; + case 2: + if (day(token, &d) !=3D 0) + return 1; + break; + case 3: + if (year(token, &y) !=3D 0) + return 1; + break; + default: + return 1; + } + t =3D NULL; + } + today(&n, after); + if (after) { + if (m < n.tm_mon) + n.tm_year++; + } else { + if (m > n.tm_mon) + n.tm_year--; + } + switch (i) { + case 1: + n.tm_mday =3D 1; + n.tm_mon =3D m; + *first =3D mktime(&n); + if (++n.tm_mon > 11) { + n.tm_mon =3D 0; + n.tm_year++; + } + *last =3D mktime(&n); + return 0; + case 2: + n.tm_mday =3D d; + n.tm_mon =3D m; + *first =3D mktime(&n); + *last =3D *first + DAY; + return 0; + case 3: + n.tm_mday =3D d; + n.tm_mon =3D m; + n.tm_year =3D y; + *first =3D mktime(&n); + *last =3D *first + DAY; + return 0; + } + return 1; +} + +static int (*parsers[])(const char *text, time_t *first, time_t *last, tim= e_t after) =3D { + parse_today, + parse_yesterday, + parse_thisweek, + parse_lastweek, + parse_thismonth, + parse_lastmonth, + parse_month, + parse_iso, + parse_us, + 0, +}; + +static notmuch_status_t +notmuch_one_date(const char *text, time_t *first, time_t *last, time_t aft= er) +{ + int i; + for (i =3D 0; parsers[i]; i++) + if (parsers[i](text, first, last, after) =3D=3D 0) + return NOTMUCH_STATUS_SUCCESS; + return NOTMUCH_STATUS_INVALID_DATE; +} + +notmuch_status_t +notmuch_date(const char *text, time_t *first, time_t *last) +{ + char *dots; + char first_text[80], last_text[80]; + notmuch_status_t status; + time_t first_first, first_last, last_first, last_last; + + if (strlen(text) > sizeof (first_text)) + return NOTMUCH_STATUS_INVALID_DATE; + dots =3D strstr(text, ".."); + if (dots) { + strncpy(first_text, text, dots - text); + first_text[dots-text] =3D '\0'; + status =3D notmuch_one_date(first_text, &first_first, &first_last, 0); + if (status) + return status; + status =3D notmuch_one_date(dots + 2, &last_first, &last_last, first_firs= t); + if (status) + return status; + *first =3D first_first; + *last =3D last_last; + return 0; + } + return notmuch_one_date(text, first, last, 0); +} + +#if 1 +int +main (int argc, char **argv) +{ + int i; + for (i =3D 1; i < argc; i++) { + time_t first, last; + + if (notmuch_date(argv[i], &first, &last) =3D=3D 0) { + char first_string[80], last_string[80]; + + ctime_r(&first, first_string); + first_string[strlen(first_string)-1] =3D '\0'; + ctime_r(&last, last_string); + last_string[strlen(last_string)-1] =3D '\0'; + printf ("%s: %s - %s\n", argv[i], first_string, last_string); + } + } +} +#endif =2D-=20 1.6.6 =2D-=20 keith.packard@intel.com --=-=-= Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iD8DBQFLXo10Qp8BWwlsTdMRAi6mAJ0UR0x6XbkL0/OcFPJkFiIvSMFcdgCfWPOq paA/CX4bNnD1Ykz0rug5gGE= =bLHT -----END PGP SIGNATURE----- --=-=-=--