1 /* parse-time-vrp.cc - date range query glue
3 * This file is part of notmuch.
5 * Copyright © 2012 Jani Nikula
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see https://www.gnu.org/licenses/ .
20 * Author: Jani Nikula <jani@nikula.org>
23 #include "database-private.h"
24 #include "parse-time-vrp.h"
25 #include "parse-time-string.h"
27 #define PREFIX "date:"
29 /* See *ValueRangeProcessor in xapian-core/api/valuerangeproc.cc */
31 ParseTimeValueRangeProcessor::operator() (std::string &begin, std::string &end)
36 /* Require date: prefix in start of the range... */
37 if (STRNCMP_LITERAL (begin.c_str (), PREFIX))
38 return Xapian::BAD_VALUENO;
40 /* ...and remove it. */
41 begin.erase (0, sizeof (PREFIX) - 1);
44 /* Use the same 'now' for begin and end. */
45 if (time (&now) == (time_t) -1)
46 return Xapian::BAD_VALUENO;
48 if (!begin.empty ()) {
49 if (parse_time_string (begin.c_str (), &t, &now, PARSE_TIME_ROUND_DOWN))
50 return Xapian::BAD_VALUENO;
52 begin.assign (Xapian::sortable_serialise ((double) t));
56 if (end == "!" && ! b.empty ())
59 if (parse_time_string (end.c_str (), &t, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
60 return Xapian::BAD_VALUENO;
62 end.assign (Xapian::sortable_serialise ((double) t));
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) {
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");
77 if (parse_time_string (str.c_str (), &from, &now, PARSE_TIME_ROUND_DOWN))
78 throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
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 + "'");
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)));