unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Sebastian Spaeth <Sebastian@SSpaeth.de>
To: notmuch@notmuchmail.org
Subject: [PATCH 3/4] integrate date parser
Date: Mon,  8 Feb 2010 12:14:11 +0100	[thread overview]
Message-ID: <1265627652-25912-3-git-send-email-Sebastian@SSpaeth.de> (raw)
In-Reply-To: <1265627652-25912-2-git-send-email-Sebastian@SSpaeth.de>

Integrate and make use of the notmuch_parse_date() function in date.c that is being called by the new MaildateValueRangeProcessor in lib/database.cc
Thanks to keithp for donating the date parser for achieving this in a nice way.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 lib/Makefile.local |    1 +
 lib/database.cc    |   33 ++++++++++++++++++++++++++++++++-
 lib/notmuch.h      |   17 +++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletions(-)

diff --git a/lib/Makefile.local b/lib/Makefile.local
index 70489e1..44deaf8 100644
--- a/lib/Makefile.local
+++ b/lib/Makefile.local
@@ -3,6 +3,7 @@ extra_cflags += -I$(dir)
 
 libnotmuch_c_srcs =		\
 	$(dir)/libsha1.c	\
+	$(dir)/date.c	\
 	$(dir)/message-file.c	\
 	$(dir)/messages.c	\
 	$(dir)/sha1.c		\
diff --git a/lib/database.cc b/lib/database.cc
index ddda933..78cd898 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -496,6 +496,37 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+struct MaildateValueRangeProcessor : public Xapian::ValueRangeProcessor {
+    MaildateValueRangeProcessor() {}
+
+    Xapian::valueno operator()(std::string &begin, std::string &end) {
+      time_t begin_first,begin_last, end_first, end_last;
+      int retval;
+
+      if (begin.substr(0, 5) != "date:")
+	 return Xapian::BAD_VALUENO;
+      begin.erase(0, 5);
+
+      retval = notmuch_parse_date(begin.c_str(), &begin_first, &begin_last, 0);
+
+      if (retval == NOTMUCH_STATUS_INVALID_DATE) {
+	fprintf(stderr,"Begin date failed to parse: %s",begin.c_str());
+	return Xapian::BAD_VALUENO;
+      }
+
+      retval = notmuch_parse_date(end.c_str(),&end_first,&end_last,begin_first);
+      if (retval == NOTMUCH_STATUS_INVALID_DATE) {
+	fprintf(stderr,"End date failed to parse: %s",end.c_str());
+	return Xapian::BAD_VALUENO;
+      }
+
+      begin.assign(Xapian::sortable_serialise(begin_first));
+      end.assign(Xapian::sortable_serialise(end_last));
+
+      return NOTMUCH_VALUE_TIMESTAMP;
+    }
+};
+
 notmuch_database_t *
 notmuch_database_open (const char *path,
 		       notmuch_database_mode_t mode)
@@ -572,7 +603,7 @@ notmuch_database_open (const char *path,
 	notmuch->query_parser = new Xapian::QueryParser;
 	notmuch->term_gen = new Xapian::TermGenerator;
 	notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
-	notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
+	notmuch->value_range_processor = new MaildateValueRangeProcessor();
 
 	notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
 	notmuch->query_parser->set_database (*notmuch->xapian_db);
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 56a76d1..fa8f41a 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -1089,6 +1089,23 @@ notmuch_filenames_advance (notmuch_filenames_t *filenames);
 void
 notmuch_filenames_destroy (notmuch_filenames_t *filenames);
 
+notmuch_status_t
+notmuch_parse_date(const char *text, time_t *first, time_t *last, time_t after);
+/* Parse a string into the first and last possible timestamps.
+ * It parses the possible formats and stops if one pattern matches.
+ * Keywords: 'today','yesterday','thisweek','lastweek','thismonth',
+ *           'lastmonth'
+ * Month-day : month[-day]] (month: January, Jan, or 1)\n"
+ * ISO format: year[-month[-day]]
+ * US format : month[/day[/year]]
+ *
+ * 'after' is used to fill in bits from context if left out, e.g. a
+ * 'date:2004..01' will find from 2004-01-01 through 2004-01-31
+ *
+ * Return values:
+ * NOTMUCH_STATUS_SUCCESS
+ * NOTMUCH_STATUS_INVALID_DATE: Error parsing the date string
+ */
 NOTMUCH_END_DECLS
 
 #endif
-- 
1.6.3.3

  reply	other threads:[~2010-02-08 11:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-08  9:41 date parser branch updated and rebased Sebastian Spaeth
2010-02-08 11:14 ` [PATCH 1/4] add date parser file from Keith Sebastian Spaeth
2010-02-08 11:14   ` [PATCH 2/4] Introduce new notmuch return status NOTMUCH_STATUS_INVALID_DATE Sebastian Spaeth
2010-02-08 11:14     ` Sebastian Spaeth [this message]
2010-02-08 11:14       ` [PATCH 4/4] add documentation for the new daterange parser and remove TODO list item Sebastian Spaeth
2010-02-08 12:57         ` [PATCH] notmuch-test: add tests for counting within absolute time ranges and 'today' Sebastian Spaeth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1265627652-25912-3-git-send-email-Sebastian@SSpaeth.de \
    --to=sebastian@sspaeth.de \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).