unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH] Make the date parser nicer
@ 2010-01-22 15:26 Sebastian Spaeth
  2010-01-22 15:33 ` Sebastian Spaeth
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Sebastian Spaeth @ 2010-01-22 15:26 UTC (permalink / raw)
  To: notmuch

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...

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 lib/database.cc |   94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 93 insertions(+), 1 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 5b12320..102a6ff 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -494,6 +494,97 @@ _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) {
+        if (begin.substr(0, 5) != "date:")
+            return Xapian::BAD_VALUENO;
+        begin.erase(0, 5);
+
+	// Parse the begin date to time_t
+	struct tm *timeinfo;
+	time_t begintime, endtime;
+	//const char * startptr;
+	int year, month, day;
+
+	if (begin.size() == 8) {
+	  int no_items;
+	  no_items = sscanf(begin.c_str(), "%4i%2i%2i", &year, &month, &day);
+	  if (no_items != 3)
+	    return Xapian::BAD_VALUENO;
+	} else if (begin.size() == 6) {
+	  int no_items;
+	  day = 1;
+	  no_items = sscanf(begin.c_str(), "%4i%2i", &year, &month);
+	  if (no_items != 2)
+	    return Xapian::BAD_VALUENO;
+	} else if (begin.size() == 4) {
+	  int no_items;
+	  day = 1;
+	  month = 1;
+	  no_items = sscanf(begin.c_str(), "%4i", &year);
+	  if (no_items != 1)
+	    return Xapian::BAD_VALUENO;
+	} else {
+	  // no expected time format
+	  return Xapian::BAD_VALUENO;
+	}
+
+	begintime = time(NULL);
+	timeinfo = localtime( &begintime );
+	timeinfo -> tm_year = year - 1900;
+	timeinfo -> tm_mon = month - 1;
+	fprintf (stderr, "Startdate %d %d %d\n",year,month,day);
+	timeinfo -> tm_mday = day;
+	begintime = mktime ( timeinfo );
+
+	if (begintime == -1)
+	  // no valid time format
+	  return Xapian::BAD_VALUENO;
+
+	if (end.size() == 8) {
+	  int no_items;
+	  no_items = sscanf(end.c_str(), "%4i%2i%2i", &year, &month, &day);
+	  if (no_items != 3)
+	    return Xapian::BAD_VALUENO;
+	} else if (end.size() == 6) {
+	  int no_items;
+	  day = 31;
+	  no_items = sscanf(end.c_str(), "%4i%2i", &year, &month);
+	  if (no_items != 2)
+	    return Xapian::BAD_VALUENO;
+	} else if (end.size() == 4) {
+	  int no_items;
+	  day = 31;
+	  month = 12;
+	  no_items = sscanf(end.c_str(), "%4i", &year);
+	  if (no_items != 1)
+	    return Xapian::BAD_VALUENO;
+	} else {
+	  // no expected time format
+	  return Xapian::BAD_VALUENO;
+	}
+
+	timeinfo = localtime( &begintime );
+	timeinfo -> tm_year = year - 1900;
+	timeinfo -> tm_mon = month - 1;
+	fprintf (stderr, "Enddate %d %d %d\n",year,month,day);
+	timeinfo -> tm_mday = day;
+	endtime = mktime ( timeinfo );
+	//XXX: plus 1 day to make the last day inclusive??
+
+	if (endtime == -1)
+	  // no valid time format
+	  return Xapian::BAD_VALUENO;
+	
+	begin.assign(Xapian::sortable_serialise(begintime));
+	end.assign(Xapian::sortable_serialise(endtime));
+
+        return NOTMUCH_VALUE_TIMESTAMP;
+    }
+};
+
 notmuch_database_t *
 notmuch_database_open (const char *path,
 		       notmuch_database_mode_t mode)
@@ -570,7 +661,8 @@ 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, "date:", true);
+	notmuch->value_range_processor = new MaildateValueRangeProcessor();
+	  // (NOTMUCH_VALUE_TIMESTAMP);
 
 	notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
 	notmuch->query_parser->set_database (*notmuch->xapian_db);
-- 
1.6.3.3

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-01-27  9:15 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-22 15:26 [PATCH] Make the date parser nicer Sebastian Spaeth
2010-01-22 15:33 ` Sebastian Spaeth
2010-01-22 16:04 ` Sebastian Spaeth
2010-01-24 14:13 ` Sebastian Spaeth
2010-01-25 10:50   ` [PATCH] Make the date parser nicer. This is v3 and considered to be final (but the documentation) Sebastian Spaeth
2010-01-25 12:22     ` [PATCH] Make the date parser nicer (v3 + 'now' keyword) Sebastian Spaeth
2010-01-25 13:14       ` [PATCH] Make the date parser nicer (v3 + 'now' keyword) (final mail) Sebastian Spaeth
2010-01-26  6:36 ` [PATCH] Make the date parser nicer Keith Packard
2010-01-26  9:12   ` Sebastian Spaeth
2010-01-26 11:50   ` Sebastian Spaeth
2010-01-26 17:55     ` Keith Packard
2010-01-27  9:15       ` Sebastian Spaeth

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).