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"
28 _notmuch_date_strings_to_query (Xapian::valueno slot,
29 const std::string &begin, const std::string &end,
30 Xapian::Query &output, std::string &msg)
32 double from = DBL_MIN, to = DBL_MAX;
33 time_t parsed_time, now;
36 /* Use the same 'now' for begin and end. */
37 if (time (&now) == (time_t) -1) {
38 msg = "unable to get current time";
39 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
42 if (! begin.empty ()) {
43 if (parse_time_string (begin.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN)) {
44 msg = "Didn't understand date specification '" + begin + "'";
45 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
48 from = (double) parsed_time;
52 if (end == "!" && ! begin.empty ())
57 if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE)) {
58 msg = "Didn't understand date specification '" + str + "'";
59 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
61 to = (double) parsed_time;
64 output = Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
65 Xapian::sortable_serialise (from),
66 Xapian::sortable_serialise (to));
67 return NOTMUCH_STATUS_SUCCESS;
71 ParseTimeRangeProcessor::operator() (const std::string &begin, const std::string &end)
77 if (_notmuch_date_strings_to_query (slot, begin, end, output, msg))
78 throw Xapian::QueryParserError (msg);
83 /* XXX TODO: is throwing an exception the right thing to do here? */
85 DateFieldProcessor::operator() (const std::string & str)
87 double from = DBL_MIN, to = DBL_MAX;
88 time_t parsed_time, now;
90 /* Use the same 'now' for begin and end. */
91 if (time (&now) == (time_t) -1)
92 throw Xapian::QueryParserError ("Unable to get current time");
94 if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_DOWN))
95 throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
97 from = (double) parsed_time;
99 if (parse_time_string (str.c_str (), &parsed_time, &now, PARSE_TIME_ROUND_UP_INCLUSIVE))
100 throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'");
102 to = (double) parsed_time;
104 return Xapian::Query (Xapian::Query::OP_VALUE_RANGE, slot,
105 Xapian::sortable_serialise (from),
106 Xapian::sortable_serialise (to));