1 Return-Path: <keithp@keithp.com>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 7A3AF431FBC
\r
6 for <notmuch@notmuchmail.org>; Mon, 25 Jan 2010 22:36:44 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-2.641 tagged_above=-999 required=5
\r
12 tests=[AWL=-0.042, BAYES_00=-2.599] autolearn=ham
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id CUL383QKqCZQ for <notmuch@notmuchmail.org>;
\r
16 Mon, 25 Jan 2010 22:36:42 -0800 (PST)
\r
17 Received: from keithp.com (home.keithp.com [63.227.221.253])
\r
18 by olra.theworths.org (Postfix) with ESMTP id 77971431FAE
\r
19 for <notmuch@notmuchmail.org>; Mon, 25 Jan 2010 22:36:41 -0800 (PST)
\r
20 Received: from localhost (localhost [127.0.0.1])
\r
21 by keithp.com (Postfix) with ESMTP id C6F7C760219;
\r
22 Mon, 25 Jan 2010 22:36:40 -0800 (PST)
\r
23 X-Virus-Scanned: Debian amavisd-new at keithp.com
\r
24 Received: from keithp.com ([127.0.0.1])
\r
25 by localhost (keithp.com [127.0.0.1]) (amavisd-new, port 10024)
\r
26 with LMTP id lzHzAfmObWpm; Mon, 25 Jan 2010 22:36:36 -0800 (PST)
\r
27 Received: by keithp.com (Postfix, from userid 1033)
\r
28 id A2011B7C00E; Mon, 25 Jan 2010 22:36:36 -0800 (PST)
\r
29 Received: from koto.keithp.com (localhost [127.0.0.1])
\r
30 by keithp.com (Postfix) with ESMTP id 997CD760196;
\r
31 Mon, 25 Jan 2010 22:36:36 -0800 (PST)
\r
32 Received: by koto.keithp.com (Postfix, from userid 1488)
\r
33 id 5A75C11C066; Tue, 26 Jan 2010 19:36:36 +1300 (NZDT)
\r
34 From: Keith Packard <keithp@keithp.com>
\r
35 To: Sebastian Spaeth <Sebastian@SSpaeth.de>, notmuch@notmuchmail.org
\r
36 In-Reply-To: <1264173971-11879-1-git-send-email-Sebastian@SSpaeth.de>
\r
37 References: <1264173971-11879-1-git-send-email-Sebastian@SSpaeth.de>
\r
38 Date: Mon, 25 Jan 2010 22:36:35 -0800
\r
39 Message-ID: <yunpr4xnzik.fsf@aiko.keithp.com>
\r
41 Content-Type: multipart/signed; boundary="=-=-=";
\r
42 micalg=pgp-sha1; protocol="application/pgp-signature"
\r
43 Subject: Re: [notmuch] [PATCH] Make the date parser nicer
\r
44 X-BeenThere: notmuch@notmuchmail.org
\r
45 X-Mailman-Version: 2.1.13
\r
47 List-Id: "Use and development of the notmuch mail system."
\r
48 <notmuch.notmuchmail.org>
\r
49 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
50 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
51 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
52 List-Post: <mailto:notmuch@notmuchmail.org>
\r
53 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
54 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
55 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
56 X-List-Received-Date: Tue, 26 Jan 2010 06:36:44 -0000
\r
59 Content-Type: text/plain; charset=utf-8
\r
60 Content-Transfer-Encoding: quoted-printable
\r
62 On Fri, 22 Jan 2010 16:26:11 +0100, Sebastian Spaeth <Sebastian@SSpaeth.de>=
\r
64 > Currently we have to enter mail dates as timestamps. This approach
\r
65 > does 2 things: it requires the prefix 'date:' and it allows timestamps
\r
66 > to be specified as YYYY, YYYYMM or YYYYMMDD. So a notmuch show
\r
67 > date:2005..20060512 will find all mails from 2005-01-01 until
\r
68 > 2006-05-12. The code is probably not in a proper location yet and
\r
69 > needs to be shoved around by someone more knowledgable than me. My C++
\r
70 > skills are somewhat,... lacking...
\r
72 Here's some code which further improves date parsing by allowing lots of
\r
73 date formats, including things like "today", "thisweek", ISO and US date
\r
74 formats and month names. You can separate two dates with .. to make a
\r
75 range, or you can just use the default range ("lastmonth" is everything
\r
76 From=20the 1st of the previous month to the 1st of the current month).
\r
78 I think this fits nicely with your code.
\r
80 From=20432b210a6218a809ebcddbb0e5b94a1ddbd34207 Mon Sep 17 00:00:00 2001
\r
81 From: Keith Packard <keithp@keithp.com>
\r
82 Date: Mon, 25 Jan 2010 22:35:30 -0800
\r
83 Subject: [PATCH] Add date.c
\r
86 lib/date.c | 455 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
\r
88 1 files changed, 455 insertions(+), 0 deletions(-)
\r
89 create mode 100644 lib/date.c
\r
91 diff --git a/lib/date.c b/lib/date.c
\r
92 new file mode 100644
\r
93 index 0000000..611ae15
\r
98 + * Copyright =C2=A9 2009 Keith Packard <keithp@keithp.com>
\r
100 + * This program is free software; you can redistribute it and/or modify
\r
101 + * it under the terms of the GNU General Public License as published by
\r
102 + * the Free Software Foundation, either version 3 of the License, or
\r
103 + * (at your option) any later version.
\r
105 + * This program is distributed in the hope that it will be useful, but
\r
106 + * WITHOUT ANY WARRANTY; without even the implied warranty of
\r
107 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
108 + * General Public License for more details.
\r
110 + * You should have received a copy of the GNU General Public License along
\r
111 + * with this program; if not, write to the Free Software Foundation, Inc.,
\r
112 + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
115 +#include "notmuch.h"
\r
117 +#include <stdio.h>
\r
118 +#include <string.h>
\r
119 +#include <stdlib.h>
\r
121 +#define DAY (24 * 60 * 60)
\r
124 +today(struct tm *result, time_t after) {
\r
131 + localtime_r(&t, result);
\r
132 + result->tm_sec =3D result->tm_min =3D result->tm_hour =3D 0;
\r
135 +static int parse_today(const char *text, time_t *first, time_t *last, time=
\r
137 + if (strcasecmp(text, "today") =3D=3D 0) {
\r
140 + *first =3D mktime(&n);
\r
141 + *last =3D *first + DAY;
\r
147 +static int parse_yesterday(const char *text, time_t *first, time_t *last, =
\r
149 + if (strcasecmp(text, "yesterday") =3D=3D 0) {
\r
152 + *last =3D mktime(&n);
\r
153 + *first =3D *last - DAY;
\r
159 +static int parse_thisweek(const char *text, time_t *first, time_t *last, t=
\r
161 + if (strcasecmp(text, "thisweek") =3D=3D 0) {
\r
164 + *first =3D mktime(&n) - (n.tm_wday * DAY);
\r
165 + *last =3D *first + DAY * 7;
\r
171 +static int parse_lastweek(const char *text, time_t *first, time_t *last, t=
\r
173 + if (strcasecmp(text, "lastweek") =3D=3D 0) {
\r
176 + *last =3D mktime(&n) - (n.tm_wday * DAY);
\r
177 + *first =3D *last - DAY * 7;
\r
183 +static int parse_thismonth(const char *text, time_t *first, time_t *last, =
\r
185 + if (strcasecmp(text, "thismonth") =3D=3D 0) {
\r
189 + *first =3D mktime(&n);
\r
190 + if (n.tm_mon++ =3D=3D 12) {
\r
194 + *last =3D mktime(&n);
\r
200 +static int parse_lastmonth(const char *text, time_t *first, time_t *last, =
\r
202 + if (strcasecmp(text, "lastmonth") =3D=3D 0) {
\r
206 + if (n.tm_mon =3D=3D 0) {
\r
211 + *first =3D mktime(&n);
\r
212 + if (n.tm_mon++ =3D=3D 12) {
\r
216 + *last =3D mktime(&n);
\r
222 +static const char *months[12][2] =3D {
\r
223 + { "January", "Jan" },
\r
224 + { "February", "Feb" },
\r
225 + { "March", "Mar" },
\r
226 + { "April", "Apr" },
\r
227 + { "May", "May" },
\r
228 + { "June", "Jun" },
\r
229 + { "July", "Jul" },
\r
230 + { "August", "Aug" },
\r
231 + { "September", "Sep" },
\r
232 + { "October", "Oct" },
\r
233 + { "November", "Nov" },
\r
234 + { "December", "Dec" },
\r
237 +static int year(const char *text, int *y) {
\r
239 + *y =3D strtol(text, &end, 10);
\r
240 + if (end =3D=3D text)
\r
242 + if (*end !=3D '\0')
\r
244 + if (*y < 1970 || *y > 2038)
\r
250 +static int month(const char *text, int *m) {
\r
253 + for (i =3D 0; i < 12; i++) {
\r
254 + if (strcasecmp(text, months[i][0]) =3D=3D 0 ||
\r
255 + strcasecmp(text, months[i][1]) =3D=3D 0)
\r
261 + *m =3D strtol(text, &end, 10);
\r
262 + if (end =3D=3D text)
\r
264 + if (*end !=3D '\0')
\r
266 + if (*m < 1 || *m > 12)
\r
272 +static int day(const char *text, int *d) {
\r
274 + *d =3D strtol(text, &end, 10);
\r
275 + if (end =3D=3D text)
\r
277 + if (*end !=3D '\0')
\r
279 + if (*d < 1 || *d > 31)
\r
285 +static int parse_month(const char *text, time_t *first, time_t *last, time=
\r
287 + int m =3D 0, d =3D 0;
\r
295 + if(strlen (text) >=3D sizeof (tmp))
\r
297 + strcpy(tmp, text);
\r
302 + while ((token =3D strtok_r(t, "-", &save)) !=3D NULL) {
\r
306 + if (month(token, &m) !=3D 0)
\r
310 + if (day(token, &d) !=3D 0)
\r
318 + today(&n, after);
\r
320 + if (m < n.tm_mon)
\r
323 + if (m > n.tm_mon)
\r
330 + *first =3D mktime(&n);
\r
331 + if (++n.tm_mon > 11) {
\r
335 + *last =3D mktime(&n);
\r
340 + *first =3D mktime(&n);
\r
341 + *last =3D *first + DAY;
\r
347 +/* year[-month[-day]] */
\r
348 +static int parse_iso(const char *text, time_t *first, time_t *last, time_t=
\r
350 + int y =3D 0, m =3D 0, d =3D 0;
\r
358 + if(strlen (text) >=3D sizeof (tmp))
\r
360 + strcpy(tmp, text);
\r
365 + while ((token =3D strtok_r(t, "-", &save)) !=3D NULL) {
\r
369 + if (year(token, &y) !=3D 0)
\r
373 + if (month(token, &m) !=3D 0)
\r
377 + if (day(token, &d) !=3D 0)
\r
391 + *first =3D mktime(&n);
\r
392 + n.tm_year =3D y + 1;
\r
393 + *last =3D mktime(&n);
\r
399 + *first =3D mktime(&n);
\r
400 + if (++n.tm_mon > 11) {
\r
404 + *last =3D mktime(&n);
\r
410 + *first =3D mktime(&n);
\r
411 + *last =3D *first + DAY;
\r
417 +/* month[/day[/year]] */
\r
418 +static int parse_us(const char *text, time_t *first, time_t *last, time_t =
\r
420 + int y =3D 0, m =3D 0, d =3D 0;
\r
428 + if(strlen (text) >=3D sizeof (tmp))
\r
430 + strcpy(tmp, text);
\r
435 + while ((token =3D strtok_r(t, "/", &save)) !=3D NULL) {
\r
439 + if (month(token, &m) !=3D 0)
\r
443 + if (day(token, &d) !=3D 0)
\r
447 + if (year(token, &y) !=3D 0)
\r
455 + today(&n, after);
\r
457 + if (m < n.tm_mon)
\r
460 + if (m > n.tm_mon)
\r
467 + *first =3D mktime(&n);
\r
468 + if (++n.tm_mon > 11) {
\r
472 + *last =3D mktime(&n);
\r
477 + *first =3D mktime(&n);
\r
478 + *last =3D *first + DAY;
\r
484 + *first =3D mktime(&n);
\r
485 + *last =3D *first + DAY;
\r
491 +static int (*parsers[])(const char *text, time_t *first, time_t *last, tim=
\r
505 +static notmuch_status_t
\r
506 +notmuch_one_date(const char *text, time_t *first, time_t *last, time_t aft=
\r
510 + for (i =3D 0; parsers[i]; i++)
\r
511 + if (parsers[i](text, first, last, after) =3D=3D 0)
\r
512 + return NOTMUCH_STATUS_SUCCESS;
\r
513 + return NOTMUCH_STATUS_INVALID_DATE;
\r
517 +notmuch_date(const char *text, time_t *first, time_t *last)
\r
520 + char first_text[80], last_text[80];
\r
521 + notmuch_status_t status;
\r
522 + time_t first_first, first_last, last_first, last_last;
\r
524 + if (strlen(text) > sizeof (first_text))
\r
525 + return NOTMUCH_STATUS_INVALID_DATE;
\r
526 + dots =3D strstr(text, "..");
\r
528 + strncpy(first_text, text, dots - text);
\r
529 + first_text[dots-text] =3D '\0';
\r
530 + status =3D notmuch_one_date(first_text, &first_first, &first_last, 0);
\r
533 + status =3D notmuch_one_date(dots + 2, &last_first, &last_last, first_firs=
\r
537 + *first =3D first_first;
\r
538 + *last =3D last_last;
\r
541 + return notmuch_one_date(text, first, last, 0);
\r
546 +main (int argc, char **argv)
\r
549 + for (i =3D 1; i < argc; i++) {
\r
550 + time_t first, last;
\r
552 + if (notmuch_date(argv[i], &first, &last) =3D=3D 0) {
\r
553 + char first_string[80], last_string[80];
\r
555 + ctime_r(&first, first_string);
\r
556 + first_string[strlen(first_string)-1] =3D '\0';
\r
557 + ctime_r(&last, last_string);
\r
558 + last_string[strlen(last_string)-1] =3D '\0';
\r
559 + printf ("%s: %s - %s\n", argv[i], first_string, last_string);
\r
569 keith.packard@intel.com
\r
572 Content-Type: application/pgp-signature
\r
574 -----BEGIN PGP SIGNATURE-----
\r
575 Version: GnuPG v1.4.10 (GNU/Linux)
\r
577 iD8DBQFLXo10Qp8BWwlsTdMRAi6mAJ0UR0x6XbkL0/OcFPJkFiIvSMFcdgCfWPOq
\r
578 paA/CX4bNnD1Ykz0rug5gGE=
\r
580 -----END PGP SIGNATURE-----
\r