Re: [PATCH v4 10/16] Add n_d_add_message_with_indexopts (extension of n_d_add_message)
[notmuch-archives.git] / bf / 711d60794f2b331acad793d66f985c8b4052fb
1 Return-Path: <Sebastian@SSpaeth.de>\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 3E1A6431FC0\r
6         for <notmuch@notmuchmail.org>; Tue, 26 Jan 2010 03:44:16 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.773\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.773 tagged_above=-999 required=5 tests=[AWL=0.826,\r
12         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 ve+xoTQJ7Vbh for <notmuch@notmuchmail.org>;\r
16         Tue, 26 Jan 2010 03:44:15 -0800 (PST)\r
17 Received: from homiemail-a20.g.dreamhost.com (caiajhbdcbef.dreamhost.com\r
18         [208.97.132.145])\r
19         by olra.theworths.org (Postfix) with ESMTP id 1213B431FBD\r
20         for <notmuch@notmuchmail.org>; Tue, 26 Jan 2010 03:44:15 -0800 (PST)\r
21 Received: from localhost.localdomain (mtec-hg-docking-2-dhcp-062.ethz.ch\r
22         [82.130.121.62])\r
23         by homiemail-a20.g.dreamhost.com (Postfix) with ESMTPA id D50987EC061; \r
24         Tue, 26 Jan 2010 03:44:13 -0800 (PST)\r
25 From: Sebastian Spaeth <Sebastian@SSpaeth.de>\r
26 To: notmuch@notmuchmail.org\r
27 Date: Tue, 26 Jan 2010 12:43:39 +0100\r
28 Message-Id: <1264506221-9636-2-git-send-email-Sebastian@SSpaeth.de>\r
29 X-Mailer: git-send-email 1.6.3.3\r
30 In-Reply-To: <1264506221-9636-1-git-send-email-Sebastian@SSpaeth.de>\r
31 References: <1264506221-9636-1-git-send-email-Sebastian@SSpaeth.de>\r
32 Subject: [notmuch] [PATCH 2/4] add date parser file from Keith\r
33 X-BeenThere: notmuch@notmuchmail.org\r
34 X-Mailman-Version: 2.1.13\r
35 Precedence: list\r
36 List-Id: "Use and development of the notmuch mail system."\r
37         <notmuch.notmuchmail.org>\r
38 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
39         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
40 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
41 List-Post: <mailto:notmuch@notmuchmail.org>\r
42 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
43 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
45 X-List-Received-Date: Tue, 26 Jan 2010 11:44:16 -0000\r
46 \r
47 From: Keith Packard <keithp@keithp.com>\r
48 \r
49 Here's some code which further improves date parsing by allowing lots of\r
50 date formats, including things like "today", "thisweek", ISO and US date\r
51 formats and month names. You can separate two dates with .. to make a\r
52 range, or you can just use the default range ("lastmonth" is everything\r
53 >From the 1st of the previous month to the 1st of the current month).\r
54 ---\r
55  lib/date.c |  455 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
56  1 files changed, 455 insertions(+), 0 deletions(-)\r
57  create mode 100644 lib/date.c\r
58 \r
59 diff --git a/lib/date.c b/lib/date.c\r
60 new file mode 100644\r
61 index 0000000..09c5ef9\r
62 --- /dev/null\r
63 +++ b/lib/date.c\r
64 @@ -0,0 +1,455 @@\r
65 +/*\r
66 + * Copyright © 2009 Keith Packard <keithp@keithp.com>\r
67 + *\r
68 + * This program is free software; you can redistribute it and/or modify\r
69 + * it under the terms of the GNU General Public License as published by\r
70 + * the Free Software Foundation, either version 3 of the License, or\r
71 + * (at your option) any later version.\r
72 + *\r
73 + * This program is distributed in the hope that it will be useful, but\r
74 + * WITHOUT ANY WARRANTY; without even the implied warranty of\r
75 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
76 + * General Public License for more details.\r
77 + *\r
78 + * You should have received a copy of the GNU General Public License along\r
79 + * with this program; if not, write to the Free Software Foundation, Inc.,\r
80 + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
81 + */\r
82 +\r
83 +#include "notmuch.h"\r
84 +#include <time.h>\r
85 +#include <stdio.h>\r
86 +#include <string.h>\r
87 +#include <stdlib.h>\r
88 +\r
89 +#define DAY    (24 * 60 * 60)\r
90 +\r
91 +static void\r
92 +today(struct tm *result, time_t after) {\r
93 +    time_t     t;\r
94 +\r
95 +    if (after)\r
96 +       t = after;\r
97 +    else\r
98 +       time(&t);\r
99 +    localtime_r(&t, result);\r
100 +    result->tm_sec = result->tm_min = result->tm_hour = 0;\r
101 +}\r
102 +\r
103 +static int parse_today(const char *text, time_t *first, time_t *last, time_t after) {\r
104 +    if (strcasecmp(text, "today") == 0) {\r
105 +       struct tm n;\r
106 +       today(&n, 0);\r
107 +       *first = mktime(&n);\r
108 +       *last = *first + DAY;\r
109 +       return 0;\r
110 +    }\r
111 +    return 1;\r
112 +}\r
113 +\r
114 +static int parse_yesterday(const char *text, time_t *first, time_t *last, time_t after) {\r
115 +    if (strcasecmp(text, "yesterday") == 0) {\r
116 +       struct tm n;\r
117 +       today(&n, 0);\r
118 +       *last = mktime(&n);\r
119 +       *first = *last - DAY;\r
120 +       return 0;\r
121 +    }\r
122 +    return 1;\r
123 +}\r
124 +\r
125 +static int parse_thisweek(const char *text, time_t *first, time_t *last, time_t after) {\r
126 +    if (strcasecmp(text, "thisweek") == 0) {\r
127 +       struct tm n;\r
128 +       today(&n, 0);\r
129 +       *first = mktime(&n) - (n.tm_wday * DAY);\r
130 +       *last = *first + DAY * 7;\r
131 +       return 0;\r
132 +    }\r
133 +    return 1;\r
134 +}\r
135 +\r
136 +static int parse_lastweek(const char *text, time_t *first, time_t *last, time_t after) {\r
137 +    if (strcasecmp(text, "lastweek") == 0) {\r
138 +       struct tm n;\r
139 +       today(&n, 0);\r
140 +       *last = mktime(&n) - (n.tm_wday * DAY);\r
141 +       *first = *last - DAY * 7;\r
142 +       return 0;\r
143 +    }\r
144 +    return 1;\r
145 +}\r
146 +\r
147 +static int parse_thismonth(const char *text, time_t *first, time_t *last, time_t after) {\r
148 +    if (strcasecmp(text, "thismonth") == 0) {\r
149 +       struct tm n;\r
150 +       today(&n, 0);\r
151 +       n.tm_mday = 1;\r
152 +       *first = mktime(&n);\r
153 +       if (n.tm_mon++ == 12) {\r
154 +           n.tm_mon = 0;\r
155 +           n.tm_year++;\r
156 +       }\r
157 +       *last = mktime(&n);\r
158 +       return 0;\r
159 +    }\r
160 +    return 1;\r
161 +}\r
162 +\r
163 +static int parse_lastmonth(const char *text, time_t *first, time_t *last, time_t after) {\r
164 +    if (strcasecmp(text, "lastmonth") == 0) {\r
165 +       struct tm n;\r
166 +       today(&n, 0);\r
167 +       n.tm_mday = 1;\r
168 +       if (n.tm_mon == 0) {\r
169 +           n.tm_year--;\r
170 +           n.tm_mon = 11;\r
171 +       } else\r
172 +           n.tm_mon--;\r
173 +       *first = mktime(&n);\r
174 +       if (n.tm_mon++ == 12) {\r
175 +           n.tm_mon = 0;\r
176 +           n.tm_year++;\r
177 +       }\r
178 +       *last = mktime(&n);\r
179 +       return 0;\r
180 +    }\r
181 +    return 1;\r
182 +}\r
183 +\r
184 +static const char *months[12][2] = {\r
185 +    { "January", "Jan" },\r
186 +    { "February", "Feb" },\r
187 +    { "March", "Mar" },\r
188 +    { "April", "Apr" },\r
189 +    { "May", "May" },\r
190 +    { "June", "Jun" },\r
191 +    { "July", "Jul" },\r
192 +    { "August", "Aug" },\r
193 +    { "September", "Sep" },\r
194 +    { "October", "Oct" },\r
195 +    { "November", "Nov" },\r
196 +    { "December", "Dec" },\r
197 +};\r
198 +\r
199 +static int year(const char *text, int *y) {\r
200 +    char *end;\r
201 +    *y = strtol(text, &end, 10);\r
202 +    if (end == text)\r
203 +       return 1;\r
204 +    if (*end != '\0')\r
205 +       return 1;\r
206 +    if (*y < 1970 || *y > 2038)\r
207 +       return 1;\r
208 +    *y -= 1900;\r
209 +    return 0;\r
210 +}\r
211 +\r
212 +static int month(const char *text, int *m) {\r
213 +    char *end;\r
214 +    int i;\r
215 +    for (i = 0; i < 12; i++) {\r
216 +       if (strcasecmp(text, months[i][0]) == 0 ||\r
217 +           strcasecmp(text, months[i][1]) == 0)\r
218 +       {\r
219 +           *m = i;\r
220 +           return 0;\r
221 +       }\r
222 +    }\r
223 +    *m = strtol(text, &end, 10);\r
224 +    if (end == text)\r
225 +       return 1;\r
226 +    if (*end != '\0')\r
227 +       return 1;\r
228 +    if (*m < 1 || *m > 12)\r
229 +       return 1;\r
230 +    *m -= 1;\r
231 +    return 0;\r
232 +}\r
233 +\r
234 +static int day(const char *text, int *d) {\r
235 +    char *end;\r
236 +    *d = strtol(text, &end, 10);\r
237 +    if (end == text)\r
238 +       return 1;\r
239 +    if (*end != '\0')\r
240 +       return 1;\r
241 +    if (*d < 1 || *d > 31)\r
242 +       return 1;\r
243 +    return 0;\r
244 +}\r
245 +\r
246 +/* month[-day] */\r
247 +static int parse_month(const char *text, time_t *first, time_t *last, time_t after) {\r
248 +    int                m = 0, d = 0;\r
249 +    int                i;\r
250 +    struct tm  n;\r
251 +    char       tmp[80];\r
252 +    char       *t;\r
253 +    char       *save;\r
254 +    char       *token;\r
255 +\r
256 +    if(strlen (text) >= sizeof (tmp))\r
257 +       return 1;\r
258 +    strcpy(tmp, text);\r
259 +\r
260 +    t = tmp;\r
261 +    save = NULL;\r
262 +    i = 0;\r
263 +    while ((token = strtok_r(t, "-", &save)) != NULL) {\r
264 +       i++;\r
265 +       switch(i) {\r
266 +       case 1:\r
267 +           if (month(token, &m) != 0)\r
268 +               return 1;\r
269 +           break;\r
270 +       case 2:\r
271 +           if (day(token, &d) != 0)\r
272 +               return 1;\r
273 +           break;\r
274 +       default:\r
275 +           return 1;\r
276 +       }\r
277 +       t = NULL;\r
278 +    }\r
279 +    today(&n, after);\r
280 +    if (after) {\r
281 +       if (m < n.tm_mon)\r
282 +           n.tm_year++;\r
283 +    } else {\r
284 +       if (m > n.tm_mon)\r
285 +           n.tm_year--;\r
286 +    }\r
287 +    switch (i) {\r
288 +    case 1:\r
289 +       n.tm_mday = 1;\r
290 +       n.tm_mon = m;\r
291 +       *first = mktime(&n);\r
292 +       if (++n.tm_mon > 11) {\r
293 +           n.tm_mon = 0;\r
294 +           n.tm_year++;\r
295 +       }\r
296 +       *last = mktime(&n);\r
297 +       return 0;\r
298 +    case 2:\r
299 +       n.tm_mday = d;\r
300 +       n.tm_mon = m;\r
301 +       *first = mktime(&n);\r
302 +       *last = *first + DAY;\r
303 +       return 0;\r
304 +    }\r
305 +    return 1;\r
306 +}\r
307 +\r
308 +/* year[-month[-day]] */\r
309 +static int parse_iso(const char *text, time_t *first, time_t *last, time_t after) {\r
310 +    int                y = 0, m = 0, d = 0;\r
311 +    int                i;\r
312 +    struct tm  n;\r
313 +    char       tmp[80];\r
314 +    char       *t;\r
315 +    char       *save;\r
316 +    char       *token;\r
317 +\r
318 +    if(strlen (text) >= sizeof (tmp))\r
319 +       return 1;\r
320 +    strcpy(tmp, text);\r
321 +\r
322 +    t = tmp;\r
323 +    save = NULL;\r
324 +    i = 0;\r
325 +    while ((token = strtok_r(t, "-", &save)) != NULL) {\r
326 +       i++;\r
327 +       switch(i) {\r
328 +       case 1:\r
329 +           if (year(token, &y) != 0)\r
330 +               return 1;\r
331 +           break;\r
332 +       case 2:\r
333 +           if (month(token, &m) != 0)\r
334 +               return 1;\r
335 +           break;\r
336 +       case 3:\r
337 +           if (day(token, &d) != 0)\r
338 +               return 1;\r
339 +           break;\r
340 +       default:\r
341 +           return 1;\r
342 +       }\r
343 +       t = NULL;\r
344 +    }\r
345 +    today(&n, 0);\r
346 +    switch (i) {\r
347 +    case 1:\r
348 +       n.tm_mday = 1;\r
349 +       n.tm_mon = 0;\r
350 +       n.tm_year = y;\r
351 +       *first = mktime(&n);\r
352 +       n.tm_year = y + 1;\r
353 +       *last = mktime(&n);\r
354 +       return 0;\r
355 +    case 2:\r
356 +       n.tm_mday = 1;\r
357 +       n.tm_mon = m;\r
358 +       n.tm_year = y;\r
359 +       *first = mktime(&n);\r
360 +       if (++n.tm_mon > 11) {\r
361 +           n.tm_mon = 0;\r
362 +           n.tm_year++;\r
363 +       }\r
364 +       *last = mktime(&n);\r
365 +       return 0;\r
366 +    case 3:\r
367 +       n.tm_mday = d;\r
368 +       n.tm_mon = m;\r
369 +       n.tm_year = y;\r
370 +       *first = mktime(&n);\r
371 +       *last = *first + DAY;\r
372 +       return 0;\r
373 +    }\r
374 +    return 1;\r
375 +}\r
376 +\r
377 +/* month[/day[/year]] */\r
378 +static int parse_us(const char *text, time_t *first, time_t *last, time_t after) {\r
379 +    int                y = 0, m = 0, d = 0;\r
380 +    int                i;\r
381 +    struct tm  n;\r
382 +    char       tmp[80];\r
383 +    char       *t;\r
384 +    char       *save;\r
385 +    char       *token;\r
386 +\r
387 +    if(strlen (text) >= sizeof (tmp))\r
388 +       return 1;\r
389 +    strcpy(tmp, text);\r
390 +\r
391 +    t = tmp;\r
392 +    save = NULL;\r
393 +    i = 0;\r
394 +    while ((token = strtok_r(t, "/", &save)) != NULL) {\r
395 +       i++;\r
396 +       switch(i) {\r
397 +       case 1:\r
398 +           if (month(token, &m) != 0)\r
399 +               return 1;\r
400 +           break;\r
401 +       case 2:\r
402 +           if (day(token, &d) != 0)\r
403 +               return 1;\r
404 +           break;\r
405 +       case 3:\r
406 +           if (year(token, &y) != 0)\r
407 +               return 1;\r
408 +           break;\r
409 +       default:\r
410 +           return 1;\r
411 +       }\r
412 +       t = NULL;\r
413 +    }\r
414 +    today(&n, after);\r
415 +    if (after) {\r
416 +       if (m < n.tm_mon)\r
417 +           n.tm_year++;\r
418 +    } else {\r
419 +       if (m > n.tm_mon)\r
420 +           n.tm_year--;\r
421 +    }\r
422 +    switch (i) {\r
423 +    case 1:\r
424 +       n.tm_mday = 1;\r
425 +       n.tm_mon = m;\r
426 +       *first = mktime(&n);\r
427 +       if (++n.tm_mon > 11) {\r
428 +           n.tm_mon = 0;\r
429 +           n.tm_year++;\r
430 +       }\r
431 +       *last = mktime(&n);\r
432 +       return 0;\r
433 +    case 2:\r
434 +       n.tm_mday = d;\r
435 +       n.tm_mon = m;\r
436 +       *first = mktime(&n);\r
437 +       *last = *first + DAY;\r
438 +       return 0;\r
439 +    case 3:\r
440 +       n.tm_mday = d;\r
441 +       n.tm_mon = m;\r
442 +       n.tm_year = y;\r
443 +       *first = mktime(&n);\r
444 +       *last = *first + DAY;\r
445 +       return 0;\r
446 +    }\r
447 +    return 1;\r
448 +}\r
449 +\r
450 +static int (*parsers[])(const char *text, time_t *first, time_t *last, time_t after) = {\r
451 +    parse_today,\r
452 +    parse_yesterday,\r
453 +    parse_thisweek,\r
454 +    parse_lastweek,\r
455 +    parse_thismonth,\r
456 +    parse_lastmonth,\r
457 +    parse_month,\r
458 +    parse_iso,\r
459 +    parse_us,\r
460 +    0,\r
461 +};\r
462 +\r
463 +static notmuch_status_t\r
464 +notmuch_one_date(const char *text, time_t *first, time_t *last, time_t after)\r
465 +{\r
466 +    int                i;\r
467 +    for (i = 0; parsers[i]; i++)\r
468 +       if (parsers[i](text, first, last, after) == 0)\r
469 +           return NOTMUCH_STATUS_SUCCESS;\r
470 +    return NOTMUCH_STATUS_INVALID_DATE;\r
471 +}\r
472 +\r
473 +notmuch_status_t\r
474 +notmuch_date(const char *text, time_t *first, time_t *last)\r
475 +{\r
476 +    char       *dots;\r
477 +    char       first_text[80], last_text[80];\r
478 +    notmuch_status_t   status;\r
479 +    time_t     first_first, first_last, last_first, last_last;\r
480 +\r
481 +    if (strlen(text) > sizeof (first_text))\r
482 +       return NOTMUCH_STATUS_INVALID_DATE;\r
483 +    dots = strstr(text, "..");\r
484 +    if (dots) {\r
485 +       strncpy(first_text, text, dots - text);\r
486 +       first_text[dots-text] = '\0';\r
487 +       status = notmuch_one_date(first_text, &first_first, &first_last, 0);\r
488 +       if (status)\r
489 +           return status;\r
490 +       status = notmuch_one_date(dots + 2, &last_first, &last_last, first_first);\r
491 +       if (status)\r
492 +           return status;\r
493 +       *first = first_first;\r
494 +       *last = last_last;\r
495 +       return 0;\r
496 +    }\r
497 +    return notmuch_one_date(text, first, last, 0);\r
498 +}\r
499 +\r
500 +#if 1\r
501 +int\r
502 +main (int argc, char **argv)\r
503 +{\r
504 +    int        i;\r
505 +    for (i = 1; i < argc; i++) {\r
506 +       time_t  first, last;\r
507 +\r
508 +       if (notmuch_date(argv[i], &first, &last) == 0) {\r
509 +           char        first_string[80], last_string[80];\r
510 +\r
511 +           ctime_r(&first, first_string);\r
512 +           first_string[strlen(first_string)-1] = '\0';\r
513 +           ctime_r(&last, last_string);\r
514 +           last_string[strlen(last_string)-1] = '\0';\r
515 +           printf ("%s: %s - %s\n", argv[i], first_string, last_string);\r
516 +       }\r
517 +    }\r
518 +}\r
519 +#endif\r
520 -- \r
521 1.6.3.3\r
522 \r