[notmuch] [PATCH 2/4] add date parser file from Keith

2010-01-26 Thread Sebastian Spaeth
From: Keith Packard 

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 the 1st of the previous month to the 1st of the current month).
---
 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 000..09c5ef9
--- /dev/null
+++ b/lib/date.c
@@ -0,0 +1,455 @@
+/*
+ * Copyright ?? 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 = after;
+else
+   time(&t);
+localtime_r(&t, result);
+result->tm_sec = result->tm_min = result->tm_hour = 0;
+}
+
+static int parse_today(const char *text, time_t *first, time_t *last, time_t 
after) {
+if (strcasecmp(text, "today") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *first = mktime(&n);
+   *last = *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") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *last = mktime(&n);
+   *first = *last - DAY;
+   return 0;
+}
+return 1;
+}
+
+static int parse_thisweek(const char *text, time_t *first, time_t *last, 
time_t after) {
+if (strcasecmp(text, "thisweek") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *first = mktime(&n) - (n.tm_wday * DAY);
+   *last = *first + DAY * 7;
+   return 0;
+}
+return 1;
+}
+
+static int parse_lastweek(const char *text, time_t *first, time_t *last, 
time_t after) {
+if (strcasecmp(text, "lastweek") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *last = mktime(&n) - (n.tm_wday * DAY);
+   *first = *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") == 0) {
+   struct tm n;
+   today(&n, 0);
+   n.tm_mday = 1;
+   *first = mktime(&n);
+   if (n.tm_mon++ == 12) {
+   n.tm_mon = 0;
+   n.tm_year++;
+   }
+   *last = 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") == 0) {
+   struct tm n;
+   today(&n, 0);
+   n.tm_mday = 1;
+   if (n.tm_mon == 0) {
+   n.tm_year--;
+   n.tm_mon = 11;
+   } else
+   n.tm_mon--;
+   *first = mktime(&n);
+   if (n.tm_mon++ == 12) {
+   n.tm_mon = 0;
+   n.tm_year++;
+   }
+   *last = mktime(&n);
+   return 0;
+}
+return 1;
+}
+
+static const char *months[12][2] = {
+{ "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 = strtol(text, &end, 10);
+if (end == text)
+   return 1;
+if (*end != '\0')
+   return 1;
+if (*y < 1970 || *y > 2038)
+   return 1;
+*y -= 1900;
+return 0;
+}
+
+static int month(const char *text, int *m) {
+char *end;
+int i;
+for (i = 0; i < 12; i++) {
+   if (strcasecmp(text, months[i][0]) == 0 ||
+   strcasecmp(text, months[i][1]) == 0)
+   {
+   *m = i;
+   return 0;
+   }
+}
+*m = strtol(text, &end, 10);
+if (end == text)
+   return 1;
+if (*end != '\0')
+   return 1;
+if (*m < 1 || *m > 12)
+   return 1;
+*m -= 1;
+return 0;
+}
+
+static int day(con

[notmuch] [PATCH 2/4] add date parser file from Keith

2010-01-26 Thread Sebastian Spaeth
From: Keith Packard 

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 the 1st of the previous month to the 1st of the current month).
---
 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 000..09c5ef9
--- /dev/null
+++ b/lib/date.c
@@ -0,0 +1,455 @@
+/*
+ * Copyright © 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 = after;
+else
+   time(&t);
+localtime_r(&t, result);
+result->tm_sec = result->tm_min = result->tm_hour = 0;
+}
+
+static int parse_today(const char *text, time_t *first, time_t *last, time_t 
after) {
+if (strcasecmp(text, "today") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *first = mktime(&n);
+   *last = *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") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *last = mktime(&n);
+   *first = *last - DAY;
+   return 0;
+}
+return 1;
+}
+
+static int parse_thisweek(const char *text, time_t *first, time_t *last, 
time_t after) {
+if (strcasecmp(text, "thisweek") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *first = mktime(&n) - (n.tm_wday * DAY);
+   *last = *first + DAY * 7;
+   return 0;
+}
+return 1;
+}
+
+static int parse_lastweek(const char *text, time_t *first, time_t *last, 
time_t after) {
+if (strcasecmp(text, "lastweek") == 0) {
+   struct tm n;
+   today(&n, 0);
+   *last = mktime(&n) - (n.tm_wday * DAY);
+   *first = *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") == 0) {
+   struct tm n;
+   today(&n, 0);
+   n.tm_mday = 1;
+   *first = mktime(&n);
+   if (n.tm_mon++ == 12) {
+   n.tm_mon = 0;
+   n.tm_year++;
+   }
+   *last = 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") == 0) {
+   struct tm n;
+   today(&n, 0);
+   n.tm_mday = 1;
+   if (n.tm_mon == 0) {
+   n.tm_year--;
+   n.tm_mon = 11;
+   } else
+   n.tm_mon--;
+   *first = mktime(&n);
+   if (n.tm_mon++ == 12) {
+   n.tm_mon = 0;
+   n.tm_year++;
+   }
+   *last = mktime(&n);
+   return 0;
+}
+return 1;
+}
+
+static const char *months[12][2] = {
+{ "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 = strtol(text, &end, 10);
+if (end == text)
+   return 1;
+if (*end != '\0')
+   return 1;
+if (*y < 1970 || *y > 2038)
+   return 1;
+*y -= 1900;
+return 0;
+}
+
+static int month(const char *text, int *m) {
+char *end;
+int i;
+for (i = 0; i < 12; i++) {
+   if (strcasecmp(text, months[i][0]) == 0 ||
+   strcasecmp(text, months[i][1]) == 0)
+   {
+   *m = i;
+   return 0;
+   }
+}
+*m = strtol(text, &end, 10);
+if (end == text)
+   return 1;
+if (*end != '\0')
+   return 1;
+if (*m < 1 || *m > 12)
+   return 1;
+*m -= 1;
+return 0;
+}
+
+static int day(con