]> git.notmuchmail.org Git - notmuch/blob - lib/parse-time-vrp.cc
lib: optionally support single argument date: queries
[notmuch] / lib / parse-time-vrp.cc
1 /* parse-time-vrp.cc - date range query glue
2  *
3  * This file is part of notmuch.
4  *
5  * Copyright © 2012 Jani Nikula
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see http://www.gnu.org/licenses/ .
19  *
20  * Author: Jani Nikula <jani@nikula.org>
21  */
22
23 #include "database-private.h"
24 #include "parse-time-vrp.h"
25 #include "parse-time-string.h"
26
27 #define PREFIX "date:"
28
29 /* See *ValueRangeProcessor in xapian-core/api/valuerangeproc.cc */
30 Xapian::valueno
31 ParseTimeValueRangeProcessor::operator() (std::string &begin, std::string &end)
32 {
33     time_t t, now;
34     std::string b;
35
36     /* Require date: prefix in start of the range... */
37     if (STRNCMP_LITERAL (begin.c_str (), PREFIX))
38         return Xapian::BAD_VALUENO;
39
40     /* ...and remove it. */
41     begin.erase (0, sizeof (PREFIX) - 1);
42     b = begin;
43
44     /* Use the same 'now' for begin and end. */
45     if (time (&now) == (time_t) -1)
46         return Xapian::BAD_VALUENO;
47
48     if (!begin.empty ()) {
49         if (parse_time_string (begin.c_str (), &t, &now, PARSE_TIME_ROUND_DOWN))
50             return Xapian::BAD_VALUENO;
51
52         begin.assign (Xapian::sortable_serialise ((double) t));
53     }
54
55     if (!end.empty ()) {
56         if (end == "!" && ! b.empty ())
57             end = b;
58
59         if (parse_time_string (end.c_str (), &t, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
60             return Xapian::BAD_VALUENO;
61
62         end.assign (Xapian::sortable_serialise ((double) t));
63     }
64
65     return valno;
66 }
67
68 #if HAVE_XAPIAN_FIELD_PROCESSOR
69 /* XXX TODO: is throwing an exception the right thing to do here? */
70 Xapian::Query DateFieldProcessor::operator()(const std::string & str) {
71     time_t from, to, now;
72
73     /* Use the same 'now' for begin and end. */
74     if (time (&now) == (time_t) -1)
75         throw Xapian::QueryParserError("Unable to get current time");
76
77     if (parse_time_string (str.c_str (), &from, &now, PARSE_TIME_ROUND_DOWN))
78         throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
79
80     if (parse_time_string (str.c_str (), &to, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
81         throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
82
83     return Xapian::Query(Xapian::Query::OP_AND,
84                          Xapian::Query(Xapian::Query::OP_VALUE_GE, 0, Xapian::sortable_serialise ((double) from)),
85                          Xapian::Query(Xapian::Query::OP_VALUE_LE, 0, Xapian::sortable_serialise ((double) to)));
86 }
87 #endif